-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aeb0459
commit 9bfc72b
Showing
9 changed files
with
252 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +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 } from '@sinclair/typebox' | ||
|
||
// ----------------------------------------------------------- | ||
// Create: Type | ||
// ----------------------------------------------------------- | ||
|
||
const T = Type.Object({ | ||
x: Type.Number(), | ||
y: Type.Number(), | ||
z: Type.Number(), | ||
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' | ||
|
||
const T = Type.Object( | ||
{ | ||
x: Type.Number(), | ||
y: Type.String(), | ||
}, | ||
{ $id: 'T' }, | ||
) | ||
|
||
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 S = Type.Object({ | ||
a: Type.Ref<typeof R4>('R4'), | ||
b: Type.Ref<typeof R4>('R4'), | ||
}) | ||
|
||
type T = Static<typeof T> | ||
|
||
console.log(T) | ||
|
||
// ----------------------------------------------------------- | ||
// Create: Value | ||
// ----------------------------------------------------------- | ||
|
||
const V = Value.Create(T) | ||
|
||
console.log(V) | ||
|
||
// ----------------------------------------------------------- | ||
// Compile: Type | ||
// ----------------------------------------------------------- | ||
|
||
const C = TypeCompiler.Compile(T) | ||
|
||
console.log(C.Code()) | ||
|
||
// ----------------------------------------------------------- | ||
// Check: Value | ||
// ----------------------------------------------------------- | ||
const X = Type.Deref(S, [T, R1, R2, R3, R4]) | ||
|
||
console.log(C.Check(V)) | ||
console.log(X) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
/*-------------------------------------------------------------------------- | ||
@sinclair/typebox | ||
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 type { TSchema } from '../schema/index' | ||
import type { Evaluate } from '../helpers/index' | ||
import type { TTuple } from '../tuple/index' | ||
import type { TIntersect } from '../intersect/index' | ||
import type { TUnion } from '../union/index' | ||
import type { TPromise } from '../promise/index' | ||
import type { TAsyncIterator } from '../async-iterator/index' | ||
import type { TIterator } from '../iterator/index' | ||
import type { TArray } from '../array/index' | ||
import type { TConstructor } from '../constructor/index' | ||
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 { IsUndefined } from '../guard/value' | ||
|
||
import { | ||
TConstructor as IsConstructorType, | ||
TFunction as IsFunctionType, | ||
TIntersect as IsIntersectType, | ||
TUnion as IsUnionType, | ||
TTuple as IsTupleType, | ||
TArray as IsArrayType, | ||
TObject as IsObjectType, | ||
TPromise as IsPromiseType, | ||
TAsyncIterator as IsAsyncIteratorType, | ||
TIterator as IsIteratorType, | ||
TRef as IsRefType, | ||
} from '../guard/type' | ||
|
||
// ------------------------------------------------------------------ | ||
// DerefResolve | ||
// ------------------------------------------------------------------ | ||
// prettier-ignore | ||
export type FromRest<T extends TSchema[]> = ( | ||
T extends [infer L extends TSchema, ...infer R extends TSchema[]] | ||
? [DerefResolve<L>, ...FromRest<R>] | ||
: [] | ||
) | ||
function FromRest(schema: TSchema[], references: TSchema[]) { | ||
return schema.map((schema) => Deref(schema, references)) | ||
} | ||
// prettier-ignore | ||
type FromProperties<T extends TProperties> = Evaluate<{ | ||
[K in keyof T]: DerefResolve<T[K]> | ||
}> | ||
// prettier-ignore | ||
function FromProperties(properties: TProperties, references: TSchema[]) { | ||
return globalThis.Object.getOwnPropertyNames(properties).reduce((acc, key) => { | ||
return {...acc, [key]: Deref(properties[key], references) } | ||
}, {} as TProperties) | ||
} | ||
// prettier-ignore | ||
function FromConstructor(schema: TConstructor, references: TSchema[]) { | ||
const clone = CloneType(schema) | ||
clone.parameters = FromRest(clone.parameters, references) | ||
clone.returns = Deref(clone.returns, references) | ||
return clone | ||
} | ||
// prettier-ignore | ||
function FromFunction(schema: TFunction, references: TSchema[]) { | ||
const clone = CloneType(schema) | ||
clone.parameters = FromRest(clone.parameters, references) | ||
clone.returns = Deref(clone.returns, references) | ||
return clone | ||
} | ||
// prettier-ignore | ||
function FromIntersect(schema: TIntersect, references: TSchema[]) { | ||
const clone = CloneType(schema) | ||
clone.allOf = FromRest(clone.allOf, references) | ||
return clone | ||
} | ||
// prettier-ignore | ||
function FromUnion(schema: TUnion, references: TSchema[]) { | ||
const clone = CloneType(schema) | ||
clone.anyOf = FromRest(clone.anyOf, references) | ||
return clone | ||
} | ||
// prettier-ignore | ||
function FromTuple(schema: TTuple, references: TSchema[]) { | ||
const clone = CloneType(schema) | ||
if(IsUndefined(clone.items)) return clone | ||
clone.items = FromRest(clone.items, references) | ||
return clone | ||
} | ||
// prettier-ignore | ||
function FromArray(schema: TArray, references: TSchema[]) { | ||
const clone = CloneType(schema) | ||
clone.items = Deref(clone.items, references) | ||
return clone | ||
} | ||
// prettier-ignore | ||
function FromObject(schema: TObject, references: TSchema[]) { | ||
const clone = CloneType(schema) | ||
clone.properties = FromProperties(clone.properties, references) | ||
return clone | ||
} | ||
// prettier-ignore | ||
function FromPromise(schema: TPromise, references: TSchema[]) { | ||
const clone = CloneType(schema) | ||
clone.item = Deref(clone.item, references) | ||
return clone | ||
} | ||
// prettier-ignore | ||
function FromAsyncIterator(schema: TAsyncIterator, references: TSchema[]) { | ||
const clone = CloneType(schema) | ||
clone.items = Deref(clone.items, references) | ||
return clone | ||
} | ||
// prettier-ignore | ||
function FromIterator(schema: TIterator, references: TSchema[]) { | ||
const clone = CloneType(schema) | ||
clone.items = Deref(clone.items, references) | ||
return clone | ||
} | ||
// prettier-ignore | ||
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) | ||
} | ||
// prettier-ignore | ||
export type DerefResolve<T extends TSchema> = | ||
T extends TConstructor<infer S extends TSchema[], infer R extends TSchema> ? TConstructor<FromRest<S>, DerefResolve<R>> : | ||
T extends TFunction<infer S extends TSchema[], infer R extends TSchema> ? TFunction<FromRest<S>, DerefResolve<R>> : | ||
T extends TIntersect<infer S extends TSchema[]> ? TIntersect<FromRest<S>> : | ||
T extends TUnion<infer S extends TSchema[]> ? TUnion<FromRest<S>> : | ||
T extends TTuple<infer S extends TSchema[]> ? TTuple<FromRest<S>> : | ||
T extends TObject<infer S extends TProperties> ? TObject<FromProperties<S>> : | ||
T extends TArray<infer S extends TSchema> ? TArray<DerefResolve<S>> : | ||
T extends TPromise<infer S extends TSchema> ? TPromise<DerefResolve<S>> : | ||
T extends TAsyncIterator<infer S extends TSchema> ? TAsyncIterator<DerefResolve<S>> : | ||
T extends TIterator<infer S extends TSchema> ? TIterator<DerefResolve<S>> : | ||
T extends TRef<infer S extends TSchema> ? DerefResolve<S> : | ||
T | ||
// prettier-ignore | ||
export function DerefResolve<T extends TSchema>(schema: T, references: TSchema[]): TDeref<T> { | ||
return ( | ||
IsConstructorType(schema) ? FromConstructor(schema, references) : | ||
IsFunctionType(schema) ? FromFunction(schema, references) : | ||
IsIntersectType(schema) ? FromIntersect(schema, references) : | ||
IsUnionType(schema) ? FromUnion(schema, references) : | ||
IsTupleType(schema) ? FromTuple(schema, references) : | ||
IsArrayType(schema) ? FromArray(schema, references) : | ||
IsObjectType(schema) ? FromObject(schema, references) : | ||
IsPromiseType(schema) ? FromPromise(schema, references) : | ||
IsAsyncIteratorType(schema) ? FromAsyncIterator(schema, references) : | ||
IsIteratorType(schema) ? FromIterator(schema, references) : | ||
IsRefType(schema) ? FromRef(schema, references) : | ||
schema | ||
) as TDeref<T> | ||
} | ||
// ------------------------------------------------------------------ | ||
// TDeref | ||
// ------------------------------------------------------------------ | ||
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> { | ||
return DerefResolve(schema, references) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/*-------------------------------------------------------------------------- | ||
@sinclair/typebox | ||
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 './deref' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters