Socket
Socket
Sign inDemoInstall

@sinclair/typebox

Package Overview
Dependencies
0
Maintainers
1
Versions
306
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @sinclair/typebox

JSONSchema Type Builder with Static Type Resolution for TypeScript


Version published
Maintainers
1
Install size
49.3 kB
Created

Package description

What is @sinclair/typebox?

The @sinclair/typebox package is a TypeScript utility designed to create type-safe schemas with a consistent syntax. It is primarily used for defining data structures with TypeScript types and validating data at runtime using a separate validation library like Ajv.

What are @sinclair/typebox's main functionalities?

Type Creation

Allows the creation of TypeScript types for various data structures such as strings, numbers, objects, arrays, etc. The created types can be used for compile-time type checking and runtime validation.

{"const T = Type.String()"}

Type Composition

Enables the composition of complex types by combining simpler types. This is useful for defining the shape of objects, with optional and required fields.

{"const UserType = Type.Object({ name: Type.String(), age: Type.Optional(Type.Number()) })"}

Type Validation

Provides a way to validate data at runtime against the defined types using a validation library like Ajv. This ensures that the data conforms to the specified schema.

{"const T = Type.String(); const validate = ajv.compile(T); const isValid = validate('hello');"}

Other packages similar to @sinclair/typebox

Readme

Source

Type<Box>

JSONSchema Type Builder with Static Type Resolution for TypeScript

npm version Build Status

Install

npm install @sinclair/typebox --save

Overview

TypeBox is a type builder library that allows developers to compose complex in-memory JSONSchema objects that can be resolved to static TypeScript types. TypeBox internally represents its types as plain JSONSchema objects and leverages TypeScript's Mapped Types to infer schemas to equivalent static type representations. No additional build process is required.

TypeBox can be used as a tool to build and validate complex schemas, or integrated into RPC or REST services to help validate data received over the wire or published directly to consumers to service as developer documentation.

Note that TypeBox does not provide any mechanisms for validating JSONSchema. Please refer to libraries such as AJV or similar to validate the schemas created with this library.

Requires TypeScript 3.8.3 and above.

License MIT

Contents

Example

The following shows the type alias for Order and its TypeBox equivalent.

import { Type, Static } from '@sinclair/typebox'

// Some type...

type Order = {
    email:    string,
    address:  string,
    quantity: number,
    option:   'pizza' | 'salad' | 'pie'
}

// ...can be expressed as...

const Order = Type.Object({
    email:    Type.Format('email'), 
    address:  Type.String(),
    quantity: Type.Range(1, 99),
    option:   Type.Union(
        Type.Literal('pizza'), 
        Type.Literal('salad'),
        Type.Literal('pie')
    )
})

// ... which can be reflected

console.log(JSON.stringify(Order, null, 2))

// ... and statically resolved

type TOrder = Static<typeof Order>

// .. and validated as JSONSchema

JSON.validate(Order, {  // IETF | TC39 ?
    email: 'dave@domain.com', 
    address: '...', 
    quantity: 99, 
    option: 'pie' 
}) 

// ... and so on ...

Types

TypeBox functions generate JSONschema objects. The following table outlines the TypeScript and JSONSchema equivalence.

TypeBox > TypeScript

The following types and modifiers are compatible with JSONschema and have both JSONschema and TypeScript representations.

TypeTypeBoxTypeScript
Optionalconst T = Type.Object({ email: Type.Optional(Type.String()) })type T = { email?: string }
Readonlyconst T = Type.Object({ email: Type.Readonly(Type.String()) })type T = { readonly email: string }
Literalconst T = Type.Literal(123)type T = 123
Stringconst T = Type.String()type T = string
Numberconst T = Type.Number()type T = number
Booleanconst T = Type.Boolean()type T = boolean
Objectconst T = Type.Object({ name: Type.String() })type T = { name: string }
Arrayconst T = Type.Array(Type.Number())type T = number[]
Mapconst T = Type.Map(Type.Number())type T = { [key: string] } : number
Intersectconst T = Type.Intersect(Type.String(), Type.Number())type T = string & number
Unionconst T = Type.Union(Type.String(), Type.Number())type T = string | number
Tupleconst T = Type.Tuple(Type.String(), Type.Number())type T = [string, number]
Anyconst T = Type.Any()type T = any
Nullconst T = Type.Null()type T = null
Patternconst T = Type.Pattern(/foo/)type T = string
Rangeconst T = Type.Range(20, 30)type T = number
Formatconst T = Type.Format('date-time')type T = string
Guidconst T = Type.Guid()type T = string

TypeBox > JSONSchema

The following shows the TypeBox to JSONSchema mappings. The following schemas are returned from each function.

