diff --git a/src/value/parse/parse.ts b/src/value/parse/parse.ts index 8fa83e48..4e4b0840 100644 --- a/src/value/parse/parse.ts +++ b/src/value/parse/parse.ts @@ -41,50 +41,57 @@ import { Clone } from '../clone/index' // ------------------------------------------------------------------ import { IsArray } from '../guard/index' +// ------------------------------------------------------------------ +// Error +// ------------------------------------------------------------------ +export class ParseError extends TypeBoxError { + constructor(message: string) { + super(message) + } +} + // ------------------------------------------------------------------ // ParseRegistry // ------------------------------------------------------------------ export type TParseOperation = 'Clone' | 'Clean' | 'Default' | 'Convert' | 'Assert' | 'Decode' | ({} & string) - -export type TParseCallback = (schema: TSchema, references: TSchema[], value: unknown) => unknown +export type TParseFunction = (schema: TSchema, references: TSchema[], value: unknown) => unknown // prettier-ignore export namespace ParseRegistry { - const map = new Map([ - ['Clone', (_schema: TSchema, _references: TSchema[], value: unknown) => Clone(value)], - ['Clean', (schema: TSchema, references: TSchema[], value: unknown) => Clean(schema, references, value)], - ['Default', (schema: TSchema, references: TSchema[], value: unknown) => Default(schema, references, value)], - ['Convert', (schema: TSchema, references: TSchema[], value: unknown) => Convert(schema, references, value)], - ['Assert', (schema: TSchema, references: TSchema[], value: unknown) => { Assert(schema, references, value); return value }], - ['Decode', (schema: TSchema, references: TSchema[], value: unknown) => (HasTransform(schema, references) ? TransformDecode(schema, references, value) : value)], - ['Encode', (schema: TSchema, references: TSchema[], value: unknown) => (HasTransform(schema, references) ? TransformEncode(schema, references, value) : value)], + const registry = new Map([ + ['Clone', (_schema, _references, value: unknown) => Clone(value)], + ['Clean', (schema, references, value: unknown) => Clean(schema, references, value)], + ['Default', (schema, references, value: unknown) => Default(schema, references, value)], + ['Convert', (schema, references, value: unknown) => Convert(schema, references, value)], + ['Assert', (schema, references, value: unknown) => { Assert(schema, references, value); return value }], + ['Decode', (schema, references, value: unknown) => (HasTransform(schema, references) ? TransformDecode(schema, references, value) : value)], + ['Encode', (schema, references, value: unknown) => (HasTransform(schema, references) ? TransformEncode(schema, references, value) : value)], ]) // Deletes an entry from the registry export function Delete(key: string): void { - map.delete(key) + registry.delete(key) } // Sets an entry in the registry - export function Set(key: string, callback: TParseCallback): void { - map.set(key, callback) + export function Set(key: string, callback: TParseFunction): void { + registry.set(key, callback) } // Gets an entry in the registry - export function Get(key: string): TParseCallback | undefined { - return map.get(key) + export function Get(key: string): TParseFunction | undefined { + return registry.get(key) } } - // ------------------------------------------------------------------ // ParseDefault: Default Sequence // ------------------------------------------------------------------ // prettier-ignore -export const ParseDefault: TParseOperation[] = [ - 'Clone', - 'Clean', - 'Default', - 'Convert', - 'Assert', +export const ParseDefault = [ + 'Clone', + 'Clean', + 'Default', + 'Convert', + 'Assert', 'Decode' -] +] as const // ------------------------------------------------------------------ // ParseValue @@ -92,14 +99,14 @@ export const ParseDefault: TParseOperation[] = [ function ParseValue = StaticDecode>(keys: TParseOperation[], type: Type, references: TSchema[], value: unknown): Result { return keys.reduce((value, key) => { const reducer = ParseRegistry.Get(key) - if (reducer === undefined) throw new TypeBoxError(`Parse: Unable to resolve reduce key '${key}'`) + if (reducer === undefined) throw new TypeBoxError(`Parse: Unable to find parse operation '${key}'`) return reducer(type, references, value) }, value) as Result } + // ------------------------------------------------------------------ // Parse // ------------------------------------------------------------------ - /** Parses a value using the default parse pipeline. Will throws an `AssertError` if invalid. */ export function Parse, Result extends Output = Output>(schema: Type, references: TSchema[], value: unknown): Result /** Parses a value using the default parse pipeline. Will throws an `AssertError` if invalid. */ @@ -115,7 +122,7 @@ export function Parse(...args: any[]): unknown { args.length === 4 ? [args[0], args[1], args[2], args[3]] : args.length === 3 ? IsArray(args[0]) ? [args[0], args[1], [], args[2]] : [ParseDefault, args[0], args[1], args[2]] : args.length === 2 ? [ParseDefault, args[0], [], args[1]] : - (() => { throw new TypeBoxError('Parse: Invalid Arguments') }) () + (() => { throw new TypeBoxError('Parse: Invalid Arguments') })() ) return ParseValue(reducers, schema, references, value) }