Skip to content

Commit

Permalink
Revision 0.32.0
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed Nov 29, 2023
1 parent 5cea70e commit 9094944
Show file tree
Hide file tree
Showing 7 changed files with 584 additions and 446 deletions.
8 changes: 4 additions & 4 deletions src/compiler/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

import { EncodeTransform, DecodeTransform, HasTransform, TransformDecodeCheckError, TransformEncodeCheckError } from '../value/transform/transform'
import { Encode as TransformEncode, Decode as TransformDecode, Has as HasTransform, TransformDecodeCheckError, TransformEncodeCheckError } from '../value/transform/index'
import { IsArray, IsString, IsNumber, IsBigInt } from '../value/guard/guard'
import { Errors, ValueErrorIterator } from '../errors/errors'
import { TypeSystemPolicy } from '../system/index'
Expand All @@ -44,7 +44,7 @@ export type CheckFunction = (value: unknown) => boolean
export class TypeCheck<T extends Types.TSchema> {
private readonly hasTransform: boolean
constructor(private readonly schema: T, private readonly references: Types.TSchema[], private readonly checkFunc: CheckFunction, private readonly code: string) {
this.hasTransform = HasTransform.Has(schema, references)
this.hasTransform = HasTransform(schema, references)
}
/** Returns the generated assertion code used to validate this type. */
public Code(): string {
Expand All @@ -61,11 +61,11 @@ export class TypeCheck<T extends Types.TSchema> {
/** Decodes a value or throws if error */
public Decode(value: unknown): Types.StaticDecode<T> {
if (!this.checkFunc(value)) throw new TransformDecodeCheckError(this.schema, value, this.Errors(value).First()!)
return this.hasTransform ? DecodeTransform.Decode(this.schema, this.references, value) : value
return this.hasTransform ? TransformDecode(this.schema, this.references, value) : value
}
/** Encodes a value or throws if error */
public Encode(value: unknown): Types.StaticEncode<T> {
const encoded = this.hasTransform ? EncodeTransform.Encode(this.schema, this.references, value) : value
const encoded = this.hasTransform ? TransformEncode(this.schema, this.references, value) : value
if (!this.checkFunc(encoded)) throw new TransformEncodeCheckError(this.schema, value, this.Errors(value).First()!)
return encoded
}
Expand Down
204 changes: 204 additions & 0 deletions src/value/transform/decode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
/*--------------------------------------------------------------------------
@sinclair/typebox/value
The MIT License (MIT)
Copyright (c) 2017-2023 Haydn Paterson (sinclair) <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
---------------------------------------------------------------------------*/

import { TTransform as IsTransformType, TSchema as IsSchemaType } from '../../type/guard/type'
import { Kind, TransformKind } from '../../type/symbols/index'
import { IsPlainObject, IsArray, IsValueType } from '../guard/index'
import { ValueError } from '../../errors/index'
import { KeyOfStringResolve } from '../../type/keyof/index'
import { IndexedTypeResolve } from '../../type/indexed/index'
import { Deref } from '../deref/index'
import { Check } from '../check/index'

import type { TSchema } from '../../type/schema/index'
import type { TArray } from '../../type/array/index'
import type { TIntersect } from '../../type/intersect/index'
import type { TNot } from '../../type/not/index'
import type { TObject } from '../../type/object/index'
import type { TRecord } from '../../type/record/index'
import type { TRef } from '../../type/ref/index'
import type { TThis } from '../../type/recursive/index'
import type { TTuple } from '../../type/tuple/index'
import type { TUnion } from '../../type/union/index'

// ------------------------------------------------------------------
// Errors
// ------------------------------------------------------------------
// thrown externally
export class TransformDecodeCheckError extends Error {
constructor(public readonly schema: TSchema, public readonly value: unknown, public readonly error: ValueError) {
super(`Unable to decode due to invalid value`)
}
}
export class TransformDecodeError extends Error {
constructor(public readonly schema: TSchema, public readonly value: unknown, error: any) {
super(`${error instanceof Error ? error.message : 'Unknown error'}`)
}
}
// ------------------------------------------------------------------
// Decode
// ------------------------------------------------------------------
// prettier-ignore
function Default(schema: TSchema, value: any) {
try {
return IsTransformType(schema) ? schema[TransformKind].Decode(value) : value
} catch (error) {
throw new TransformDecodeError(schema, value, error)
}
}
// prettier-ignore
function TArray(schema: TArray, references: TSchema[], value: any): any {
return (IsArray(value))
? Default(schema, value.map((value: any) => Visit(schema.items, references, value)))
: Default(schema, value)
}
// prettier-ignore
function TIntersect(schema: TIntersect, references: TSchema[], value: any) {
if (!IsPlainObject(value) || IsValueType(value)) return Default(schema, value)
const knownKeys = KeyOfStringResolve(schema) as string[]
const knownProperties = knownKeys.reduce((value, key) => {
return (key in value)
? { ...value, [key]: Visit(IndexedTypeResolve(schema, [key]), references, value[key]) }
: value
}, value)
if (!IsTransformType(schema.unevaluatedProperties)) {
return Default(schema, knownProperties)
}
const unknownKeys = Object.getOwnPropertyNames(knownProperties)
const unevaluatedProperties = schema.unevaluatedProperties as TSchema
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: TNot, references: TSchema[], value: any) {
return Default(schema, Visit(schema.not, references, value))
}
// prettier-ignore
function TObject(schema: TObject, references: TSchema[], value: any) {
if (!IsPlainObject(value)) return Default(schema, value)
const knownKeys = KeyOfStringResolve(schema)
const knownProperties = knownKeys.reduce((value, key) => {
return (key in value)
? { ...value, [key]: Visit(schema.properties[key], references, value[key]) }
: value
}, value)
if (!IsSchemaType(schema.additionalProperties)) {
return Default(schema, knownProperties)
}
const unknownKeys = Object.getOwnPropertyNames(knownProperties)
const additionalProperties = schema.additionalProperties as TSchema
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: TRecord<any, any>, references: TSchema[], value: any) {
if (!IsPlainObject(value)) return Default(schema, value)
const pattern = Object.getOwnPropertyNames(schema.patternProperties)[0]
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 (!IsSchemaType(schema.additionalProperties)) {
return Default(schema, knownProperties)
}
const unknownKeys = Object.getOwnPropertyNames(knownProperties)
const additionalProperties = schema.additionalProperties as TSchema
const unknownProperties = unknownKeys.reduce((value, key) => {
return !knownKeys.test(key)
? { ...value, [key]: Default(additionalProperties, value[key]) }
: value
}, knownProperties)
return Default(schema, unknownProperties)
}
// prettier-ignore
function TRef(schema: TRef<any>, references: TSchema[], value: any) {
const target = Deref(schema, references)
return Default(schema, Visit(target, references, value))
}
// prettier-ignore
function TThis(schema: TThis, references: TSchema[], value: any) {
const target = Deref(schema, references)
return Default(schema, Visit(target, references, value))
}
// prettier-ignore
function TTuple(schema: TTuple, references: TSchema[], value: any) {
return (IsArray(value) && IsArray(schema.items))
? Default(schema, schema.items.map((schema, index) => Visit(schema, references, value[index])))
: Default(schema, value)
}
// prettier-ignore
function TUnion(schema: TUnion, references: TSchema[], value: any) {
const defaulted = Default(schema, value)
for (const subschema of schema.anyOf) {
if (!Check(subschema, references, defaulted)) continue
return Visit(subschema, references, defaulted)
}
return defaulted
}
// prettier-ignore
function Visit(schema: TSchema, references: TSchema[], value: any): any {
const references_ = typeof schema.$id === 'string' ? [...references, schema] : references
const schema_ = schema as any
switch (schema[Kind]) {
case 'Array':
return TArray(schema_, references_, value)
case 'Intersect':
return TIntersect(schema_, references_, value)
case 'Not':
return TNot(schema_, references_, value)
case 'Object':
return TObject(schema_, references_, value)
case 'Record':
return TRecord(schema_, references_, value)
case 'Ref':
return TRef(schema_, references_, value)
case 'Symbol':
return Default(schema_, value)
case 'This':
return TThis(schema_, references_, value)
case 'Tuple':
return TTuple(schema_, references_, value)
case 'Union':
return TUnion(schema_, references_, value)
default:
return Default(schema_, value)
}
}
// prettier-ignore
export function Decode(schema: TSchema, references: TSchema[], value: unknown): unknown {
return Visit(schema, references, value)
}
Loading

0 comments on commit 9094944

Please sign in to comment.