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 81bae60 commit 5cea70e
Show file tree
Hide file tree
Showing 40 changed files with 502 additions and 160 deletions.
2 changes: 1 addition & 1 deletion benchmark/compression/module/typebox-value-cast.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { Cast } from '@sinclair/typebox/value/cast'
import { Cast } from '@sinclair/typebox/value/cast/cast'

console.log(Cast)
2 changes: 1 addition & 1 deletion benchmark/compression/module/typebox-value-check.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { Check } from '@sinclair/typebox/value/check'
import { Check } from '@sinclair/typebox/value/check/check'

console.log(Check)
2 changes: 1 addition & 1 deletion benchmark/compression/module/typebox-value-convert.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { Convert } from '@sinclair/typebox/value/convert'
import { Convert } from '@sinclair/typebox/value/convert/convert'

console.log(Convert)
2 changes: 1 addition & 1 deletion benchmark/compression/module/typebox-value-create.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { Create } from '@sinclair/typebox/value/create'
import { Create } from '@sinclair/typebox/value/create/create'

console.log(Create)
46 changes: 23 additions & 23 deletions examples/index.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import { TypeSystem } from '@sinclair/typebox/system'
import { TypeCompiler } from '@sinclair/typebox/compiler'
import { Value, ValuePointer } from '@sinclair/typebox/value'
import { Type, TypeGuard, Kind, Static, TSchema, TTuple, TIntersect, TUnion, TPromise, TAsyncIterator, TIterator, TArray, TConstructor, TFunction, Clone, TRef, TObject, TProperties } from '@sinclair/typebox'
import { CloneType } from '@sinclair/typebox'
import { ValueGuard } from '@sinclair/typebox'
import Type from '@sinclair/typebox'
import { Value } from '@sinclair/typebox/value'

const T = Type.Object(
{
x: Type.Number(),
y: Type.String(),
},
{ $id: 'T' },
)
const A = Type.Transform(Type.String())
.Decode((value: string): Date => new Date(value))
.Encode((value: Date): string => value.toISOString())

const R1 = Type.Ref<typeof T>('T', { $id: 'R1' })
const R2 = Type.Ref<typeof R1>('R1', { $id: 'R2' })
const R3 = Type.Ref<typeof R2>('R2', { $id: 'R3' })
const R4 = Type.Ref<typeof R3>('R3', { $id: 'R4' })
const B = Type.Union([Type.Object({ date: A }), Type.Number()])

const S = Type.Object({
a: Type.Ref<typeof R4>('R4'),
b: Type.Ref<typeof R4>('R4'),
})
const C = Type.Transform(B)
.Decode((value) => {
console.log(value)
if (typeof value === 'object') {
// date is supposed to be a Date according to type inference, but it's a string when it's logged.
console.log('union', typeof value.date) // string
}
return value
})
.Encode((o) => o)

const X = Type.Deref(S, [T, R1, R2, R3, R4])
const D = Type.Transform(Type.Object({ date: A }))
.Decode((o) => {
console.log('simple', typeof o.date) // object
return o
})
.Encode((o) => o)

console.log(X)
const R1 = Value.Decode(C, { date: new Date().toISOString() })
26 changes: 13 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@
"./errors": "./errors/index.js",
"./type": "./type/index.js",
"./system": "./system/index.js",
"./value/cast": "./value/cast.js",
"./value/check": "./value/check.js",
"./value/clone": "./value/clone.js",
"./value/convert": "./value/convert.js",
"./value/create": "./value/create.js",
"./value/delta": "./value/delta.js",
"./value/deref": "./value/deref.js",
"./value/equal": "./value/equal.js",
"./value/guard": "./value/guard.js",
"./value/hash": "./value/hash.js",
"./value/mutate": "./value/mutate.js",
"./value/pointer": "./value/pointer.js",
"./value/transform": "./value/transform.js",
"./value/cast": "./value/cast/index.js",
"./value/check": "./value/check/index.js",
"./value/clone": "./value/clone/index.js",
"./value/convert": "./value/convert/index.js",
"./value/create": "./value/create/index.js",
"./value/delta": "./value/delta/index.js",
"./value/deref": "./value/deref/index.js",
"./value/equal": "./value/equal/index.js",
"./value/guard": "./value/guard/index.js",
"./value/hash": "./value/hash/index.js",
"./value/mutate": "./value/mutate/index.js",
"./value/pointer": "./value/pointer/index.js",
"./value/transform": "./value/transform/index.js",
"./value": "./value/index.js",
".": "./index.js"
},
Expand Down
8 changes: 4 additions & 4 deletions src/compiler/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

