@sinclair/typebox
Advanced tools
Comparing version 0.31.22 to 0.31.28
@@ -65,7 +65,7 @@ "use strict"; | ||
throw new transform_1.TransformDecodeCheckError(this.schema, value, this.Errors(value).First()); | ||
return this.hasTransform ? transform_1.DecodeTransform.Decode(this.schema, this.references, value, (_, __, value) => this.Check(value)) : value; | ||
return this.hasTransform ? transform_1.DecodeTransform.Decode(this.schema, this.references, value) : value; | ||
} | ||
/** Encodes a value or throws if error */ | ||
Encode(value) { | ||
const encoded = this.hasTransform ? transform_1.EncodeTransform.Encode(this.schema, this.references, value, (_, __, value) => this.Check(value)) : value; | ||
const encoded = this.hasTransform ? transform_1.EncodeTransform.Encode(this.schema, this.references, value) : value; | ||
if (!this.checkFunc(encoded)) | ||
@@ -72,0 +72,0 @@ throw new transform_1.TransformEncodeCheckError(this.schema, value, this.Errors(value).First()); |
{ | ||
"name": "@sinclair/typebox", | ||
"version": "0.31.22", | ||
"version": "0.31.28", | ||
"description": "JSONSchema Type Builder with Static Type Resolution for TypeScript", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -125,3 +125,3 @@ export declare const Transform: unique symbol; | ||
} | ||
export type TConstructorParameters<T extends TConstructor<TSchema[], TSchema>> = TTuple<T['parameters']>; | ||
export type TConstructorParameters<T extends TConstructor<TSchema[], TSchema>> = Ensure<TTuple<T['parameters']>>; | ||
export type TInstanceType<T extends TConstructor<TSchema[], TSchema>> = T['returns']; | ||
@@ -134,8 +134,8 @@ export type TCompositeKeys<T extends TObject[]> = T extends [infer L, ...infer R] ? keyof Assert<L, TObject>['properties'] | TCompositeKeys<Assert<R, TObject[]>> : never; | ||
export type TComposite<T extends TObject[]> = TIntersect<T> extends TIntersect ? TObject<TCompositeReduce<T>> : TObject<{}>; | ||
export type TConstructorParameterArray<T extends readonly TSchema[], P extends unknown[]> = [...{ | ||
[K in keyof T]: Static<AssertType<T[K]>, P>; | ||
}]; | ||
export type TConstructorReturnTypeResolve<T extends TSchema, P extends unknown[]> = Static<T, P>; | ||
export type TConstructorParametersResolve<T extends TSchema[], P extends unknown[]> = T extends [infer L extends TSchema, ...infer R extends TSchema[]] ? [Static<L, P>, ...TFunctionParametersResolve<R, P>] : []; | ||
export type TConstructorResolve<T extends TSchema[], U extends TSchema, P extends unknown[]> = Ensure<new (...param: TConstructorParametersResolve<T, P>) => TConstructorReturnTypeResolve<U, P>>; | ||
export interface TConstructor<T extends TSchema[] = TSchema[], U extends TSchema = TSchema> extends TSchema { | ||
[Kind]: 'Constructor'; | ||
static: new (...param: TConstructorParameterArray<T, this['params']>) => Static<U, this['params']>; | ||
static: TConstructorResolve<T, U, this['params']>; | ||
type: 'Constructor'; | ||
@@ -188,8 +188,8 @@ parameters: T; | ||
export type TExtract<T extends TSchema, U extends TSchema> = T extends TTemplateLiteral ? TExtractTemplateLiteral<T, U> : T extends TUnion<infer S> ? TExtractArray<S, U> : T extends U ? T : T; | ||
export type TFunctionParameters<T extends TSchema[], P extends unknown[]> = [...{ | ||
[K in keyof T]: Static<AssertType<T[K]>, P>; | ||
}]; | ||
export type TFunctionReturnTypeResolve<T extends TSchema, P extends unknown[]> = Static<T, P>; | ||
export type TFunctionParametersResolve<T extends TSchema[], P extends unknown[]> = T extends [infer L extends TSchema, ...infer R extends TSchema[]] ? [Static<L, P>, ...TFunctionParametersResolve<R, P>] : []; | ||
export type TFunctionResolve<T extends TSchema[], U extends TSchema, P extends unknown[]> = Ensure<(...param: TFunctionParametersResolve<T, P>) => TFunctionReturnTypeResolve<U, P>>; | ||
export interface TFunction<T extends TSchema[] = TSchema[], U extends TSchema = TSchema> extends TSchema { | ||
[Kind]: 'Function'; | ||
static: (...param: TFunctionParameters<T, this['params']>) => Static<U, this['params']>; | ||
static: TFunctionResolve<T, U, this['params']>; | ||
type: 'Function'; | ||
@@ -321,3 +321,3 @@ parameters: T; | ||
export type TPartialProperties<T extends TProperties> = Evaluate<AssertProperties<{ | ||
[K in keyof T]: TOptional<T[K]>; | ||
[K in keyof T]: T[K] extends (TReadonlyOptional<infer S>) ? TReadonlyOptional<S> : T[K] extends (TReadonly<infer S>) ? TReadonlyOptional<S> : T[K] extends (TOptional<infer S>) ? TOptional<S> : TOptional<T[K]>; | ||
}>>; | ||
@@ -394,3 +394,3 @@ export type TPartial<T extends TSchema> = T extends TRecursive<infer S> ? TRecursive<TPartial<S>> : T extends TIntersect<infer S> ? TIntersect<TPartialRest<S>> : T extends TUnion<infer S> ? TUnion<TPartialRest<S>> : T extends TObject<infer S> ? TObject<TPartialProperties<S>> : T; | ||
export type TRequiredProperties<T extends TProperties> = Evaluate<AssertProperties<{ | ||
[K in keyof T]: T[K] extends TOptional<infer S> ? S : T[K]; | ||
[K in keyof T]: T[K] extends (TReadonlyOptional<infer S>) ? TReadonly<S> : T[K] extends (TReadonly<infer S>) ? TReadonly<S> : T[K] extends (TOptional<infer S>) ? S : T[K]; | ||
}>>; | ||
@@ -439,7 +439,7 @@ export type TRequired<T extends TSchema> = T extends TRecursive<infer S> ? TRecursive<TRequired<S>> : T extends TIntersect<infer S> ? TIntersect<TRequiredRest<S>> : T extends TUnion<infer S> ? TUnion<TRequiredRest<S>> : T extends TObject<infer S> ? TObject<TRequiredProperties<S>> : T; | ||
}[number] : T extends TTemplateLiteral ? `${Static<T>}` : T extends TLiteral<infer U> ? `${U}` : T extends TString ? `${string}` : T extends TNumber ? `${number}` : T extends TBigInt ? `${bigint}` : T extends TBoolean ? `${boolean}` : never; | ||
export type TTemplateLiteralUnion<T extends TTemplateLiteralKind[], Acc extends string = ''> = T extends [infer L, ...infer R] ? `${TTemplateLiteralConst<L, Acc>}${TTemplateLiteralUnion<Assert<R, TTemplateLiteralKind[]>, Acc>}` : Acc; | ||
export type TTemplateLiteralUnion<T extends TTemplateLiteralKind[], Acc extends string> = T extends [infer L, ...infer R] ? `${TTemplateLiteralConst<L, Acc>}${TTemplateLiteralUnion<Assert<R, TTemplateLiteralKind[]>, Acc>}` : Acc; | ||
export type TTemplateLiteralKeyRest<T extends TTemplateLiteral> = Assert<UnionToTuple<Static<T>>, TPropertyKey[]>; | ||
export interface TTemplateLiteral<T extends TTemplateLiteralKind[] = TTemplateLiteralKind[]> extends TSchema { | ||
[Kind]: 'TemplateLiteral'; | ||
static: TTemplateLiteralUnion<T>; | ||
static: TTemplateLiteralUnion<T, ''>; | ||
type: 'string'; | ||
@@ -452,3 +452,3 @@ pattern: string; | ||
export type DecodeRest<T extends TSchema[]> = T extends [infer L extends TSchema, ...infer R extends TSchema[]] ? [DecodeType<L>, ...DecodeRest<R>] : []; | ||
export type DecodeType<T extends TSchema> = (T extends TOptional<infer S extends TSchema> ? TOptional<DecodeType<S>> : T extends TReadonly<infer S extends TSchema> ? TReadonly<DecodeType<S>> : T extends TTransform<infer _, infer R> ? TUnsafe<R> : T extends TArray<infer S extends TSchema> ? TArray<DecodeType<S>> : T extends TAsyncIterator<infer S extends TSchema> ? TAsyncIterator<DecodeType<S>> : T extends TConstructor<infer P extends TSchema[], infer R extends TSchema> ? TConstructor<P, DecodeType<R>> : T extends TEnum<infer S> ? TEnum<S> : T extends TFunction<infer P extends TSchema[], infer R extends TSchema> ? TFunction<P, DecodeType<R>> : T extends TIntersect<infer S extends TSchema[]> ? TIntersect<DecodeRest<S>> : T extends TIterator<infer S extends TSchema> ? TIterator<DecodeType<S>> : T extends TNot<infer S extends TSchema> ? TNot<DecodeType<S>> : T extends TObject<infer S> ? TObject<Evaluate<DecodeProperties<S>>> : T extends TPromise<infer S extends TSchema> ? TPromise<DecodeType<S>> : T extends TRecord<infer K, infer S> ? TRecord<K, DecodeType<S>> : T extends TRecursive<infer S extends TSchema> ? TRecursive<DecodeType<S>> : T extends TRef<infer S extends TSchema> ? TRef<DecodeType<S>> : T extends TTuple<infer S extends TSchema[]> ? TTuple<DecodeRest<S>> : T extends TUnion<infer S extends TSchema[]> ? TUnion<DecodeRest<S>> : T); | ||
export type DecodeType<T extends TSchema> = (T extends TOptional<infer S extends TSchema> ? TOptional<DecodeType<S>> : T extends TReadonly<infer S extends TSchema> ? TReadonly<DecodeType<S>> : T extends TTransform<infer _, infer R> ? TUnsafe<R> : T extends TArray<infer S extends TSchema> ? TArray<DecodeType<S>> : T extends TAsyncIterator<infer S extends TSchema> ? TAsyncIterator<DecodeType<S>> : T extends TConstructor<infer P extends TSchema[], infer R extends TSchema> ? TConstructor<DecodeRest<P>, DecodeType<R>> : T extends TEnum<infer S> ? TEnum<S> : T extends TFunction<infer P extends TSchema[], infer R extends TSchema> ? TFunction<DecodeRest<P>, DecodeType<R>> : T extends TIntersect<infer S extends TSchema[]> ? TIntersect<DecodeRest<S>> : T extends TIterator<infer S extends TSchema> ? TIterator<DecodeType<S>> : T extends TNot<infer S extends TSchema> ? TNot<DecodeType<S>> : T extends TObject<infer S> ? TObject<Evaluate<DecodeProperties<S>>> : T extends TPromise<infer S extends TSchema> ? TPromise<DecodeType<S>> : T extends TRecord<infer K, infer S> ? TRecord<K, DecodeType<S>> : T extends TRecursive<infer S extends TSchema> ? TRecursive<DecodeType<S>> : T extends TRef<infer S extends TSchema> ? TRef<DecodeType<S>> : T extends TTuple<infer S extends TSchema[]> ? TTuple<DecodeRest<S>> : T extends TUnion<infer S extends TSchema[]> ? TUnion<DecodeRest<S>> : T); | ||
export type TransformFunction<T = any, U = any> = (value: T) => U; | ||
@@ -519,7 +519,7 @@ export interface TransformOptions<I extends TSchema = TSchema, O extends unknown = unknown> { | ||
} | ||
/** Creates the decoded static form for a TypeBox type */ | ||
/** Creates an decoded static type from a TypeBox type */ | ||
export type StaticDecode<T extends TSchema, P extends unknown[] = []> = Static<DecodeType<T>, P>; | ||
/** Creates the encoded static form for a TypeBox type */ | ||
/** Creates an encoded static type from a TypeBox type */ | ||
export type StaticEncode<T extends TSchema, P extends unknown[] = []> = Static<T, P>; | ||
/** Creates the static type for a TypeBox type */ | ||
/** Creates a static type from a TypeBox type */ | ||
export type Static<T extends TSchema, P extends unknown[] = []> = (T & { | ||
@@ -571,2 +571,4 @@ params: P; | ||
function IsBoolean(value: unknown): value is boolean; | ||
/** Returns true if this value is a Date object */ | ||
function IsDate(value: unknown): value is Date; | ||
/** Returns true if this value is null */ | ||
@@ -580,2 +582,4 @@ function IsNull(value: unknown): value is null; | ||
function IsString(value: unknown): value is string; | ||
/** Returns true if this value is a Uint8Array */ | ||
function IsUint8Array(value: unknown): value is Uint8Array; | ||
/** Returns true if this value is undefined */ | ||
@@ -931,3 +935,3 @@ function IsUndefined(value: unknown): value is undefined; | ||
/** `[JavaScript]` Extracts the ConstructorParameters from the given Constructor type */ | ||
ConstructorParameters<T extends TConstructor<any[], any>>(schema: T, options?: SchemaOptions): TConstructorParameters<T>; | ||
ConstructorParameters<T extends TConstructor<TSchema[], TSchema>>(schema: T, options?: SchemaOptions): TConstructorParameters<T>; | ||
/** `[JavaScript]` Creates a Constructor type */ | ||
@@ -944,3 +948,3 @@ Constructor<T extends TSchema[], U extends TSchema>(parameters: [...T], returns: U, options?: SchemaOptions): TConstructor<T, U>; | ||
/** `[JavaScript]` Extracts the Parameters from the given Function type */ | ||
Parameters<T extends TFunction<any[], any>>(schema: T, options?: SchemaOptions): TParameters<T>; | ||
Parameters<T extends TFunction<TSchema[], TSchema>>(schema: T, options?: SchemaOptions): TParameters<T>; | ||
/** `[JavaScript]` Creates a Promise type */ | ||
@@ -947,0 +951,0 @@ Promise<T extends TSchema>(item: T, options?: SchemaOptions): TPromise<T>; |
@@ -165,3 +165,3 @@ "use strict"; | ||
else { | ||
return new Date(0); | ||
return new Date(); | ||
} | ||
@@ -168,0 +168,0 @@ } |
import { ValueError } from '../errors/errors'; | ||
import * as Types from '../typebox'; | ||
export type CheckFunction = (schema: Types.TSchema, references: Types.TSchema[], value: unknown) => boolean; | ||
export declare class TransformUnknownTypeError extends Types.TypeBoxError { | ||
readonly schema: Types.TRef | Types.TThis; | ||
constructor(schema: Types.TRef | Types.TThis); | ||
} | ||
export declare class TransformDecodeCheckError extends Types.TypeBoxError { | ||
@@ -37,7 +32,7 @@ readonly schema: Types.TSchema; | ||
export declare namespace DecodeTransform { | ||
function Decode(schema: Types.TSchema, references: Types.TSchema[], value: unknown, check: CheckFunction): unknown; | ||
function Decode(schema: Types.TSchema, references: Types.TSchema[], value: unknown): unknown; | ||
} | ||
/** Encodes a value using transform encoders if available. Does not ensure correct results. */ | ||
export declare namespace EncodeTransform { | ||
function Encode(schema: Types.TSchema, references: Types.TSchema[], value: unknown, check: CheckFunction): unknown; | ||
function Encode(schema: Types.TSchema, references: Types.TSchema[], value: unknown): unknown; | ||
} |
@@ -30,5 +30,6 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.EncodeTransform = exports.DecodeTransform = exports.HasTransform = exports.TransformEncodeError = exports.TransformDecodeError = exports.TransformEncodeCheckError = exports.TransformDecodeCheckError = exports.TransformUnknownTypeError = void 0; | ||
exports.EncodeTransform = exports.DecodeTransform = exports.HasTransform = exports.TransformEncodeError = exports.TransformDecodeError = exports.TransformEncodeCheckError = exports.TransformDecodeCheckError = void 0; | ||
const guard_1 = require("./guard"); | ||
const deref_1 = require("./deref"); | ||
const check_1 = require("./check"); | ||
const Types = require("../typebox"); | ||
@@ -38,9 +39,2 @@ // ------------------------------------------------------------------------- | ||
// ------------------------------------------------------------------------- | ||
class TransformUnknownTypeError extends Types.TypeBoxError { | ||
constructor(schema) { | ||
super(`Unknown type`); | ||
this.schema = schema; | ||
} | ||
} | ||
exports.TransformUnknownTypeError = TransformUnknownTypeError; | ||
class TransformDecodeCheckError extends Types.TypeBoxError { | ||
@@ -80,5 +74,5 @@ constructor(schema, value, error) { | ||
exports.TransformEncodeError = TransformEncodeError; | ||
// ------------------------------------------------------------------------- | ||
// ------------------------------------------------------------------ | ||
// HasTransform | ||
// ------------------------------------------------------------------------- | ||
// ------------------------------------------------------------------ | ||
/** Recursively checks a schema for transform codecs */ | ||
@@ -131,3 +125,3 @@ var HasTransform; | ||
function TTuple(schema, references) { | ||
return Types.TypeGuard.TTransform(schema) || (Types.TypeGuard.TSchema(schema.items) && schema.items.some((schema) => Visit(schema, references))); | ||
return Types.TypeGuard.TTransform(schema) || (!(0, guard_1.IsUndefined)(schema.items) && schema.items.some((schema) => Visit(schema, references))); | ||
} | ||
@@ -145,5 +139,2 @@ function TUnion(schema, references) { | ||
switch (schema[Types.Kind]) { | ||
// ------------------------------------------------------ | ||
// Structural | ||
// ------------------------------------------------------ | ||
case 'Array': | ||
@@ -177,25 +168,3 @@ return TArray(schema_, references_); | ||
return TUnion(schema_, references_); | ||
// ------------------------------------------------------ | ||
// Default | ||
// ------------------------------------------------------ | ||
case 'Any': | ||
case 'BigInt': | ||
case 'Boolean': | ||
case 'Date': | ||
case 'Integer': | ||
case 'Literal': | ||
case 'Never': | ||
case 'Null': | ||
case 'Number': | ||
case 'String': | ||
case 'Symbol': | ||
case 'TemplateLiteral': | ||
case 'Undefined': | ||
case 'Uint8Array': | ||
case 'Unknown': | ||
case 'Void': | ||
return Types.TypeGuard.TTransform(schema); | ||
default: | ||
if (!Types.TypeRegistry.Has(schema_[Types.Kind])) | ||
throw new TransformUnknownTypeError(schema_); | ||
return Types.TypeGuard.TTransform(schema); | ||
@@ -212,5 +181,5 @@ } | ||
})(HasTransform || (exports.HasTransform = HasTransform = {})); | ||
// ------------------------------------------------------------------------- | ||
// ------------------------------------------------------------------ | ||
// DecodeTransform | ||
// ------------------------------------------------------------------------- | ||
// ------------------------------------------------------------------ | ||
/** Decodes a value using transform decoders if available. Does not ensure correct results. */ | ||
@@ -227,38 +196,56 @@ var DecodeTransform; | ||
} | ||
// prettier-ignore | ||
function TArray(schema, references, value) { | ||
const elements1 = value.map((value) => Visit(schema.items, references, value)); | ||
return Default(schema, elements1); | ||
return ((0, guard_1.IsArray)(value)) | ||
? Default(schema, value.map((value) => Visit(schema.items, references, value))) | ||
: Default(schema, value); | ||
} | ||
// prettier-ignore | ||
function TIntersect(schema, references, value) { | ||
if (!(0, guard_1.IsPlainObject)(value) || (0, guard_1.IsValueType)(value)) | ||
return Default(schema, value); | ||
const keys = Types.KeyResolver.ResolveKeys(schema, { includePatterns: false }); | ||
const properties1 = Object.entries(value).reduce((acc, [key, value]) => { | ||
return !keys.includes(key) ? { ...acc, [key]: value } : { ...acc, [key]: Default(Types.IndexedAccessor.Resolve(schema, [key]), value) }; | ||
}, {}); | ||
if (!Types.TypeGuard.TTransform(schema.unevaluatedProperties)) | ||
return Default(schema, properties1); | ||
const properties2 = Object.entries(properties1).reduce((acc, [key, value]) => { | ||
return keys.includes(key) ? { ...acc, [key]: value } : { ...acc, [key]: Default(schema.unevaluatedProperties, value) }; | ||
}, {}); | ||
return Default(schema, properties2); | ||
const knownKeys = Types.KeyResolver.ResolveKeys(schema, { includePatterns: false }); | ||
const knownProperties = knownKeys.reduce((value, key) => { | ||
return (key in value) | ||
? { ...value, [key]: Visit(Types.IndexedAccessor.Resolve(schema, [key]), references, value[key]) } | ||
: value; | ||
}, value); | ||
if (!Types.TypeGuard.TTransform(schema.unevaluatedProperties)) { | ||
return Default(schema, knownProperties); | ||
} | ||
const unknownKeys = Object.getOwnPropertyNames(knownProperties); | ||
const unevaluatedProperties = schema.unevaluatedProperties; | ||
const unknownProperties = unknownKeys.reduce((value, key) => { | ||
return !knownKeys.includes(key) | ||
? { ...value, [key]: Default(unevaluatedProperties, value[key]) } | ||
: value; | ||
}, knownProperties); | ||
return Default(schema, unknownProperties); | ||
} | ||
function TNot(schema, references, value) { | ||
const value1 = Visit(schema.not, references, value); | ||
return Default(schema, value1); | ||
return Default(schema, Visit(schema.not, references, value)); | ||
} | ||
// prettier-ignore | ||
function TObject(schema, references, value) { | ||
if (!(0, guard_1.IsPlainObject)(value)) | ||
return Default(schema, value); | ||
const properties1 = Object.entries(value).reduce((acc, [key, value]) => { | ||
return !(key in schema.properties) ? { ...acc, [key]: value } : { ...acc, [key]: Visit(schema.properties[key], references, value) }; | ||
}, {}); | ||
if (!Types.TypeGuard.TSchema(schema.additionalProperties)) | ||
return Default(schema, properties1); | ||
const knownKeys = Types.KeyResolver.ResolveKeys(schema, { includePatterns: false }); | ||
const knownProperties = knownKeys.reduce((value, key) => { | ||
return (key in value) | ||
? { ...value, [key]: Visit(schema.properties[key], references, value[key]) } | ||
: value; | ||
}, value); | ||
if (!Types.TypeGuard.TSchema(schema.additionalProperties)) { | ||
return Default(schema, knownProperties); | ||
} | ||
const unknownKeys = Object.getOwnPropertyNames(knownProperties); | ||
const additionalProperties = schema.additionalProperties; | ||
const properties2 = Object.entries(properties1).reduce((acc, [key, value]) => { | ||
return key in schema.properties ? { ...acc, [key]: value } : { ...acc, [key]: Visit(additionalProperties, references, value) }; | ||
}, {}); | ||
return Default(schema, properties2); | ||
const unknownProperties = unknownKeys.reduce((value, key) => { | ||
return !knownKeys.includes(key) | ||
? { ...value, [key]: Default(additionalProperties, value[key]) } | ||
: value; | ||
}, knownProperties); | ||
return Default(schema, unknownProperties); | ||
} | ||
// prettier-ignore | ||
function TRecord(schema, references, value) { | ||
@@ -268,37 +255,42 @@ if (!(0, guard_1.IsPlainObject)(value)) | ||
const pattern = Object.getOwnPropertyNames(schema.patternProperties)[0]; | ||
const property = schema.patternProperties[pattern]; | ||
const regex = new RegExp(pattern); | ||
const properties1 = Object.entries(value).reduce((acc, [key, value]) => { | ||
return !regex.test(key) ? { ...acc, [key]: value } : { ...acc, [key]: Visit(property, references, value) }; | ||
}, {}); | ||
if (!Types.TypeGuard.TSchema(schema.additionalProperties)) | ||
return Default(schema, properties1); | ||
const knownKeys = new RegExp(pattern); | ||
const knownProperties = Object.getOwnPropertyNames(value).reduce((value, key) => { | ||
return knownKeys.test(key) | ||
? { ...value, [key]: Visit(schema.patternProperties[pattern], references, value[key]) } | ||
: value; | ||
}, value); | ||
if (!Types.TypeGuard.TSchema(schema.additionalProperties)) { | ||
return Default(schema, knownProperties); | ||
} | ||
const unknownKeys = Object.getOwnPropertyNames(knownProperties); | ||
const additionalProperties = schema.additionalProperties; | ||
const properties2 = Object.entries(properties1).reduce((acc, [key, value]) => { | ||
return regex.test(key) ? { ...acc, [key]: value } : { ...acc, [key]: Visit(additionalProperties, references, value) }; | ||
}, {}); | ||
return Default(schema, properties2); | ||
const unknownProperties = unknownKeys.reduce((value, key) => { | ||
return !knownKeys.test(key) | ||
? { ...value, [key]: Default(additionalProperties, value[key]) } | ||
: value; | ||
}, knownProperties); | ||
return Default(schema, unknownProperties); | ||
} | ||
function TRef(schema, references, value) { | ||
const target = (0, deref_1.Deref)(schema, references); | ||
const resolved = Visit(target, references, value); | ||
return Default(schema, resolved); | ||
return Default(schema, Visit(target, references, value)); | ||
} | ||
function TThis(schema, references, value) { | ||
const target = (0, deref_1.Deref)(schema, references); | ||
const resolved = Visit(target, references, value); | ||
return Default(schema, resolved); | ||
return Default(schema, Visit(target, references, value)); | ||
} | ||
// prettier-ignore | ||
function TTuple(schema, references, value) { | ||
const value1 = (0, guard_1.IsArray)(schema.items) ? schema.items.map((schema, index) => Visit(schema, references, value[index])) : []; | ||
return Default(schema, value1); | ||
return ((0, guard_1.IsArray)(value) && (0, guard_1.IsArray)(schema.items)) | ||
? Default(schema, schema.items.map((schema, index) => Visit(schema, references, value[index]))) | ||
: Default(schema, value); | ||
} | ||
function TUnion(schema, references, value) { | ||
const value1 = Default(schema, value); | ||
const defaulted = Default(schema, value); | ||
for (const subschema of schema.anyOf) { | ||
if (!checkFunction(subschema, references, value1)) | ||
if (!(0, check_1.Check)(subschema, references, defaulted)) | ||
continue; | ||
return Visit(subschema, references, value1); | ||
return Visit(subschema, references, defaulted); | ||
} | ||
return value1; | ||
return defaulted; | ||
} | ||
@@ -309,5 +301,2 @@ function Visit(schema, references, value) { | ||
switch (schema[Types.Kind]) { | ||
// ------------------------------------------------------ | ||
// Structural | ||
// ------------------------------------------------------ | ||
case 'Array': | ||
@@ -333,35 +322,7 @@ return TArray(schema_, references_, value); | ||
return TUnion(schema_, references_, value); | ||
// ------------------------------------------------------ | ||
// Default | ||
// ------------------------------------------------------ | ||
case 'Any': | ||
case 'AsyncIterator': | ||
case 'BigInt': | ||
case 'Boolean': | ||
case 'Constructor': | ||
case 'Date': | ||
case 'Function': | ||
case 'Integer': | ||
case 'Iterator': | ||
case 'Literal': | ||
case 'Never': | ||
case 'Null': | ||
case 'Number': | ||
case 'Promise': | ||
case 'String': | ||
case 'TemplateLiteral': | ||
case 'Undefined': | ||
case 'Uint8Array': | ||
case 'Unknown': | ||
case 'Void': | ||
return Default(schema_, value); | ||
default: | ||
if (!Types.TypeRegistry.Has(schema_[Types.Kind])) | ||
throw new TransformUnknownTypeError(schema_); | ||
return Default(schema_, value); | ||
} | ||
} | ||
let checkFunction = () => false; | ||
function Decode(schema, references, value, check) { | ||
checkFunction = check; | ||
function Decode(schema, references, value) { | ||
return Visit(schema, references, value); | ||
@@ -371,5 +332,5 @@ } | ||
})(DecodeTransform || (exports.DecodeTransform = DecodeTransform = {})); | ||
// ------------------------------------------------------------------------- | ||
// ------------------------------------------------------------------ | ||
// DecodeTransform | ||
// ------------------------------------------------------------------------- | ||
// ------------------------------------------------------------------ | ||
/** Encodes a value using transform encoders if available. Does not ensure correct results. */ | ||
@@ -386,54 +347,78 @@ var EncodeTransform; | ||
} | ||
// prettier-ignore | ||
function TArray(schema, references, value) { | ||
const elements1 = Default(schema, value); | ||
return elements1.map((value) => Visit(schema.items, references, value)); | ||
const defaulted = Default(schema, value); | ||
return (0, guard_1.IsArray)(defaulted) | ||
? defaulted.map((value) => Visit(schema.items, references, value)) | ||
: defaulted; | ||
} | ||
// prettier-ignore | ||
function TIntersect(schema, references, value) { | ||
const properties1 = Default(schema, value); | ||
const defaulted = Default(schema, value); | ||
if (!(0, guard_1.IsPlainObject)(value) || (0, guard_1.IsValueType)(value)) | ||
return properties1; | ||
const keys = Types.KeyResolver.ResolveKeys(schema, { includePatterns: false }); | ||
const properties2 = Object.entries(properties1).reduce((acc, [key, value]) => { | ||
return !keys.includes(key) ? { ...acc, [key]: value } : { ...acc, [key]: Default(Types.IndexedAccessor.Resolve(schema, [key]), value) }; | ||
}, {}); | ||
if (!Types.TypeGuard.TTransform(schema.unevaluatedProperties)) | ||
return Default(schema, properties2); | ||
return Object.entries(properties2).reduce((acc, [key, value]) => { | ||
return keys.includes(key) ? { ...acc, [key]: value } : { ...acc, [key]: Default(schema.unevaluatedProperties, value) }; | ||
}, {}); | ||
return defaulted; | ||
const knownKeys = Types.KeyResolver.ResolveKeys(schema, { includePatterns: false }); | ||
const knownProperties = knownKeys.reduce((value, key) => { | ||
return key in defaulted | ||
? { ...value, [key]: Visit(Types.IndexedAccessor.Resolve(schema, [key]), references, value[key]) } | ||
: value; | ||
}, defaulted); | ||
if (!Types.TypeGuard.TTransform(schema.unevaluatedProperties)) { | ||
return Default(schema, knownProperties); | ||
} | ||
const unknownKeys = Object.getOwnPropertyNames(knownProperties); | ||
const unevaluatedProperties = schema.unevaluatedProperties; | ||
return unknownKeys.reduce((value, key) => { | ||
return !knownKeys.includes(key) | ||
? { ...value, [key]: Default(unevaluatedProperties, value[key]) } | ||
: value; | ||
}, knownProperties); | ||
} | ||
function TNot(schema, references, value) { | ||
const value1 = Default(schema, value); | ||
return Default(schema.not, value1); | ||
return Default(schema.not, Default(schema, value)); | ||
} | ||
// prettier-ignore | ||
function TObject(schema, references, value) { | ||
const properties1 = Default(schema, value); | ||
const defaulted = Default(schema, value); | ||
if (!(0, guard_1.IsPlainObject)(value)) | ||
return properties1; | ||
const properties2 = Object.entries(properties1).reduce((acc, [key, value]) => { | ||
return !(key in schema.properties) ? { ...acc, [key]: value } : { ...acc, [key]: Visit(schema.properties[key], references, value) }; | ||
}, {}); | ||
if (!Types.TypeGuard.TSchema(schema.additionalProperties)) | ||
return properties2; | ||
return defaulted; | ||
const knownKeys = Types.KeyResolver.ResolveKeys(schema, { includePatterns: false }); | ||
const knownProperties = knownKeys.reduce((value, key) => { | ||
return key in value | ||
? { ...value, [key]: Visit(schema.properties[key], references, value[key]) } | ||
: value; | ||
}, defaulted); | ||
if (!Types.TypeGuard.TSchema(schema.additionalProperties)) { | ||
return knownProperties; | ||
} | ||
const unknownKeys = Object.getOwnPropertyNames(knownProperties); | ||
const additionalProperties = schema.additionalProperties; | ||
return Object.entries(properties2).reduce((acc, [key, value]) => { | ||
return key in schema.properties ? { ...acc, [key]: value } : { ...acc, [key]: Visit(additionalProperties, references, value) }; | ||
}, {}); | ||
return unknownKeys.reduce((value, key) => { | ||
return !knownKeys.includes(key) | ||
? { ...value, [key]: Default(additionalProperties, value[key]) } | ||
: value; | ||
}, knownProperties); | ||
} | ||
// prettier-ignore | ||
function TRecord(schema, references, value) { | ||
const properties1 = Default(schema, value); | ||
const defaulted = Default(schema, value); | ||
if (!(0, guard_1.IsPlainObject)(value)) | ||
return properties1; | ||
return defaulted; | ||
const pattern = Object.getOwnPropertyNames(schema.patternProperties)[0]; | ||
const property = schema.patternProperties[pattern]; | ||
const regex = new RegExp(pattern); | ||
const properties2 = Object.entries(properties1).reduce((acc, [key, value]) => { | ||
return !regex.test(key) ? { ...acc, [key]: value } : { ...acc, [key]: Visit(property, references, value) }; | ||
}, {}); | ||
if (!Types.TypeGuard.TSchema(schema.additionalProperties)) | ||
return Default(schema, properties2); | ||
const knownKeys = new RegExp(pattern); | ||
const knownProperties = Object.getOwnPropertyNames(value).reduce((value, key) => { | ||
return knownKeys.test(key) | ||
? { ...value, [key]: Visit(schema.patternProperties[pattern], references, value[key]) } | ||
: value; | ||
}, defaulted); | ||
if (!Types.TypeGuard.TSchema(schema.additionalProperties)) { | ||
return Default(schema, knownProperties); | ||
} | ||
const unknownKeys = Object.getOwnPropertyNames(knownProperties); | ||
const additionalProperties = schema.additionalProperties; | ||
return Object.entries(properties2).reduce((acc, [key, value]) => { | ||
return regex.test(key) ? { ...acc, [key]: value } : { ...acc, [key]: Visit(additionalProperties, references, value) }; | ||
}, {}); | ||
return unknownKeys.reduce((value, key) => { | ||
return !knownKeys.test(key) | ||
? { ...value, [key]: Default(additionalProperties, value[key]) } | ||
: value; | ||
}, knownProperties); | ||
} | ||
@@ -457,3 +442,3 @@ function TRef(schema, references, value) { | ||
for (const subschema of schema.anyOf) { | ||
if (!checkFunction(subschema, references, value)) | ||
if (!(0, check_1.Check)(subschema, references, value)) | ||
continue; | ||
@@ -466,3 +451,3 @@ const value1 = Visit(subschema, references, value); | ||
const value1 = Visit(subschema, references, value); | ||
if (!checkFunction(schema, references, value1)) | ||
if (!(0, check_1.Check)(schema, references, value1)) | ||
continue; | ||
@@ -477,5 +462,2 @@ return Default(schema, value1); | ||
switch (schema[Types.Kind]) { | ||
// ------------------------------------------------------ | ||
// Structural | ||
// ------------------------------------------------------ | ||
case 'Array': | ||
@@ -499,36 +481,7 @@ return TArray(schema_, references_, value); | ||
return TUnion(schema_, references_, value); | ||
// ------------------------------------------------------ | ||
// Apply | ||
// ------------------------------------------------------ | ||
case 'Any': | ||
case 'AsyncIterator': | ||
case 'BigInt': | ||
case 'Boolean': | ||
case 'Constructor': | ||
case 'Date': | ||
case 'Function': | ||
case 'Integer': | ||
case 'Iterator': | ||
case 'Literal': | ||
case 'Never': | ||
case 'Null': | ||
case 'Number': | ||
case 'Promise': | ||
case 'String': | ||
case 'Symbol': | ||
case 'TemplateLiteral': | ||
case 'Undefined': | ||
case 'Uint8Array': | ||
case 'Unknown': | ||
case 'Void': | ||
return Default(schema_, value); | ||
default: | ||
if (!Types.TypeRegistry.Has(schema_[Types.Kind])) | ||
throw new TransformUnknownTypeError(schema_); | ||
return Default(schema_, value); | ||
} | ||
} | ||
let checkFunction = () => false; | ||
function Encode(schema, references, value, check) { | ||
checkFunction = check; | ||
function Encode(schema, references, value) { | ||
return Visit(schema, references, value); | ||
@@ -535,0 +488,0 @@ } |
@@ -26,9 +26,9 @@ import * as ValueErrors from '../errors/index'; | ||
/** Decodes a value or throws if error */ | ||
function Decode<T extends Types.TSchema, D = Types.StaticDecode<T>>(schema: T, references: Types.TSchema[], value: unknown): D; | ||
function Decode<T extends Types.TSchema, R = Types.StaticDecode<T>>(schema: T, references: Types.TSchema[], value: unknown): R; | ||
/** Decodes a value or throws if error */ | ||
function Decode<T extends Types.TSchema, D = Types.StaticDecode<T>>(schema: T, value: unknown): D; | ||
function Decode<T extends Types.TSchema, R = Types.StaticDecode<T>>(schema: T, value: unknown): R; | ||
/** Encodes a value or throws if error */ | ||
function Encode<T extends Types.TSchema, E = Types.StaticEncode<T>>(schema: T, references: Types.TSchema[], value: unknown): E; | ||
function Encode<T extends Types.TSchema, R = Types.StaticEncode<T>>(schema: T, references: Types.TSchema[], value: unknown): R; | ||
/** Encodes a value or throws if error */ | ||
function Encode<T extends Types.TSchema, E = Types.StaticEncode<T>>(schema: T, value: unknown): E; | ||
function Encode<T extends Types.TSchema, R = Types.StaticEncode<T>>(schema: T, value: unknown): R; | ||
/** Returns an iterator for each error in this value. */ | ||
@@ -35,0 +35,0 @@ function Errors<T extends Types.TSchema>(schema: T, references: Types.TSchema[], value: unknown): ValueErrors.ValueErrorIterator; |
@@ -75,3 +75,3 @@ "use strict"; | ||
throw new ValueTransform.TransformDecodeCheckError(schema, value, Errors(schema, references, value).First()); | ||
return ValueTransform.DecodeTransform.Decode(schema, references, value, ValueCheck.Check); | ||
return ValueTransform.DecodeTransform.Decode(schema, references, value); | ||
} | ||
@@ -82,3 +82,3 @@ Value.Decode = Decode; | ||
const [schema, references, value] = args.length === 3 ? [args[0], args[1], args[2]] : [args[0], [], args[1]]; | ||
const encoded = ValueTransform.EncodeTransform.Encode(schema, references, value, ValueCheck.Check); | ||
const encoded = ValueTransform.EncodeTransform.Encode(schema, references, value); | ||
if (!Check(schema, references, encoded)) | ||
@@ -85,0 +85,0 @@ throw new ValueTransform.TransformEncodeCheckError(schema, value, Errors(schema, references, value).First()); |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
535697
8519