Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@sinclair/typebox

Package Overview
Dependencies
Maintainers
1
Versions
338
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@sinclair/typebox - npm Package Compare versions

Comparing version 0.23.0 to 0.23.1

2

package.json
{
"name": "@sinclair/typebox",
"version": "0.23.0",
"version": "0.23.1",
"description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",

@@ -5,0 +5,0 @@ "keywords": [

@@ -5,4 +5,8 @@ <div align='center'>

<img src="https://github.com/sinclairzx81/typebox/blob/master/typebox.png?raw=true" />
<p>JSON Schema Type Builder with Static Type Resolution for TypeScript</p>
[![npm version](https://badge.fury.io/js/%40sinclair%2Ftypebox.svg)](https://badge.fury.io/js/%40sinclair%2Ftypebox) [![GitHub CI](https://github.com/sinclairzx81/typebox/workflows/GitHub%20CI/badge.svg)](https://github.com/sinclairzx81/typebox/actions)

@@ -28,3 +32,3 @@

## Usage
## Example

@@ -43,3 +47,3 @@ ```typescript

TypeBox is a library that builds 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.
TypeBox is a library that creates in-memory JSON Schema objects that can be statically inferred as 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 both statically asserted by the TypeScript compiler and runtime asserted using industry standard JSON Schema validation.

@@ -55,3 +59,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.

- [Overview](#Overview)
- [Example](#Example)
- [Usage](#Usage)
- [Types](#Types)

@@ -70,3 +74,3 @@ - [Modifiers](#Modifiers)

## Example
## Usage

@@ -97,21 +101,21 @@ The following demonstrates TypeBox's general usage.

const T = Type.Object({ // const T = {
id: Type.String(), // type: 'object',
name: Type.String(), // properties: {
timestamp: Type.Integer() // id: {
}) // type: 'string'
// },
// name: {
// type: 'string'
// },
// timestamp: {
// type: 'integer'
// }
// },
// required: [
// "id",
// "name",
// "timestamp"
// ]
// }
const T = Type.Object({ // const T = {
id: Type.String(), // type: 'object',
name: Type.String(), // properties: {
timestamp: Type.Integer() // id: {
}) // type: 'string'
// },
// name: {
// type: 'string'
// },
// timestamp: {
// type: 'integer'
// }
// },
// required: [
// "id",
// "name",
// "timestamp"
// ]
// }

@@ -124,7 +128,7 @@ //--------------------------------------------------------------------------------------------

type T = Static<typeof T> // type T = {
// id: string,
// name: string,
// timestamp: number
// }
type T = Static<typeof T> // type T = {
// id: string,
// name: string,
// timestamp: number
// }

@@ -137,5 +141,5 @@ //--------------------------------------------------------------------------------------------

function receive(value: T) { // ... as a Type
function receive(value: T) { // ... as a Type
if(JSON.validate(T, value)) { // ... as a Schema
if(JSON.validate(T, value)) { // ... as a Schema

@@ -156,9 +160,9 @@ // ok...

│ TypeBox │ TypeScript │ JSON Schema │
│ │ │ │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.Any() │ type T = any │ const T = { } │
│ │ │ │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.Unknown() │ type T = unknown │ const T = { } │
│ │ │ │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤

@@ -168,3 +172,3 @@ │ const T = Type.String() │ type T = string │ const T = { │

│ │ │ } │
│ │ │ │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤

@@ -174,3 +178,3 @@ │ const T = Type.Number() │ type T = number │ const T = { │

│ │ │ } │
│ │ │ │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤

@@ -180,3 +184,3 @@ │ const T = Type.Integer() │ type T = number │ const T = { │

│ │ │ } │
│ │ │ │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤

@@ -186,3 +190,3 @@ │ const T = Type.Boolean() │ type T = boolean │ const T = { │

│ │ │ } │
│ │ │ │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤

@@ -192,9 +196,9 @@ │ const T = Type.Null() │ type T = null │ const T = { │

│ │ │ } │
│ │ │ │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.RegEx(/foo/) │ type T = string │ const T = { │
│ const T = Type.RegEx(/foo/) │ type T = string │ const T = { │
│ │ │ type: 'string', │
│ │ │ pattern: 'foo' │
│ │ │ } │
│ │ │ │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤

@@ -205,3 +209,3 @@ │ const T = Type.Literal(42) │ type T = 42 │ const T = { │

│ │ │ } │
│ │ │ │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤

@@ -214,3 +218,3 @@ │ const T = Type.Array( │ type T = number[] │ const T = { │

│ │ │ } │
│ │ │ │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤

@@ -222,10 +226,10 @@ │ const T = Type.Object({ │ type T = { │ const T = { │

│ │ │ type: 'number' │
│ │ │ }, │
│ │ │ y: { │
│ │ │ type: 'number' │
│ │ │ }, │
│ │ │ y: { │
│ │ │ type: 'number' │
│ │ │ } │
│ │ │ }, │
│ │ │ required: ['x', 'y'] │
│ │ │ }, │
│ │ │ required: ['x', 'y'] │
│ │ │ } │
│ │ │ │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤

@@ -236,12 +240,12 @@ │ const T = Type.Tuple([ │ type T = [number, number] │ const T = { │

│ ]) │ │ { │
│ │ │ type: 'number' │
│ │ │ }, { │
│ │ │ type: 'number' │
│ │ │ } │
│ │ │ ], │
│ │ │ additionalItems: false, │
│ │ │ minItems: 2, │
│ │ │ maxItems: 2, │
│ │ │ } │
│ │ │ │
│ │ │ type: 'number' │
│ │ │ }, { │
│ │ │ type: 'number' │
│ │ │ } │
│ │ │ ], │
│ │ │ additionalItems: false, │
│ │ │ minItems: 2, │
│ │ │ maxItems: 2, │
│ │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤

@@ -265,3 +269,3 @@ │ enum Foo { │ enum Foo { │ const T = { │

│ ) │ │ │
│ │ │ │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤

@@ -275,3 +279,3 @@ │ const T = Type.Union([ │ type T = string | number │ const T = { │

│ │ │ } │
│ │ │ │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤

@@ -283,3 +287,3 @@ │ const T = Type.Intersect([ │ type T = { │ const T = { │

│ Type.Object({ │ } │ a: { │
│ y: Type.Number() │ │ type: 'number' │
│ y: Type.Number() │ │ type: 'number' │
│ }) │ │ } │

@@ -292,9 +296,9 @@ │ }) │ │ }, │

│ │ │ b: { │
│ │ │ type: 'number' │
│ │ │ } │
│ │ │ }, │
│ │ │ required: ['b'] │
│ │ │ }] │
│ │ │ } │
│ │ │ │
│ │ │ type: 'number' │
│ │ │ } │
│ │ │ }, │
│ │ │ required: ['b'] │
│ │ │ }] │
│ │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤

@@ -304,8 +308,8 @@ │ const T = Type.Record( │ type T = { │ const T = { │

│ Type.Number() │ } │ patternProperties: { │
│ ) │ │ '^.*$': { │
│ │ │ type: 'number' │
│ │ │ } │
│ │ │ } │
│ │ │ } │
│ │ │ │
│ ) │ │ '^.*$': { │
│ │ │ type: 'number' │
│ │ │ } │
│ │ │ } │
│ │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤

@@ -323,3 +327,3 @@ │ const T = Type.Partial( │ type T = Partial<{ │ const T = { │

│ │ │ } │
│ │ │ │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤

@@ -338,3 +342,3 @@ │ const T = Type.Required( │ type T = Required<{ │ const T = { │

│ │ │ } │
│ │ │ │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤

@@ -373,3 +377,3 @@ │ const T = Type.Pick( │ type T = Pick<{ │ const T = { │

│ TypeBox │ TypeScript │ JSON Schema │
│ │ │ │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤

@@ -379,8 +383,8 @@ │ const T = Type.Object({ │ type T = { │ const T = { │

│ Type.String(), │ } │ properties: { │
│ ) │ │ name: { │
│ ) │ │ name: { │
│ }) │ │ type: 'string' │
│ │ │ } │
│ │ │ } │
│ │ │ } │
│ │ │ │
│ │ │ } │
│ │ │ } │
│ │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤

@@ -390,9 +394,9 @@ │ const T = Type.Object({ │ type T = { │ const T = { │

│ Type.String(), │ } │ properties: { │
│ ) │ │ name: { │
│ ) │ │ name: { │
│ }) │ │ type: 'string' │
│ │ │ } │
│ │ │ }, │
│ │ │ } │
│ │ │ }, │
│ │ │ required: ['name'] │
│ │ │ } │
│ │ │ │
│ │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤

@@ -402,8 +406,8 @@ │ const T = Type.Object({ │ type T = { │ const T = { │

│ Type.String(), │ } │ properties: { │
│ ) │ │ name: { │
│ ) │ │ name: { │
│ }) │ │ type: 'string' │
│ │ │ } │
│ │ │ } │
│ │ │ } │
│ │ │ } │
│ │ │ } │
│ │ │ │
│ │ │ │
└────────────────────────────────┴─────────────────────────────┴────────────────────────────────┘

@@ -441,21 +445,21 @@ ```

const T = Nullable(Type.String()) // const T = {
// "anyOf": [{
// type: 'string'
// }, {
// type: 'null'
// }]
// }
const T = Nullable(Type.String()) // const T = {
// "anyOf": [{
// type: 'string'
// }, {
// type: 'null'
// }]
// }
type T = Static<typeof T> // type T = string | null
type T = Static<typeof T> // type T = string | null
const U = Nullable(Type.Number()) // const U = {
// "anyOf": [{
// type: 'number'
// }, {
// type: 'null'
// }]
// }
const U = Nullable(Type.Number()) // const U = {
// "anyOf": [{
// type: 'number'
// }, {
// type: 'null'
// }]
// }
type U = Static<typeof U> // type U = number | null
type U = Static<typeof U> // type U = number | null
```

@@ -470,57 +474,57 @@

```typescript
const T = Type.String({ $id: 'T' }) // const T = {
// $id: 'T',
// type: 'string'
// }
const T = Type.String({ $id: 'T' }) // const T = {
// $id: 'T',
// type: 'string'
// }
const R = Type.Ref(T) // const R = {
// $ref: 'T'
// }
const R = Type.Ref(T) // const R = {
// $ref: 'T'
// }
```
It can be helpful to organize shared referenced types under a common namespace. The `Type.Namespace(...)` 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.
It can sometimes be helpful to organize shared referenced types under a common namespace. The `Type.Namespace(...)` 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
const Math3D = Type.Namespace({ // const Math3D = {
Vector4: Type.Object({ // $id: 'Math3D',
x: Type.Number(), // $defs: {
y: Type.Number(), // Vector4: {
z: Type.Number(), // type: 'object',
w: Type.Number() // properties: {
}), // x: { type: 'number' },
Vector3: Type.Object({ // y: { type: 'number' },
x: Type.Number(), // z: { type: 'number' },
y: Type.Number(), // w: { type: 'number' }
z: Type.Number() // },
}), // required: ['x', 'y', 'z', 'w']
Vector2: Type.Object({ // },
x: Type.Number(), // Vector3: {
y: Type.Number() // type: 'object',
}) // properties: {
}, { $id: 'Math3D' }) // x: { 'type': 'number' },
// y: { 'type': 'number' },
// z: { 'type': 'number' }
// },
// required: ['x', 'y', 'z']
// },
// Vector2: {
// type: 'object',
// properties: {
// x: { 'type': 'number' },
// y: { 'type': 'number' },
// },
// required: ['x', 'y']
// }
// }
// }
const Math3D = Type.Namespace({ // const Math3D = {
Vector4: Type.Object({ // $id: 'Math3D',
x: Type.Number(), // $defs: {
y: Type.Number(), // Vector4: {
z: Type.Number(), // type: 'object',
w: Type.Number() // properties: {
}), // x: { type: 'number' },
Vector3: Type.Object({ // y: { type: 'number' },
x: Type.Number(), // z: { type: 'number' },
y: Type.Number(), // w: { type: 'number' }
z: Type.Number() // },
}), // required: ['x', 'y', 'z', 'w']
Vector2: Type.Object({ // },
x: Type.Number(), // Vector3: {
y: Type.Number() // type: 'object',
}) // properties: {
}, { $id: 'Math3D' }) // x: { 'type': 'number' },
// y: { 'type': 'number' },
// z: { 'type': 'number' }
// },
// required: ['x', 'y', 'z']
// },
// Vector2: {
// type: 'object',
// properties: {
// x: { 'type': 'number' },
// y: { 'type': 'number' },
// },
// required: ['x', 'y']
// }
// }
// }
const Vertex = Type.Object({ // const Vertex = {
position: Type.Ref(Math3D, 'Vector4'), // type: 'object',
normal: Type.Ref(Math3D, 'Vector3'), // properties: {
uv: Type.Ref(Math3D, 'Vector2') // position: { $ref: 'Math3D#/$defs/Vector4' },
}) // normal: { $ref: 'Math3D#/$defs/Vector3' },
// uv: { $ref: 'Math3D#/$defs/Vector2' }
// },
// required: ['position', 'normal', 'uv']
// }
const Vertex = Type.Object({ // const Vertex = {
position: Type.Ref(Math3D, 'Vector4'), // type: 'object',
normal: Type.Ref(Math3D, 'Vector3'), // properties: {
uv: Type.Ref(Math3D, 'Vector2') // position: { $ref: 'Math3D#/$defs/Vector4' },
}) // normal: { $ref: 'Math3D#/$defs/Vector3' },
// uv: { $ref: 'Math3D#/$defs/Vector2' }
// },
// required: ['position', 'normal', 'uv']
// }
```

@@ -535,26 +539,26 @@

```typescript
const Node = Type.Rec(Self => Type.Object({ // const Node = {
id: Type.String(), // $id: 'Node',
nodes: Type.Array(Self), // $ref: 'Node#/$defs/self',
}), { $id: 'Node' }) // $defs: {
// self: {
// type: 'object',
// properties: {
// id: {
// type: 'string'
// },
// nodes: {
// type: 'array',
// items: {
// $ref: 'Node#/$defs/self'
// }
// }
// }
// }
// }
const Node = Type.Rec(Self => Type.Object({ // const Node = {
id: Type.String(), // $id: 'Node',
nodes: Type.Array(Self), // $ref: 'Node#/$defs/self',
}), { $id: 'Node' }) // $defs: {
// self: {
// type: 'object',
// properties: {
// id: {
// type: 'string'
// },
// nodes: {
// type: 'array',
// items: {
// $ref: 'Node#/$defs/self'
// }
// }
// }
// }
// }
type Node = Static<typeof Node> // type Node = {
// id: string
// nodes: any[]
//
type Node = Static<typeof Node> // type Node = {
// id: string
// nodes: any[]
// }

@@ -577,3 +581,3 @@ function visit(node: Node) {

│ TypeBox │ TypeScript │ Extended Schema │
│ │ │ │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤

@@ -591,3 +595,3 @@ │ const T = Type.Constructor([ │ type T = new ( │ const T = { │

│ │ │ } │
│ │ │ │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤

@@ -605,3 +609,3 @@ │ const T = Type.Function([ │ type T = ( │ const T = { │

│ │ │ } │
│ │ │ │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤

@@ -614,3 +618,3 @@ │ const T = Type.Promise( │ type T = Promise<string> │ const T = { │

│ │ │ } │
│ │ │ │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤

@@ -620,3 +624,3 @@ │ const T = Type.Undefined() │ type T = undefined │ const T = { │

│ │ │ } │
│ │ │ │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤

@@ -626,3 +630,3 @@ │ const T = Type.Void() │ type T = void │ const T = { │

│ │ │ } │
│ │ │ │
│ │ │ │
└────────────────────────────────┴─────────────────────────────┴────────────────────────────────┘

@@ -635,3 +639,3 @@ ```

TypeBox includes the properties `kind` and `modifier` on each underlying schema. These properties are used to help TypeBox statically resolve the schemas to the appropriate TypeScript type as well as apply the appropriate modifiers to an objects properties (such as optional). These properties are not strictly valid JSON schema so in some cases it may be desirable to omit them. TypeBox provides a `Type.Strict()` function that will omit these properties if nessasary.
TypeBox schemas contain the properties `kind` and `modifier`. These properties are provided to enable runtime type reflection on schemas, as well as helping TypeBox apply the appropriate static type inference rules. These properties are not strictly valid JSON schema so in some cases it may be desirable to omit them. TypeBox provides a `Type.Strict()` function that will omit these properties if nessasary.

@@ -665,3 +669,3 @@ ```typescript

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.
TypeBox does not provide JSON schema validation so users will need to select an appropriate JSON Schema validator for their needs. TypeBox schemas target 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.

@@ -732,60 +736,7 @@ ```bash

#### Reference Types
Please refer to the official AJV [documentation](https://ajv.js.org/guide/getting-started.html) for additional information on using AJV.
Referenced types can be added to AJV with the `ajv.addSchema(...)` function. The following moves the `userId` and `email` property types into a `Type.Namespace(...)` and registers the box with AJV.
```typescript
//--------------------------------------------------------------------------------------------
//
// Shared Types
//
//--------------------------------------------------------------------------------------------
const Shared = Type.Namespace({
UserId: Type.String({ format: 'uuid' }),
Email: Type.String({ format: 'email' })
}, { $id: 'Shared' })
//--------------------------------------------------------------------------------------------
//
// Setup Validator and Register Shared Types
//
//--------------------------------------------------------------------------------------------
const ajv = addFormats(new Ajv({}), [...])
.addKeyword('kind')
.addKeyword('modifier')
.addSchema(Shared) // <-- Register Shared Types
//--------------------------------------------------------------------------------------------
//
// Create a TypeBox type
//
//--------------------------------------------------------------------------------------------
const User = Type.Object({
userId: Type.Ref(Shared, 'UserId'),
email: Type.Ref(Shared, 'Email'),
online: Type.Boolean()
}, { additionalProperties: false })
//--------------------------------------------------------------------------------------------
//
// Validate Data
//
//--------------------------------------------------------------------------------------------
const ok = ajv.validate(User, {
userId: '68b4b1d8-0db6-468d-b551-02069a692044',
email: 'dave@domain.com',
online: true
}) // -> ok
```
Please refer to the official AJV [documentation](https://ajv.js.org/guide/getting-started.html) for additional information.
### OpenAPI
TypeBox can be used to create schemas for OpenAPI, however users should be aware of the various differences between the JSON Schema and OpenAPI specifications. Two common instances where OpenAPI diverges from the JSON Schema specification is OpenAPI's handling of `string enum` and `nullable`. The following shows how you can use TypeBox to construct these types.
TypeBox can be used to create schemas for OpenAPI, however users should be aware of the various disparities between the JSON Schema and OpenAPI schema specifications. Two common instances where OpenAPI diverges from the JSON Schema specification is OpenAPI's handling of string enums and nullable schemas. The following shows how you can use TypeBox to construct these types.

@@ -805,8 +756,8 @@ ```typescript

const T = Nullable(Type.String()) // const T = {
// type: 'string',
// nullable: true
// }
const T = Nullable(Type.String()) // const T = {
// type: 'string',
// nullable: true
// }
type T = Static<typeof T> // type T = string | null
type T = Static<typeof T> // type T = string | null

@@ -825,7 +776,7 @@ //--------------------------------------------------------------------------------------------

const T = StringUnion(['A', 'B', 'C']) // const T = {
// enum: ['A', 'B', 'C']
// }
const T = StringUnion(['A', 'B', 'C']) // const T = {
// enum: ['A', 'B', 'C']
// }
type T = Static<typeof T> // type T = 'A' | 'B' | 'C'
type T = Static<typeof T> // type T = 'A' | 'B' | 'C'
```

@@ -145,2 +145,7 @@ export declare const ReadonlyOptionalModifier: unique symbol;

}
export interface TRef<T extends TSchema> extends TSchema, CustomOptions {
$static: Static<T>;
kind: typeof RefKind;
$ref: string;
}
export interface TString extends TSchema, StringOptions<string> {

@@ -179,7 +184,2 @@ $static: string;

}
export interface TRef<T extends TSchema> extends TSchema, CustomOptions {
$static: Static<T>;
kind: typeof RefKind;
$ref: string;
}
export declare const ConstructorKind: unique symbol;

@@ -220,5 +220,5 @@ export declare const FunctionKind: unique symbol;

}
export declare type Pickable = TObject<TProperties> | TRef<TObject<TProperties>>;
export declare type PickablePropertyKeys<T extends Pickable> = T extends TObject<infer U> ? keyof U : T extends TRef<TObject<infer U>> ? keyof U : never;
export declare type PickableProperties<T extends Pickable> = T extends TObject<infer U> ? U : T extends TRef<TObject<infer U>> ? U : never;
export declare type Selectable = TObject<TProperties> | TRef<TObject<TProperties>>;
export declare type SelectablePropertyKeys<T extends Selectable> = T extends TObject<infer U> ? keyof U : T extends TRef<TObject<infer U>> ? keyof U : never;
export declare type SelectableProperties<T extends Selectable> = T extends TObject<infer U> ? U : T extends TRef<TObject<infer U>> ? U : never;
export declare type UnionToIntersect<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;

@@ -277,3 +277,3 @@ export declare type StaticReadonlyOptionalPropertyKeys<T extends TProperties> = {

export declare class TypeBuilder {
private readonly schemas;
protected readonly schemas: Map<string, TSchema>;
/** `Standard` Modifies an object property to be both readonly and optional */

@@ -315,6 +315,6 @@ ReadonlyOptional<T extends TSchema>(item: T): TReadonlyOptional<T>;

Any(options?: CustomOptions): TAny;
/** `Standard` Creates a keyof type from the given object */
KeyOf<T extends TObject<TProperties>>(object: T, options?: CustomOptions): TKeyOf<(keyof T['properties'])[]>;
/** `Standard` Creates a record type */
Record<K extends TRecordKey, T extends TSchema>(key: K, value: T, options?: ObjectOptions): TRecord<K, T>;
/** `Standard` Creates a keyof type from the given object */
KeyOf<T extends TObject<TProperties> | TRef<TObject<TProperties>>>(object: T, options?: CustomOptions): TKeyOf<SelectablePropertyKeys<T>[]>;
/** `Standard` Makes all properties in the given object type required */

@@ -325,5 +325,5 @@ Required<T extends TObject<TProperties> | TRef<TObject<TProperties>>>(object: T, options?: ObjectOptions): TObject<StaticRequired<T['properties']>>;

/** `Standard` Picks property keys from the given object type */
Pick<T extends TObject<TProperties> | TRef<TObject<TProperties>>, K extends PickablePropertyKeys<T>[]>(object: T, keys: [...K], options?: ObjectOptions): TObject<Pick<PickableProperties<T>, K[number]>>;
Pick<T extends TObject<TProperties> | TRef<TObject<TProperties>>, K extends SelectablePropertyKeys<T>[]>(object: T, keys: [...K], options?: ObjectOptions): TObject<Pick<SelectableProperties<T>, K[number]>>;
/** `Standard` Omits property keys from the given object type */
Omit<T extends TObject<TProperties> | TRef<TObject<TProperties>>, K extends PickablePropertyKeys<T>[]>(object: T, keys: [...K], options?: ObjectOptions): TObject<Omit<PickableProperties<T>, K[number]>>;
Omit<T extends TObject<TProperties> | TRef<TObject<TProperties>>, K extends SelectablePropertyKeys<T>[]>(object: T, keys: [...K], options?: ObjectOptions): TObject<Omit<SelectableProperties<T>, K[number]>>;
/** `Standard` Omits the `kind` and `modifier` properties from the underlying schema */

@@ -344,3 +344,3 @@ Strict<T extends TSchema>(schema: T, options?: CustomOptions): T;

/** `Standard` References a type within a namespace. The referenced namespace must specify an `$id` */
Ref<T extends TNamespace<TDefinitions>, K extends keyof T['$defs']>(box: T, key: K): TRef<T['$defs'][K]>;
Ref<T extends TNamespace<TDefinitions>, K extends keyof T['$defs']>(namespace: T, key: K): TRef<T['$defs'][K]>;
/** `Standard` References type. The referenced type must specify an `$id` */

@@ -350,7 +350,7 @@ Ref<T extends TSchema>(schema: T): TRef<T>;

Rec<T extends TSchema>(callback: (self: TAny) => T, options?: CustomOptions): T;
/** Stores this schema if it contains an $id. This function is used for later referencing. */
private Store;
/** Resolves a schema by $id. May resolve recursively if the target is a TRef. */
private Resolve;
/** Conditionally stores and schema if it contains an $id and returns. */
protected Store(schema: any): any;
/** Conditionally dereferences a schema if RefKind. Otherwise return argument. */
protected Deref(schema: any): any;
}
export declare const Type: TypeBuilder;

@@ -178,7 +178,2 @@ "use strict";

}
/** `Standard` Creates a keyof type from the given object */
KeyOf(object, options = {}) {
const keys = Object.keys(object.properties);
return this.Store({ ...options, kind: exports.KeyOfKind, type: 'string', enum: keys });
}
/** `Standard` Creates a record type */

@@ -197,5 +192,11 @@ Record(key, value, options = {}) {

}
/** `Standard` Creates a keyof type from the given object */
KeyOf(object, options = {}) {
const source = this.Deref(object);
const keys = Object.keys(source.properties);
return this.Store({ ...options, kind: exports.KeyOfKind, type: 'string', enum: keys });
}
/** `Standard` Makes all properties in the given object type required */
Required(object, options = {}) {
const source = this.Resolve(object);
const source = this.Deref(object);
const schema = { ...clone(source), ...options };

@@ -224,3 +225,3 @@ schema.required = Object.keys(schema.properties);

Partial(object, options = {}) {
const source = this.Resolve(object);
const source = this.Deref(object);
const schema = { ...clone(source), ...options };

@@ -249,3 +250,3 @@ delete schema.required;

Pick(object, keys, options = {}) {
const source = this.Resolve(object);
const source = this.Deref(object);
const schema = { ...clone(source), ...options };

@@ -261,3 +262,3 @@ schema.required = schema.required ? schema.required.filter((key) => keys.includes(key)) : undefined;

Omit(object, keys, options = {}) {
const source = this.Resolve(object);
const source = this.Deref(object);
const schema = { ...clone(source), ...options };

@@ -327,3 +328,3 @@ schema.required = schema.required ? schema.required.filter((key) => !keys.includes(key)) : undefined;

}
/** Stores this schema if it contains an $id. This function is used for later referencing. */
/** Conditionally stores and schema if it contains an $id and returns. */
Store(schema) {

@@ -335,4 +336,4 @@ if (!schema.$id)

}
/** Resolves a schema by $id. May resolve recursively if the target is a TRef. */
Resolve(schema) {
/** Conditionally dereferences a schema if RefKind. Otherwise return argument. */
Deref(schema) {
if (schema.kind !== exports.RefKind)

@@ -342,3 +343,3 @@ return schema;

throw Error(`Unable to locate schema with $id '${schema.$ref}'`);
return this.Resolve(this.schemas.get(schema.$ref));
return this.Deref(this.schemas.get(schema.$ref));
}

@@ -345,0 +346,0 @@ }

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc