@sinclair/typebox
Advanced tools
Comparing version 0.19.2 to 0.20.0
{ | ||
"name": "@sinclair/typebox", | ||
"version": "0.19.2", | ||
"version": "0.20.0", | ||
"description": "JSONSchema Type Builder with Static Type Resolution for TypeScript", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -56,2 +56,3 @@ <div align='center'> | ||
- [Options](#Options) | ||
- [Generic Types](#Generic-Types) | ||
- [Reference Types](#Reference-Types) | ||
@@ -240,2 +241,10 @@ - [Recursive Types](#Recursive-Types) | ||
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤ | ||
│ const T = Type.KeyOf( │ type T = keyof { │ const T = { │ | ||
│ Type.Object({ │ x: number, │ enum: ['x', 'y'], │ | ||
│ x: Type.Number(), │ y: number │ type: 'string' │ | ||
│ y: Type.Number() │ } │ } │ | ||
│ }) │ │ │ | ||
│ ) │ │ │ | ||
│ │ │ │ | ||
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤ | ||
│ const T = Type.Union([ │ type T = string | number │ const T = { │ | ||
@@ -250,10 +259,2 @@ │ Type.String(), │ │ anyOf: [{ │ | ||
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤ | ||
│ const T = Type.KeyOf( │ type T = keyof { │ const T = { │ | ||
│ Type.Object({ │ x: number, │ enum: ['x', 'y'], │ | ||
│ x: Type.Number(), │ y: number │ type: 'string' │ | ||
│ y: Type.Number() │ } │ } │ | ||
│ }) │ │ │ | ||
│ ) │ │ │ | ||
│ │ │ │ | ||
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤ | ||
│ const T = Type.Intersect([ │ type T = { │ const T = { │ | ||
@@ -283,3 +284,3 @@ │ Type.Object({ │ x: number │ allOf: [{ │ | ||
│ Type.Number() │ } │ patternProperties: { │ | ||
│ ) │ │ '.*': { │ | ||
│ ) │ │ '^.*$': { │ | ||
│ │ │ type: 'number' │ | ||
@@ -401,3 +402,37 @@ │ │ │ } │ | ||
``` | ||
<a name="Generic-Types"></a> | ||
### Generic Types | ||
TypeBox supports Generic Types. The following creates a generic type `Nullable<T>`. | ||
```typescript | ||
import { Type, Static, TSchema } from '@sinclair/typebox' | ||
function Nullable<T extends TSchema>(t: T) { | ||
return Type.Union([t, Type.Null()]) | ||
} | ||
const T = Nullable(Type.String()) // const T = { | ||
// "anyOf": [{ | ||
// type: 'string' | ||
// }, { | ||
// type: 'null' | ||
// }] | ||
// } | ||
type T = Static<typeof T> // type T = string | null | ||
const U = Nullable(Type.Number()) // const U = { | ||
// "anyOf": [{ | ||
// type: 'number' | ||
// }, { | ||
// type: 'null' | ||
// }] | ||
// } | ||
type U = Static<typeof U> // type U = number | null | ||
``` | ||
<a name="Reference-Types"></a> | ||
@@ -474,8 +509,8 @@ | ||
```typescript | ||
const Node = Type.Rec('Node', Self => // const Node = { | ||
Type.Object({ // $id: 'Node', | ||
id: Type.String(), // $ref: 'Node#/definitions/self', | ||
nodes: Type.Array(Self), // definitions: { | ||
}) // self: { | ||
) // type: 'object', | ||
const Node = Type.Rec(Self => Type.Object({ // const Node = { | ||
id: Type.String(), // $id: 'Node', | ||
nodes: Type.Array(Self), // $ref: 'Node#/definitions/self', | ||
}), { $id: 'Node' }) // definitions: { | ||
// self: { | ||
// type: 'object', | ||
// properties: { | ||
@@ -482,0 +517,0 @@ // id: { |
@@ -313,3 +313,3 @@ export declare const ReadonlyOptionalModifier: unique symbol; | ||
/** `EXPERIMENTAL` Creates a recursive type. */ | ||
Rec<T extends TSchema>($id: string, callback: (self: TAny) => T): T; | ||
Rec<T extends TSchema>(callback: (self: TAny) => T, options?: CustomOptions): T; | ||
/** `EXPERIMENTAL` Creates a recursive type. Pending https://github.com/ajv-validator/ajv/issues/1709 */ | ||
@@ -316,0 +316,0 @@ /** `EXPERIMENTAL` Creates a container for schema definitions. */ |
@@ -276,5 +276,6 @@ "use strict"; | ||
/** `EXPERIMENTAL` Creates a recursive type. */ | ||
Rec($id, callback) { | ||
Rec(callback, options = {}) { | ||
const $id = options.$id || ''; | ||
const self = callback({ $ref: `${$id}#/definitions/self` }); | ||
return { $id, $ref: `${$id}#/definitions/self`, definitions: { self } }; | ||
return { ...options, $ref: `${$id}#/definitions/self`, definitions: { self } }; | ||
} | ||
@@ -281,0 +282,0 @@ /** `EXPERIMENTAL` Creates a recursive type. Pending https://github.com/ajv-validator/ajv/issues/1709 */ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
88769
618
746