import { EncodeTransform, DecodeTransform, HasTransform, TransformDecodeCheckError, TransformEncodeCheckError } from '../value/transform'
import { IsArray, IsString, IsNumber, IsBigInt } from '../value/guard'
import { EncodeTransform, DecodeTransform, HasTransform, TransformDecodeCheckError, TransformEncodeCheckError } from '../value/transform/transform'
import { IsArray, IsString, IsNumber, IsBigInt } from '../value/guard/guard'
import { Errors, ValueErrorIterator } from '../errors/errors'
import { TypeSystemPolicy } from '../system/index'
import { Deref } from '../value/deref'
import { Hash } from '../value/hash'
import { Deref } from '../value/deref/deref'
import { Hash } from '../value/hash/hash'
import * as Types from '../type/index'

// -------------------------------------------------------------------
Expand Down
6 changes: 3 additions & 3 deletions src/errors/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

import { IsArray, IsUint8Array, IsDate, IsPromise, IsFunction, IsAsyncIterator, IsIterator, IsBoolean, IsNumber, IsBigInt, IsString, IsSymbol, IsInteger, IsNull, IsUndefined } from '../value/guard'
import { IsArray, IsUint8Array, IsDate, IsPromise, IsFunction, IsAsyncIterator, IsIterator, IsBoolean, IsNumber, IsBigInt, IsString, IsSymbol, IsInteger, IsNull, IsUndefined } from '../value/guard/guard'
import { TypeSystemPolicy, TypeSystemErrorFunction } from '../system/system'
import { Deref } from '../value/deref'
import { Hash } from '../value/hash'
import { Deref } from '../value/deref/deref'
import { Hash } from '../value/hash/hash'
import * as Types from '../type/index'

// --------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion src/system/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

import { IsObject, IsArray, IsNumber, IsUndefined } from '../value/guard'
import { IsObject, IsArray, IsNumber, IsUndefined } from '../value/guard/guard'
import { ValueErrorType } from '../errors/errors'
import * as Types from '../type/index'

