@sinclair/typebox
Advanced tools
Comparing version 0.20.1 to 0.20.2
{ | ||
"name": "@sinclair/typebox", | ||
"version": "0.20.1", | ||
"version": "0.20.2", | ||
"description": "JSONSchema Type Builder with Static Type Resolution for TypeScript", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -41,3 +41,3 @@ <div align='center'> | ||
TypeBox is a type builder library that creates in-memory JSON Schema objects that can be statically resolved to TypeScript types. The schemas produced by this library are built to match the static type checking rules of the TypeScript compiler. TypeBox allows one to create a single unified type that can be both statically checked by the TypeScript compiler and runtime asserted using standard JSON schema validation. | ||
TypeBox is a library that creates in-memory JSON Schema objects that can be statically resolved to TypeScript types. The schemas produced by this library are designed to match the static type checking rules of the TypeScript compiler. TypeBox allows one to create a unified type that can be statically checked by the TypeScript compiler and runtime asserted using standard JSON Schema validation. | ||
@@ -403,3 +403,3 @@ TypeBox can be used as a simple tool to build up complex schemas or integrated into RPC or REST services to help validate JSON data received over the wire. TypeBox does not provide any JSON schema validation. Please use libraries such as AJV to validate schemas built with this library. | ||
TypeBox supports Generic Types. The following creates a Generic Type `Nullable<T>`. | ||
Generic types can be created using functions. The following creates a generic `Nullable<T>` type. | ||
@@ -409,6 +409,5 @@ ```typescript | ||
function Nullable<T extends TSchema>(t: T) { | ||
// type Nullable<T> = T | null | ||
return Type.Union([t, Type.Null()]) | ||
} | ||
const Nullable = <T extends TSchema>(type: T) => Type.Union([type, Type.Null()]) | ||
@@ -440,3 +439,3 @@ const T = Nullable(Type.String()) // const T = { | ||
Reference Types can be used to reduce schema duplication. TypeBox provides support for referencing with the `Type.Ref(...)` and `Type.Box(...)` functions. The `Type.Ref(...)` function references into an existing type and `Type.Box(...)` provides a container for multiple referenceable types. To reference a type you must specify an `$id` on the target type being referenced. The following example shows referencing an existing `string` type. | ||
Types can be referenced with `Type.Ref(...)`. To reference a type, the target type must specify an `$id`. | ||
@@ -453,5 +452,5 @@ ```typescript | ||
``` | ||
The `Type.Box(...)` function provides a way to group related types under a common namespace. The following example groups a set of related `Vector` types under the namespace `Math3D` which are later referenced in the `Vertex` structure below. | ||
It can be helpful to organize shared referenced types under a common namespace. The `Type.Box(...)` function can be used to create a shared definition container for related types. The following creates a `Math3D` container and a `Vertex` structure that references types in the container. | ||
```typescript | ||
@@ -506,3 +505,3 @@ const Math3D = Type.Box({ // const Math3D = { | ||
TypeBox provides support for creating recursive schemas. This is handled with the `Type.Rec(...)` function. The following will create a `Node` type that contains an array of inner Nodes. Note that due to current restrictions on TypeScript recursive inference, it's currently not possible for TypeBox to statically infer for recursive types. Instead TypeBox will resolve inner recursive types as `any`. | ||
Recursive types can be created with the `Type.Rec(...)` function. The following creates a `Node` type that contains an array of inner Nodes. Note that due to current restrictions on TypeScript inference, it is not possible for TypeBox to statically infer for recursive types. TypeBox will infer the inner recursive type as `any`. | ||
@@ -632,3 +631,3 @@ ```typescript | ||
TypeBox does not provide JSON schema validation out of the box and expects users to select an appropriate JSON schema validation library for their needs. TypeBox schemas should match JSON Schema draft `2019-09` so any library capable of draft `2019-09` should be fine. A good library to use for validation is [Ajv](https://www.npmjs.com/package/ajv). The following example shows setting up Ajv 7 to work with TypeBox. | ||
TypeBox does not provide JSON schema validation functionality, so users will need to select an appropriate JSON Schema validator for their language or framework. TypeBox targets JSON Schema draft `2019-09` so any validator capable of draft `2019-09` should be fine. A good library to use for validation in JavaScript environments is [AJV](https://www.npmjs.com/package/ajv). The following example shows setting up AJV 7 to work with TypeBox. | ||
@@ -681,3 +680,3 @@ ```bash | ||
const User = Type.Object({ | ||
id: Type.String({ format: 'uuid' }), | ||
userId: Type.String({ format: 'uuid' }), | ||
email: Type.String({ format: 'email' }), | ||
@@ -694,5 +693,5 @@ online: Type.Boolean(), | ||
const ok = ajv.validate(User, { | ||
id: '68b4b1d8-0db6-468d-b551-02069a692044', | ||
email: 'dave@domain.com', | ||
online: true | ||
userId: '68b4b1d8-0db6-468d-b551-02069a692044', | ||
email: 'dave@domain.com', | ||
online: true | ||
}) // -> ok | ||
@@ -703,3 +702,3 @@ ``` | ||
[Reference Types](#Reference-Types) can be added to AJV with the `ajv.addSchema(...)` function. The following moves the `id` and `email` types above into a common box and registers it with the validator. | ||
Referenced types can be added to AJV with the `ajv.addSchema(...)` function. The following moves the `userId` and `email` property types into a `Type.Box(...)` and registers the box with AJV. | ||
@@ -736,3 +735,3 @@ ```typescript | ||
const User = Type.Object({ | ||
id: Type.Ref(Common, 'UserId'), | ||
userId: Type.Ref(Common, 'UserId'), | ||
email: Type.Ref(Common, 'Email'), | ||
@@ -749,4 +748,4 @@ online: Type.Boolean() | ||
const ok = ajv.validate(User, { | ||
id: '68b4b1d8-0db6-468d-b551-02069a692044', | ||
email: 'dave@domain.com', | ||
userId: '68b4b1d8-0db6-468d-b551-02069a692044', | ||
email: 'dave@domain.com', | ||
online: true | ||
@@ -757,2 +756,2 @@ }) // -> ok | ||
For more information on AJV, refer to the website located [here](https://ajv.js.org/guide/getting-started.html). | ||
Please refer to the official AJV [documentation](https://ajv.js.org/guide/getting-started.html) for more information on using this validator. |
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
88471
745