Skip to content

Commit

Permalink
Type fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrich committed Jun 11, 2024
1 parent ff6eb17 commit 77bfa04
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 94 deletions.
4 changes: 2 additions & 2 deletions src/custom/schema/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {type SchemaVerifyInit} from '../../schema/verify/init';
/**
* @category Schema – Custom Type
*/
export interface CustomSchemaVerify<InputT = unknown, DataT = unknown>
extends SchemaVerifyInit<InputT, DataT> {
export interface CustomSchemaVerify<DataT = unknown, InputT = unknown>
extends SchemaVerifyInit<DataT, InputT> {
typeId: string;
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export {SchemaOptions} from './schema/options';
export {SchemaOutputTransformer} from './schema/output/transformer';
export {SchemaVerifyFlags} from './schema/verify/flags';
export {SchemaVerifyInit} from './schema/verify/init';
export {SchemaVerifyValue} from './schema/verify/value';
export {SchemaVerifyValue} from './schema/verify/field';
export {Statement} from './statement';
export {Tracer} from './tracer';
export {TracerInit} from './tracer/init';
Expand Down
36 changes: 17 additions & 19 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import {builtinTypes} from './builtin/types';
import {valueTypeLabel} from './value/type/label';
import {Tracer} from './tracer';
import {type SchemaVerifyInit} from './schema/verify/init';
import {type SchemaVerifyValue} from './schema/verify/value';
import {type SchemaVerifyField} from './schema/verify/field';
import {type VerifiedField} from './verified/field';
import {type VerifiedMap} from './verified/map';

Expand Down Expand Up @@ -91,7 +91,7 @@ export class Schema<DataT, InputT extends SchemaData<DataT>, TransformedT = Inpu

public async verifyField(
field: SchemaField<DataT>,
value: InputT | SchemaData<InputT>,
value: DataT | SchemaData<DataT>,
tracer: Tracer,
base: Log
): Promise<Fate<VerifiedField<DataT> | VerifiedField<DataT>[] | null>> {
Expand Down Expand Up @@ -173,7 +173,7 @@ export class Schema<DataT, InputT extends SchemaData<DataT>, TransformedT = Inpu

public async verifyFieldValue(
field: SchemaField<DataT>,
value: InputT | SchemaData<InputT>,
value: DataT | SchemaData<DataT>,
tracer: Tracer,
base: Log
): Promise<Fate<VerifiedField<DataT> | VerifiedField<DataT>[]>> {
Expand All @@ -198,7 +198,7 @@ export class Schema<DataT, InputT extends SchemaData<DataT>, TransformedT = Inpu
const result = await this.verifyValue({
fieldId: field.name,
fieldType: baseType,
value: value,
data: value,
tracer: tracer,
base: base
});
Expand Down Expand Up @@ -276,48 +276,46 @@ export class Schema<DataT, InputT extends SchemaData<DataT>, TransformedT = Inpu
* Check if value type matches type for primitives, and whether the content matches
* the expected range or format (if any).
*/
public async verifyValue(
init: SchemaVerifyValue<InputT, DataT>
): Promise<Fate<DataT | VerifiedMap<DataT>>> {
public async verifyValue(init: SchemaVerifyField<DataT>): Promise<Fate<DataT | VerifiedMap<DataT>>> {
const fate = new Fate<DataT | VerifiedMap<DataT>>();

if (this.isBuiltIn(init.fieldType)) {
if (this.valueHasBuiltinType(init.fieldType, init.value)) {
if (this.valueHasBuiltinType(init.fieldType, init.data)) {
// TODO: Add validation here. Type match does not automatically prove valid content.
fate.data = init.value;
fate.data = init.data;
return fate.setSuccess(true);
} else {
return fate.setErrorCode(
schemaError(
`field_does_not_support_value_type:${valueTypeLabel(init.value)}`,
`field_does_not_support_value_type:${valueTypeLabel(init.data)}`,
init.tracer.current()
)
);
}
}

const baseType = init.fieldType.endsWith('[]') ? init.fieldType.substring(0, -2) : init.fieldType;
if (this.customTypes.hasSchema(init.fieldType) && typeof init.value === 'object') {
if (this.customTypes.hasSchema(init.fieldType) && typeof init.data === 'object') {
return this.customTypes.verifyOnly({
id: init.fieldId,
typeId: baseType,
value: init.value,
value: init.data,
tracer: init.tracer,
base: init.base,
childSchema: true
});
}

if (this.customTypes.hasVerifier(init.fieldType)) {
return this.customTypes.verifyValue(init.fieldId, baseType, init.value, init.base);
return this.customTypes.verifyValue(init.fieldId, baseType, init.data, init.base);
}

return fate.setErrorCode(
schemaError(`field_does_not_support_type:${baseType}`, init.tracer.current())
);
}

public async verify(init: SchemaVerifyInit<InputT, DataT>): Promise<Fate<TransformedT | null>> {
public async verify(init: SchemaVerifyInit<DataT>): Promise<Fate<TransformedT | null>> {
const fate = new Fate<TransformedT | null>();

if (!init) {
Expand Down Expand Up @@ -385,7 +383,7 @@ export class Schema<DataT, InputT extends SchemaData<DataT>, TransformedT = Inpu
* Verify provided data object's structure, content, and types against this schema.
* @param init
*/
public async verifyOnly(init: SchemaVerifyInit<InputT, DataT>): Promise<Fate<VerifiedMap<DataT>>> {
public async verifyOnly(init: SchemaVerifyInit<DataT>): Promise<Fate<VerifiedMap<DataT>>> {
const fate = new Fate<VerifiedMap<DataT>>();

const currPath = init.tracer ? init.tracer : new Tracer();
Expand All @@ -398,13 +396,13 @@ export class Schema<DataT, InputT extends SchemaData<DataT>, TransformedT = Inpu

const log = init.base.makeLog(`schema:${currPath.current()}`);

if (init.value === undefined || init.value === null) {
if (init.data === undefined || init.data === null) {
return fate.setErrorCode(
schemaError('missing_schema_data', currPath.current(), 'verify', 'init.value')
schemaError('missing_schema_data', currPath.current(), 'verify', 'init.data')
);
}

if (Object.keys(init.value)?.length === 0) {
if (Object.keys(init.data)?.length === 0) {
return fate.setErrorCode(schemaError('empty_schema_object', currPath.current(), 'verify'));
}

Expand Down Expand Up @@ -443,7 +441,7 @@ export class Schema<DataT, InputT extends SchemaData<DataT>, TransformedT = Inpu

for (const [id, field] of this.fields.entries()) {
const name = id.toString();
const verified = await this.verifyField(field, init.value[name], currPath, init.base);
const verified = await this.verifyField(field, init.data[name], currPath, init.base);

if (!verified.ok()) {
return fate.setErrorCode(verified.errorCode());
Expand Down
4 changes: 2 additions & 2 deletions src/schema/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {type CustomTypesData} from '../custom/types/data';
*
* @category Schema
*/
export interface SchemaInit<DataT, InputT, OutputT = InputT> {
export interface SchemaInit<DataT, InputT, TransformedT = InputT> {
/**
* @name Schema Name
*/
Expand All @@ -50,7 +50,7 @@ export interface SchemaInit<DataT, InputT, OutputT = InputT> {
* @description (optional) Flags & options that change verifier behavior.
*/
options?: SchemaOptions;
transformOutput?: SchemaOutputTransformer<DataT, OutputT | null>;
transformOutput?: SchemaOutputTransformer<DataT, TransformedT | null>;
customTypes?: CustomTypesData<DataT> | null;
base: Log;
parentPath?: string[];
Expand Down
4 changes: 2 additions & 2 deletions src/schema/verify/value.ts → src/schema/verify/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ import {type SchemaData} from '../data';
/**
* @category Schema
*/
export interface SchemaVerifyValue<InputT = unknown, DataT = unknown> {
export interface SchemaVerifyField<DataT = unknown> {
fieldId: string;
fieldType: SchemaFieldType<DataT>;
tracer: Tracer;
value: InputT | SchemaData<InputT>;
data: DataT | SchemaData<DataT>;
base: Log;
}
6 changes: 3 additions & 3 deletions src/schema/verify/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ import {type SchemaData} from '../data';
*
* @category Schema
*/
export interface SchemaVerifyInit<InputT = unknown, DataT = unknown> {
export interface SchemaVerifyInit<DataT = unknown> {
/** Optional ID used in printed schema paths. Schema name is used when ID not provided. */
id?: string;
/** Value to using this schema. */
value: InputT | SchemaData<InputT>;
/** Data to be verified and optionally transformed. */
data: DataT | SchemaData<DataT>;
/** Current path of properties accessed to reach this point. */
tracer?: Tracer;
base: Log;
Expand Down
4 changes: 2 additions & 2 deletions tests/_data/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ export interface SampleData extends SchemaData<Primitive> {
bool1: boolean;
}

export class SampleSchema extends Schema<Primitive, SampleData, SampleData> {
constructor(init: SchemaInit<Primitive, SampleData, SampleData>) {
export class SampleSchema extends Schema<Primitive, SampleData> {
constructor(init: SchemaInit<Primitive, SampleData>) {
super({
name: 'SampleSchema',
fields: [
Expand Down
Loading

0 comments on commit 77bfa04

Please sign in to comment.