Skip to content

Commit

Permalink
Working out type issue between input data and schema data.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrich committed Jun 9, 2024
1 parent c5e4488 commit cd95195
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 29 deletions.
1 change: 1 addition & 0 deletions src/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export class Rule<InputT> 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<InputT>[];
public readonly does: BlockWithNot<BlockDoes<InputT>>;
Expand Down
38 changes: 19 additions & 19 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<DataT, InputT extends SchemaData<DataT>, TransformedT = InputT> {
public readonly schemaName: string;
public readonly fields: Map<keyof InputT, SchemaField<InputT>>;
public readonly fields: Map<keyof DataT, SchemaField<DataT>>;
public readonly cfg: SchemaConfig;
public readonly transformOutput: SchemaOutputTransformer<DataT, TransformedT | null>;
public readonly customTypes: CustomTypes<DataT, InputT, TransformedT>;
Expand All @@ -76,27 +76,27 @@ export class Schema<DataT, InputT extends SchemaData<DataT>, TransformedT = Inpu
* Build and set schema fields from schema init data.
* @param fields
*/
private _makeFields(fields: SchemaFieldData<InputT>[]): Map<keyof InputT, SchemaField<InputT>> {
const result = new Map<keyof InputT, SchemaField<InputT>>();
private _makeFields(fields: SchemaFieldData<DataT>[]): Map<keyof DataT, SchemaField<DataT>> {
const result = new Map<keyof DataT, SchemaField<DataT>>();

if (!Array.isArray(fields)) {
return result;
}

for (const data of fields) {
result.set(data.name, new SchemaField<InputT>(data));
result.set(data.name, new SchemaField<DataT>(data));
}

return result;
}

public async verifyField(
field: SchemaField<InputT>,
field: SchemaField<DataT>,
value: InputT | SchemaData<InputT>,
tracer: Tracer,
base: Log
): Promise<Fate<VerifiedField<DataT> | VerifiedField<DataT>[]>> {
const fate = new Fate<VerifiedField<DataT> | VerifiedField<DataT>[]>();
): Promise<Fate<VerifiedField<DataT> | VerifiedField<DataT>[] | null>> {
const fate = new Fate<VerifiedField<DataT> | VerifiedField<DataT>[] | null>();

if (!field) {
return fate.setErrorCode(schemaError(`missing_field`, tracer.current()));
Expand Down Expand Up @@ -143,12 +143,12 @@ export class Schema<DataT, InputT extends SchemaData<DataT>, TransformedT = Inpu
}

public async verifyFieldValues(
field: SchemaField<InputT>,
values: DataT[] | SchemaData<InputT>[],
field: SchemaField<DataT>,
values: InputT[] | SchemaData<InputT>[],
tracer: Tracer,
base: Log
): Promise<Fate<VerifiedField<InputT>[]>> {
const fate = new Fate<VerifiedField<InputT>[]>({
): Promise<Fate<VerifiedField<DataT>[] | null>> {
const fate = new Fate<VerifiedField<DataT>[] | null>({
data: []
});

Expand All @@ -173,7 +173,7 @@ export class Schema<DataT, InputT extends SchemaData<DataT>, TransformedT = Inpu
}

public async verifyFieldValue(
field: SchemaField<InputT>,
field: SchemaField<DataT>,
value: InputT | SchemaData<InputT>,
tracer: Tracer,
base: Log
Expand All @@ -189,7 +189,7 @@ export class Schema<DataT, InputT extends SchemaData<DataT>, TransformedT = Inpu
}

for (const type of field.types) {
const baseType = (type.endsWith('[]') ? type.slice(0, -2) : type) as SchemaFieldType<InputT>;
const baseType = (type.endsWith('[]') ? type.slice(0, -2) : type) as SchemaFieldType<DataT>;
if (!this.schemaSupportsType(baseType)) {
return fate.setErrorCode(
schemaError(`schema_does_not_support_type:${baseType}`, tracer.current())
Expand Down Expand Up @@ -217,11 +217,11 @@ export class Schema<DataT, InputT extends SchemaData<DataT>, TransformedT = Inpu
);
}

public isBuiltIn(type: SchemaFieldType<InputT>): boolean {
return builtinTypes<InputT>().includes(type);
public isBuiltIn(type: SchemaFieldType<DataT>): boolean {
return builtinTypes<DataT>().includes(type);
}

public schemaSupportsType(type: SchemaFieldType<InputT>): boolean {
public schemaSupportsType(type: SchemaFieldType<DataT>): boolean {
if (typeof type !== 'string' || !type) {
return false;
}
Expand All @@ -238,7 +238,7 @@ export class Schema<DataT, InputT extends SchemaData<DataT>, TransformedT = Inpu
* @param type
* @param value
*/
public valueHasBuiltinType(type: SchemaFieldType<InputT>, value: unknown): value is DataT {
public valueHasBuiltinType(type: SchemaFieldType<DataT>, value: unknown): value is DataT {
if (typeof type !== 'string') {
return false;
}
Expand Down Expand Up @@ -412,7 +412,7 @@ export class Schema<DataT, InputT extends SchemaData<DataT>, TransformedT = Inpu
const total = this.fields.size;
let processed = 0;
const fieldCount = this.fields.size;
const mapped: VerifiedMap<DataT> = new Map<string, VerifiedField<DataT> | null>();
const mapped = new Map<string, VerifiedField<DataT> | VerifiedField<DataT>[] | null>();

if (fieldCount === 0) {
if (init?.flags?.allowEmptyInputObject !== true) {
Expand Down
10 changes: 5 additions & 5 deletions src/schema/field/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ import {type SchemaFieldType} from './type';
/**
* @category Schema – Field
*/
export interface SchemaFieldData<InputT = unknown> {
name: keyof InputT;
types: SchemaFieldType<InputT> | SchemaFieldType<InputT>[];
type?: SchemaFieldType<InputT>;
export interface SchemaFieldData<DataT = unknown> {
name: keyof DataT;
types: SchemaFieldType<DataT> | SchemaFieldType<DataT>[];
type?: SchemaFieldType<DataT>;
defaultValue?: unknown;
rules?: Block<Statement<InputT>>[];
rules?: Block<Statement<DataT>>[];
}
2 changes: 1 addition & 1 deletion src/schema/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export interface SchemaInit<DataT, InputT, OutputT = InputT> {
* @name Schema Fields
* @description Details for each field that should be verified.
*/
fields: SchemaFieldData<InputT>[];
fields: SchemaFieldData<DataT>[];
/**
* @name Schema Options
* @description (optional) Flags & options that change verifier behavior.
Expand Down
4 changes: 2 additions & 2 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<DataT = unknown> {
export interface SchemaVerifyInit<InputT = unknown, 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: DataT | SchemaData<DataT>;
value: InputT | SchemaData<InputT>;
/** Current path of properties accessed to reach this point. */
tracer?: Tracer;
base: Log;
Expand Down
4 changes: 2 additions & 2 deletions src/schema/verify/value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ import {type SchemaData} from '../data';
*/
export interface SchemaVerifyValue<DataT = unknown, InputT = unknown> {
fieldId: string;
fieldType: SchemaFieldType<InputT>;
fieldType: SchemaFieldType<DataT>;
tracer: Tracer;
value: DataT | SchemaData<DataT>;
value: InputT | SchemaData<InputT>;
base: Log;
}
1 change: 1 addition & 0 deletions src/verified/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {type VerifiedSchema} from './schema';
*/
export type VerifiedField<DataT = unknown> =
| DataT
| null
| VerifiedSchema<DataT>
| DataT[]
| VerifiedSchema<DataT>[];

0 comments on commit cd95195

Please sign in to comment.