tson-schema
This project aims to bring you an easy way to create json-schemas and TypeScript types using a single API. This API is kept as close as possible to json-schema so you don't have to worry about learning yet another API. Support for new json-schema versions and new TypeScript versions is added on a best effort basis.
This is a work in progress! Known missing json-schema features include:
array.additionalItems
object.additionalProperties
$ref
- JSON-Schema conditional schemas (
if
/else
) - Limited TypeScript support for:
- big enum schemas
- big allOf schemas
Installing
npm install -S tson-schema
Or
yarn add tson-schema
Usage
import * as t from 'tson-schema'
const numberArraySchema = t.array({
items: t.number({
minimum: 1
}),
minItems: 2,
uniqueItems: true
})
numberArraySchema.getSchema()
numberArraySchema.type
const objectSchema = t.object({
properties: {
req: t.string(),
opt: t.tuple({
items: [t.integer()]
})
},
required: ['req']
})
objectSchema.getSchema()
objectSchema.type
const enumSchema = t.enum(['A', 2, 'C', 4])
enumSchema.getSchema()
enumSchema.type
const anyOfSchema = t.anyOf([
s.const('A'),
s.integer()
])
enumSchema.getSchema()
enumSchema.type