-
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
2a62259
commit 7df9219
Showing
8 changed files
with
174 additions
and
106 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,15 +1,10 @@ | ||
import { Value } from '@sinclair/typebox/value' | ||
import { TSchema, Type, TypeGuard, Discard, Evaluate, Ensure, TObject, Kind } from '@sinclair/typebox' | ||
import { PickResolver, IndexedKeyResolver } from '@sinclair/typebox/type' | ||
|
||
const L = Type.Union([Type.Literal('x'), Type.Literal('y'), Type.Literal('z')]) | ||
const K = Type.TemplateLiteral('${0|1}${0|1}${0|1}${0|1}${0|1}') | ||
|
||
const T = Type.Pick( | ||
Type.Record(K, Type.Number()), | ||
K, | ||
) | ||
console.log(T) | ||
import { TSchema, Type, Static, TypeGuard, Discard, Evaluate, Ensure, TObject, Kind } from '@sinclair/typebox' | ||
import { PartialResolver, IndexedKeyResolver } from '@sinclair/typebox/type' | ||
|
||
const A = Type.Object({ x: Type.Number() }) | ||
const B = Type.Object({ y: Type.Number() }) | ||
const I = Type.Intersect([A, B]) | ||
const T = Type.Partial(I) | ||
|
||
console.log(T) |
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,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 './partial' |
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,95 @@ | ||
/*-------------------------------------------------------------------------- | ||
@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, TypeGuard, TObject, TProperties, TIntersect, TUnion, TOptional, TRecursive } from '../../../typebox' | ||
|
||
// prettier-ignore | ||
export namespace PartialResolver { | ||
export type Evaluate<T> = T extends infer O ? { [K in keyof O]: O[K] } : never | ||
// ---------------------------------------------------------------- | ||
// Intersect | ||
// ---------------------------------------------------------------- | ||
export type Intersect<T extends TSchema[]> = ( | ||
T extends [infer L extends TSchema, ...infer R extends TSchema[]] | ||
? [Resolve<L>, ...Intersect<R>] | ||
: [] | ||
) | ||
export function Intersect<T extends TSchema[]>(T: [...T]) : Intersect<T> { | ||
const [L, ...R] = T | ||
return ( | ||
T.length > 0 | ||
? [Resolve(L), ...Intersect(R)] | ||
: [] | ||
) as Intersect<T> | ||
} | ||
// ---------------------------------------------------------------- | ||
// Union | ||
// ---------------------------------------------------------------- | ||
export type Union<T extends TSchema[]> = ( | ||
T extends [infer L extends TSchema, ...infer R extends TSchema[]] | ||
? [Resolve<L>, ...Union<R>] | ||
: [] | ||
) | ||
export function Union<T extends TSchema[]>(T: [...T]): Union<T> { | ||
const [L, ...R] = T | ||
return ( | ||
T.length > 0 | ||
? [Resolve(L), ...Union(R)] | ||
: [] | ||
) as Union<T> | ||
} | ||
// ---------------------------------------------------------------- | ||
// Properties | ||
// ---------------------------------------------------------------- | ||
export type Properties<T extends TProperties> = Evaluate<{ | ||
[K in keyof T]: TOptional<T[K]> | ||
}> | ||
export function Properties<T extends TProperties>(T: T) { | ||
return globalThis.Object.getOwnPropertyNames(T).reduce((Acc, K) => { | ||
return { ...Acc, [K]: Type.Optional(T[K]) } | ||
}, {} as TProperties) | ||
} | ||
// ---------------------------------------------------------------- | ||
// Resolve | ||
// ---------------------------------------------------------------- | ||
export type Resolve<T extends TSchema> = ( | ||
T extends TRecursive<infer S> ? TRecursive<Resolve<S>> : | ||
T extends TIntersect<infer S> ? TIntersect<Intersect<S>> : | ||
T extends TUnion<infer S> ? TUnion<Union<S>> : | ||
T extends TObject<infer S> ? TObject<Properties<S>> : | ||
TObject<{}> | ||
) | ||
export function Resolve<T extends TSchema>(T: T): Resolve<T> { | ||
return ( | ||
TypeGuard.TIntersect(T) ? Type.Intersect(Intersect(T.allOf)) : | ||
TypeGuard.TUnion(T) ? Type.Union(Union(T.anyOf)) : | ||
TypeGuard.TObject(T) ? Type.Object(Properties(T.properties)) : | ||
Type.Object({}) | ||
) as Resolve<T> | ||
} | ||
} |
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