From cd95195efe94594d2eaf9be39ef12523b15255e3 Mon Sep 17 00:00:00 2001 From: Michael Brich Date: Sat, 8 Jun 2024 22:31:10 -0700 Subject: [PATCH] Working out type issue between input data and schema data. --- src/rule.ts | 1 + src/schema.ts | 38 +++++++++++++++++++------------------- src/schema/field/data.ts | 10 +++++----- src/schema/init.ts | 2 +- src/schema/verify/init.ts | 4 ++-- src/schema/verify/value.ts | 4 ++-- src/verified/field.ts | 1 + 7 files changed, 31 insertions(+), 29 deletions(-) diff --git a/src/rule.ts b/src/rule.ts index 9ccd6d0..569c602 100644 --- a/src/rule.ts +++ b/src/rule.ts @@ -48,6 +48,7 @@ export class Rule implements Resettable, Verifier { /** * @description IMPORTANT: New properties intended using rule syntax MUST be added to * the switch statement in `value.ts`. + * @internal */ public readonly statements: Statement[]; public readonly does: BlockWithNot>; diff --git a/src/schema.ts b/src/schema.ts index c2fde0d..c4e4131 100644 --- a/src/schema.ts +++ b/src/schema.ts @@ -45,14 +45,14 @@ import {type SchemaVerifyInit} from './schema/verify/init'; import {type SchemaVerifyValue} from './schema/verify/value'; import {type VerifiedField} from './verified/field'; import {type VerifiedMap} from './verified/map'; -import {VerifiedSchema} from './verified/schema'; +import {type VerifiedSchema} from './verified/schema'; /** * @category Schema */ export class Schema, TransformedT = InputT> { public readonly schemaName: string; - public readonly fields: Map>; + public readonly fields: Map>; public readonly cfg: SchemaConfig; public readonly transformOutput: SchemaOutputTransformer; public readonly customTypes: CustomTypes; @@ -76,27 +76,27 @@ export class Schema, TransformedT = Inpu * Build and set schema fields from schema init data. * @param fields */ - private _makeFields(fields: SchemaFieldData[]): Map> { - const result = new Map>(); + private _makeFields(fields: SchemaFieldData[]): Map> { + const result = new Map>(); if (!Array.isArray(fields)) { return result; } for (const data of fields) { - result.set(data.name, new SchemaField(data)); + result.set(data.name, new SchemaField(data)); } return result; } public async verifyField( - field: SchemaField, + field: SchemaField, value: InputT | SchemaData, tracer: Tracer, base: Log - ): Promise | VerifiedField[]>> { - const fate = new Fate | VerifiedField[]>(); + ): Promise | VerifiedField[] | null>> { + const fate = new Fate | VerifiedField[] | null>(); if (!field) { return fate.setErrorCode(schemaError(`missing_field`, tracer.current())); @@ -143,12 +143,12 @@ export class Schema, TransformedT = Inpu } public async verifyFieldValues( - field: SchemaField, - values: DataT[] | SchemaData[], + field: SchemaField, + values: InputT[] | SchemaData[], tracer: Tracer, base: Log - ): Promise[]>> { - const fate = new Fate[]>({ + ): Promise[] | null>> { + const fate = new Fate[] | null>({ data: [] }); @@ -173,7 +173,7 @@ export class Schema, TransformedT = Inpu } public async verifyFieldValue( - field: SchemaField, + field: SchemaField, value: InputT | SchemaData, tracer: Tracer, base: Log @@ -189,7 +189,7 @@ export class Schema, TransformedT = Inpu } for (const type of field.types) { - const baseType = (type.endsWith('[]') ? type.slice(0, -2) : type) as SchemaFieldType; + const baseType = (type.endsWith('[]') ? type.slice(0, -2) : type) as SchemaFieldType; if (!this.schemaSupportsType(baseType)) { return fate.setErrorCode( schemaError(`schema_does_not_support_type:${baseType}`, tracer.current()) @@ -217,11 +217,11 @@ export class Schema, TransformedT = Inpu ); } - public isBuiltIn(type: SchemaFieldType): boolean { - return builtinTypes().includes(type); + public isBuiltIn(type: SchemaFieldType): boolean { + return builtinTypes().includes(type); } - public schemaSupportsType(type: SchemaFieldType): boolean { + public schemaSupportsType(type: SchemaFieldType): boolean { if (typeof type !== 'string' || !type) { return false; } @@ -238,7 +238,7 @@ export class Schema, TransformedT = Inpu * @param type * @param value */ - public valueHasBuiltinType(type: SchemaFieldType, value: unknown): value is DataT { + public valueHasBuiltinType(type: SchemaFieldType, value: unknown): value is DataT { if (typeof type !== 'string') { return false; } @@ -412,7 +412,7 @@ export class Schema, TransformedT = Inpu const total = this.fields.size; let processed = 0; const fieldCount = this.fields.size; - const mapped: VerifiedMap = new Map | null>(); + const mapped = new Map | VerifiedField[] | null>(); if (fieldCount === 0) { if (init?.flags?.allowEmptyInputObject !== true) { diff --git a/src/schema/field/data.ts b/src/schema/field/data.ts index 966bd81..bb32b42 100644 --- a/src/schema/field/data.ts +++ b/src/schema/field/data.ts @@ -30,10 +30,10 @@ import {type SchemaFieldType} from './type'; /** * @category Schema – Field */ -export interface SchemaFieldData { - name: keyof InputT; - types: SchemaFieldType | SchemaFieldType[]; - type?: SchemaFieldType; +export interface SchemaFieldData { + name: keyof DataT; + types: SchemaFieldType | SchemaFieldType[]; + type?: SchemaFieldType; defaultValue?: unknown; - rules?: Block>[]; + rules?: Block>[]; } diff --git a/src/schema/init.ts b/src/schema/init.ts index 974d440..72b8472 100644 --- a/src/schema/init.ts +++ b/src/schema/init.ts @@ -44,7 +44,7 @@ export interface SchemaInit { * @name Schema Fields * @description Details for each field that should be verified. */ - fields: SchemaFieldData[]; + fields: SchemaFieldData[]; /** * @name Schema Options * @description (optional) Flags & options that change verifier behavior. diff --git a/src/schema/verify/init.ts b/src/schema/verify/init.ts index 98066bf..1f69eb8 100644 --- a/src/schema/verify/init.ts +++ b/src/schema/verify/init.ts @@ -33,11 +33,11 @@ import {type SchemaData} from '../data'; * * @category Schema */ -export interface SchemaVerifyInit { +export interface SchemaVerifyInit { /** Optional ID used in printed schema paths. Schema name is used when ID not provided. */ id?: string; /** Value to using this schema. */ - value: DataT | SchemaData; + value: InputT | SchemaData; /** Current path of properties accessed to reach this point. */ tracer?: Tracer; base: Log; diff --git a/src/schema/verify/value.ts b/src/schema/verify/value.ts index c9f5af9..79bdb1f 100644 --- a/src/schema/verify/value.ts +++ b/src/schema/verify/value.ts @@ -33,8 +33,8 @@ import {type SchemaData} from '../data'; */ export interface SchemaVerifyValue { fieldId: string; - fieldType: SchemaFieldType; + fieldType: SchemaFieldType; tracer: Tracer; - value: DataT | SchemaData; + value: InputT | SchemaData; base: Log; } diff --git a/src/verified/field.ts b/src/verified/field.ts index 1e8c3e9..3f51244 100644 --- a/src/verified/field.ts +++ b/src/verified/field.ts @@ -32,6 +32,7 @@ import {type VerifiedSchema} from './schema'; */ export type VerifiedField = | DataT + | null | VerifiedSchema | DataT[] | VerifiedSchema[];