TypeTypeBoxJSONSchema
Literalconst T = Type.Literal(123){ type: 'number', enum: [123] }
Stringconst T = Type.String(){ type: 'string' }
Numberconst T = Type.Number(){ type: 'number' }
Booleanconst T = Type.Boolean(){ type: 'boolean' }
Objectconst T = Type.Object({ name: Type: String() }){ type: 'object': properties: { name: { type: 'string' } }, required: ['name'] }
Arrayconst T = Type.Array(Type.String()){ type: 'array': items: { type: 'string' } }
Mapconst T = Type.Map(Type.Number()){ type: 'object', additionalProperties: { type: 'number' } }
Intersectconst T = Type.Intersect(Type.Number(), Type.String()){ allOf: [{ type: 'number'}, {type: 'string'}] }
Unionconst T = Type.Union(Type.Number(), Type.String()){ oneOf: [{ type: 'number'}, {type: 'string'}] }
Tupleconst T = Type.Union(Type.Number(), Type.String()){ type: "array", items: [{type: 'string'}, {type: 'number'}], additionalItems: false, minItems: 2, maxItems: 2 }
Anyconst T = Type.Any(){ }
Nullconst T = Type.Null(){ type: 'null' }
Patternconst T = Type.Pattern(/foo/){ type: 'string', pattern: 'foo' }
Rangeconst T = Type.Range(20, 30){ type: 'number', minimum: 20, maximum: 30 }
Formatconst T = Type.Format('date-time'){ type: 'string',format: 'date-time' }
Guidconst T = Type.Guid(){ type: 'string', format: '' }

Other Types

TypeBox provides some non-standard JSONSchema functions that TypeBox refers to as Intrinsic types. While these types cannot be used with JSONSchema, they do provide similar reflection and introspection metadata for expressing function signatures with TypeBox.

See Functions section for more details.

TypeBox > Intrinsics

IntrinsicTypeBoxTypeScript
Functionconst T = Type.Function([Type.String()], Type.String())type T = (arg0: string) => string
Promiseconst T = Type.Promise(Type.String())type T = Promise<string>
Undefinedconst T = Type.Undefined()type T = undefined
Voidconst T = Type.Void()type T = void

TypeBox > Non Schema

IntrinsicTypeBoxTypeScript
Functionconst T = Type.Function([Type.String()], Type.Number()){ type: 'function', arguments: [ { type: 'string' } ], returns: { type: 'number' } }
Promiseconst T = Type.Promise(Type.String()){ type: 'promise', item: { type: 'string' } }
Undefinedconst T = Type.Undefined(){ type: 'undefined' }
Voidconst T = Type.Void(){ type: 'void' }

Functions

TypeBox provides some capabilities for building typed function signatures. It is important to note however that unlike the other functions available on Type the Type.Function(...) and other intrinsic types do not produce valid JSONSchema. However, the types returned from Type.Function(...) may be comprised of schemas that describe its arguments and return types. Consider the following TypeScript and TypeBox variants.


// TypeScript

type T0 = (a0: number, a0: number) => number;

type T1 = (a0: string, a1: () => string) => void;

type T2 = (a0: string) => Promise<number>;

type T3 = () => () => string;

// Convention

Type.Function([...Arguments], ReturnType)

// TypeBox

const T0 = Type.Function([Type.Number(), Type.Number()], Type.Number())

const T1 = Type.Function([Type.String(), Type.Function([], Type.String())], Type.Void())

const T2 = Type.Function([Type.String()], Type.Promise(Type.Number()))

const T3 = Type.Function([], Type.Function([], Type.String()))

Validation

TypeBox does not provide any mechanism for validating JSONSchema out of the box. Users are expected to bring their own JSONSchema validation library. The following demonstrates how you might enable validation with the AJV npm module.

General

import * Ajv from 'ajv'

const ajv = new Ajv({ })

ajv.validate(Type.String(), 'hello')  // true

ajv.validate(Type.String(), 123)      // false

Runtime Type Validation

The following demonstrates how you might want to approach runtime type validation with TypeBox. The following code creates a function that takes a signature type S which is used to infer function arguments. The body of the function validates with the signatures arguments and returns schemas against values passed by the caller.

import { Type, Static, TFunction } from '@sinclair/typebox'

// Some validation function.
declare function validate(schema: any, data: any): boolean;

// A function that returns a closure that validates its 
// arguments and return value from the given signature.
function Func<S extends TFunction>(signature: S, func: Static<S>): Static<S> {    
    const validator = (...params: any[]) => {
        params.forEach((param, index) => {
            if(!validate(signature.arguments[index], param)) {
                console.log('error on argument', index)
            }
        })
        const result = (func as Function)(...params);
        if(!validate(signature.return, result)) {
            console.log('error on return')
        }
        return result
    }
    return validator as Static<S>
}

// Create some function.
const Add = Func(
    Type.Function([
        Type.Number(), 
        Type.Number()
    ], Type.Number()), 
    (a, b) => {
        return a + b
    })

// Call it
Add(20, 30)

FAQs

Last updated on 05 Mar 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc