@sinclair/typebox
Advanced tools
Comparing version 0.31.27 to 0.31.28
{ | ||
"name": "@sinclair/typebox", | ||
"version": "0.31.27", | ||
"version": "0.31.28", | ||
"description": "JSONSchema Type Builder with Static Type Resolution for TypeScript", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
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 { | ||
@@ -9,0 +4,0 @@ readonly schema: Types.TSchema; |
@@ -30,3 +30,3 @@ "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"); | ||
@@ -39,9 +39,2 @@ const deref_1 = require("./deref"); | ||
// ------------------------------------------------------------------------- | ||
class TransformUnknownTypeError extends Types.TypeBoxError { | ||
constructor(schema) { | ||
super(`Unknown type`); | ||
this.schema = schema; | ||
} | ||
} | ||
exports.TransformUnknownTypeError = TransformUnknownTypeError; | ||
class TransformDecodeCheckError extends Types.TypeBoxError { | ||
@@ -81,5 +74,5 @@ constructor(schema, value, error) { | ||
exports.TransformEncodeError = TransformEncodeError; | ||
// ------------------------------------------------------------------------- | ||
// ------------------------------------------------------------------ | ||
// HasTransform | ||
// ------------------------------------------------------------------------- | ||
// ------------------------------------------------------------------ | ||
/** Recursively checks a schema for transform codecs */ | ||
@@ -145,5 +138,2 @@ var HasTransform; | ||
switch (schema[Types.Kind]) { | ||
// ------------------------------------------------------ | ||
// Structural | ||
// ------------------------------------------------------ | ||
case 'Array': | ||
@@ -177,25 +167,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 +180,5 @@ } | ||
})(HasTransform || (exports.HasTransform = HasTransform = {})); | ||
// ------------------------------------------------------------------------- | ||
// ------------------------------------------------------------------ | ||
// DecodeTransform | ||
// ------------------------------------------------------------------------- | ||
// ------------------------------------------------------------------ | ||
/** Decodes a value using transform decoders if available. Does not ensure correct results. */ | ||
@@ -227,38 +195,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 +254,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 (!(0, check_1.Check)(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 +300,2 @@ function Visit(schema, references, value) { | ||
switch (schema[Types.Kind]) { | ||
// ------------------------------------------------------ | ||
// Structural | ||
// ------------------------------------------------------ | ||
case 'Array': | ||
@@ -333,29 +321,3 @@ 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); | ||
@@ -369,5 +331,5 @@ } | ||
})(DecodeTransform || (exports.DecodeTransform = DecodeTransform = {})); | ||
// ------------------------------------------------------------------------- | ||
// ------------------------------------------------------------------ | ||
// DecodeTransform | ||
// ------------------------------------------------------------------------- | ||
// ------------------------------------------------------------------ | ||
/** Encodes a value using transform encoders if available. Does not ensure correct results. */ | ||
@@ -384,54 +346,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); | ||
} | ||
@@ -473,5 +459,2 @@ function TRef(schema, references, value) { | ||
switch (schema[Types.Kind]) { | ||
// ------------------------------------------------------ | ||
// Structural | ||
// ------------------------------------------------------ | ||
case 'Array': | ||
@@ -495,30 +478,3 @@ 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); | ||
@@ -525,0 +481,0 @@ } |
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
4
535697
8519