Expand Down
2 changes: 1 addition & 1 deletion src/type/builder/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export class JsonTypeBuilder {
public Composite<T extends TObject[]>(objects: [...T], options?: ObjectOptions): TComposite<T> {
return Composite(objects, options)
}
/** [Json] Dereferences a schema to its target type */
/** `[Json]` Creates a dereferenced type */
public Deref<T extends TSchema>(schema: T, references: TSchema[]): TDeref<T> {
return Deref(schema, references)
}
Expand Down
9 changes: 5 additions & 4 deletions src/type/deref/deref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import type { TFunction } from '../function/index'
import type { TRef } from '../ref/index'
import type { TObject, TProperties } from '../object/index'
import { CloneType } from '../clone/type'
import { Discard } from '../discard/index'
import { IsUndefined } from '../guard/value'

import {
Expand Down Expand Up @@ -145,7 +146,8 @@ function FromIterator(schema: TIterator, references: TSchema[]) {
function FromRef(schema: TRef, references: TSchema[]) {
const target = references.find(remote => remote.$id === schema.$ref)
if(target === undefined) throw Error(`Unable to dereference schema with $id ${schema.$ref}`)
return Deref(target, references)
const discard = Discard(target, ['$id']) as TSchema
return Deref(discard, references)
}
// prettier-ignore
export type DerefResolve<T extends TSchema> =
Expand Down Expand Up @@ -183,8 +185,7 @@ export function DerefResolve<T extends TSchema>(schema: T, references: TSchema[]
// ------------------------------------------------------------------
export type TDeref<T extends TSchema> = DerefResolve<T>

/** [Json] Dereferences a schema to its target type */
// prettier-ignore
export function Deref<T extends TSchema>(schema: T, references: TSchema[]): TDeref<T> {
/** `[Json]` Creates a dereferenced type */
export function Deref<T extends TSchema>(schema: T, references: TSchema[]): TDeref<T> {
return DerefResolve(schema, references)
}
14 changes: 7 additions & 7 deletions src/value/cast.ts → src/value/cast/cast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

import { IsPlainObject, IsArray, IsString, IsNumber, IsNull } from './guard'
import { TypeRegistry } from '../type/registry/index'
import * as Types from '../type/index'
import { Create } from './create'
import { Check } from './check'
import { Clone } from './clone'
import { Deref } from './deref'
import { IsPlainObject, IsArray, IsString, IsNumber, IsNull } from '../guard/guard'
import { TypeRegistry } from '../../type/registry/index'
import * as Types from '../../type/index'
import { Create } from '../create/create'
import { Check } from '../check/check'
import { Clone } from '../clone/clone'
import { Deref } from '../deref/deref'

// --------------------------------------------------------------------------
// Errors
Expand Down
29 changes: 29 additions & 0 deletions src/value/cast/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*--------------------------------------------------------------------------
@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.
---------------------------------------------------------------------------*/

export * from './cast'
10 changes: 5 additions & 5 deletions src/value/check.ts → src/value/check/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

import { IsArray, IsUint8Array, IsDate, IsPromise, IsFunction, IsAsyncIterator, IsIterator, IsBoolean, IsNumber, IsBigInt, IsString, IsSymbol, IsInteger, IsNull, IsUndefined } from './guard'
import { TypeSystemPolicy } from '../system/index'
import { Deref } from './deref'
import { Hash } from './hash'
import * as Types from '../type/index'
import { IsArray, IsUint8Array, IsDate, IsPromise, IsFunction, IsAsyncIterator, IsIterator, IsBoolean, IsNumber, IsBigInt, IsString, IsSymbol, IsInteger, IsNull, IsUndefined } from '../guard/guard'
import { TypeSystemPolicy } from '../../system/index'
import { Deref } from '../deref/deref'
import { Hash } from '../hash/hash'
import * as Types from '../../type/index'

// --------------------------------------------------------------------------
// Errors
Expand Down
29 changes: 29 additions & 0 deletions src/value/check/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*--------------------------------------------------------------------------
@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.
---------------------------------------------------------------------------*/

export * from './check'
4 changes: 2 additions & 2 deletions src/value/clone.ts → src/value/clone/clone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

import { IsArray, IsDate, IsPlainObject, IsTypedArray, IsValueType } from './guard'
import type { ObjectType, ArrayType, TypedArrayType, ValueType } from './guard'
import { IsArray, IsDate, IsPlainObject, IsTypedArray, IsValueType } from '../guard/guard'
import type { ObjectType, ArrayType, TypedArrayType, ValueType } from '../guard/guard'

// --------------------------------------------------------------------------
// Clonable
Expand Down
29 changes: 29 additions & 0 deletions src/value/clone/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*--------------------------------------------------------------------------
@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.
---------------------------------------------------------------------------*/

export * from './clone'
10 changes: 5 additions & 5 deletions src/value/convert.ts → src/value/convert/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

import { IsArray, IsObject, IsDate, IsUndefined, IsString, IsNumber, IsBoolean, IsBigInt, IsSymbol } from './guard'
import { Clone } from './clone'
import { Check } from './check'
import { Deref } from './deref'
import * as Types from '../type/index'
import { IsArray, IsObject, IsDate, IsUndefined, IsString, IsNumber, IsBoolean, IsBigInt, IsSymbol } from '../guard/guard'
import { Clone } from '../clone/clone'
import { Check } from '../check/check'
import { Deref } from '../deref/deref'
import * as Types from '../../type/index'

// --------------------------------------------------------------------------
// Errors
Expand Down
29 changes: 29 additions & 0 deletions src/value/convert/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*--------------------------------------------------------------------------
@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.
---------------------------------------------------------------------------*/

export * from './convert'
8 changes: 4 additions & 4 deletions src/value/create.ts → src/value/create/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

import { HasPropertyKey, IsString } from './guard'
import { Check } from './check'
import { Deref } from './deref'
import * as Types from '../type/index'
import { HasPropertyKey, IsString } from '../guard/guard'
import { Check } from '../check/check'
import { Deref } from '../deref/deref'
import * as Types from '../../type/index'

// --------------------------------------------------------------------------
// Errors
Expand Down
Loading

0 comments on commit 5cea70e

Please sign in to comment.