Typed, composable JSON schemas. This module consumes and produces JSON schemas with little to no processing.
npm i @tselect/schema [--save]
This module takes the following positions:
additionalProperties
forobject
schemas isfalse
unless you specify a valueadditionalItems
forarray
schemas isfalse
unless you specify a value
Writing JSON schemas for large applications can be pretty time consuming. Having to type double quotes and "additionalProperties" for each new object by hand is no fun.
This module has been created with the intention of reducing the overhead of describing JSON by providing simple wrappers and utilities to manage common use cases with easiness.
It is also fully typed and will allow you to benefit from your favorite IDE's auto-completion, making typing a lot easier.
import { object, email } from '@tselect/schema';
const schema = object({
foo: email()
});
Produces:
schema = {
type: 'object',
additionalProperties: false, // Default, see disclaimer
properties: {
foo: {
type: 'string',
format: 'email'
}
}
}
import { object, email, integer, omitProperties } from '@tselect/schema';
const schema = object({
foo: email(),
bar: integer()
}, {
required: ['foo', 'bar']
});
const modified = omitProperties(schema, ['bar']);
Produces:
modified = {
type: 'object',
required: ['foo'], // Only foo is kept as required
additionalProperties: false, // Default, see disclaimer
properties: {
foo: { // No bar property anymore
type: 'string',
format: 'email'
}
}
}
import { string } from '@tselect/schema';
const schema = string({ nullable: true });
Produces:
schema = {
type: ['string', 'null']
}
Any JSON schema you already wrote can be manipulated using this module.
import { object } from '@tselect/schema';
const schema = object({}, {
type: 'object',
required: ['foo'],
additionalProperties: true,
properties: {
foo: {
type: 'string',
format: 'email'
}
}
})
Produces the exact same schema :
schema = {
type: 'object',
required: ['foo'],
additionalProperties: true,
properties: {
foo: {
type: 'string',
format: 'email'
}
}
};