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 28, 2023
1 parent aeb0459 commit 9bfc72b
Show file tree
Hide file tree
Showing 9 changed files with 252 additions and 35 deletions.
56 changes: 22 additions & 34 deletions examples/index.ts
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)
1 change: 1 addition & 0 deletions hammer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { readFileSync } from 'fs'
// Clean
// -------------------------------------------------------------------------------
export async function clean() {
await folder('node_modules/typebox').delete()
await folder('target').delete()
}
// -------------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export { Const, type TConst } from './type/const/index'
export { Constructor, type TConstructor } from './type/constructor/index'
export { ConstructorParameters, type TConstructorParameters } from './type/constructor-parameters/index'
export { Date, type TDate, type DateOptions } from './type/date/index'
export { Deref, type TDeref } from './type/deref/index'
export { Enum, type TEnum } from './type/enum/index'
export { Exclude, type TExclude } from './type/exclude/index'
export { Extends, type TExtends } from './type/extends/index'
Expand Down
5 changes: 5 additions & 0 deletions src/type/builder/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { Any, type TAny } from '../any/index'
import { Array, type TArray, type ArrayOptions } from '../array/index'
import { Boolean, type TBoolean } from '../boolean/index'
import { Composite, type TComposite } from '../composite/index'
import { Deref, type TDeref } from '../deref/index'
import { Enum, type TEnum, type TEnumKey, type TEnumValue } from '../enum/index'
import { Exclude, type TExclude } from '../exclude/index'
import { Extends, type TExtends } from '../extends/index'
Expand Down Expand Up @@ -113,6 +114,10 @@ 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 */
public Deref<T extends TSchema>(schema: T, references: TSchema[]): TDeref<T> {
return Deref(schema, references)
}
/** `[Json]` Creates a Enum type */
public Enum<V extends TEnumValue, T extends Record<TEnumKey, V>>(item: T, options: SchemaOptions = {}): TEnum<T> {
return Enum(item, options)
Expand Down
1 change: 1 addition & 0 deletions src/type/builder/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export { Const } from '../const/index'
export { Constructor } from '../constructor/index'
export { ConstructorParameters } from '../constructor-parameters/index'
export { Date } from '../date/index'
export { Deref } from '../deref/index'
export { Enum } from '../enum/index'
export { Exclude } from '../exclude/index'
export { Extends } from '../extends/index'
Expand Down
190 changes: 190 additions & 0 deletions src/type/deref/deref.ts
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)
}
29 changes: 29 additions & 0 deletions src/type/deref/index.ts
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'
1 change: 1 addition & 0 deletions src/type/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export * from './const/index'
export * from './constructor/index'
export * from './constructor-parameters/index'
export * from './date/index'
export * from './deref/index'
export * from './discard/index'
export * from './enum/index'
export * from './exclude/index'
Expand Down
3 changes: 2 additions & 1 deletion todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- [x] Exclude, Extract and Extends could use a clean up
- [x] Look for invalid import references
- [x] The Symbols Change May be a bit too breaking. Move Kind, Hint, and others to the top (again)
- [ ] Implement static tests for Type.Const()
- [ ] Implement tests for Type.Const()
- [ ] Implement tests for Type.Deref()
- [ ] Implement experimental Type.Mapped(). This will only be usable on the Workbench
- [ ] Maybe rename the arguments back to 'schema' (T is a little verbose)

0 comments on commit 9bfc72b

Please sign in to comment.