So I herd U liek
JSON Schema
!
dsl-schema
provides simple, yet powerful API for building up your JSON Schema
s based on examples you provide.
import like from 'dsl-schema'
const mySchema = like({
foo: 'some string',
baz: ['yup'],
// Can be nested for more fine-grained control!
here: like.Enum('come dat boi', 'I come'),
})
// true
ajv.validate(mySchema, {
foo: 'bar',
baz: ['fizz buz'],
here: 'I come',
})
// false
ajv.validate(mySchema, {
not: 'valid',
})
npm install dsl-schema # OR
yarn add dsl-schema
import like from 'dsl-schema'
const schema = like(1337)
like: (example: any) => Schema
- For object like
example
it builds up aRecord
of its properties - For array like
example
it builds up aTuple
of its elements - For primitive
example
it tries to guess the best schema for it:
Str: (opts: string) => Schema
Min and max length can be specified using opts
Str('10 > len')
Str('3 < length')
Str('3 < len <=10')
Float: (opts: string) => Schema
Min, max, and multipleOf can be specified using opts
Float('1.3 < x')
Float('1.3 < x <= 6')
Float('n*0.1')
Float('1.3 < x <= 6, n*2.6')
Int: (opts: string) => Schema
See Float
req: (s: Schema) => TaggedSchema
Tags schema as required
req: (s: Schema) => TaggedSchema
Tags schema as optional
Tuple: (...items: TaggedSchema[]) => Schema
Record: (props: { [key: string]: TaggedSchema }) => Schema
Dict: (opts: string, itemsType: Schema) => Schema
Dict: (itemsType: Schema) => Schema
Size of the dictionary can be specified using opts
Dict('size <= 10')
Dict('length > 10')
Dict('30 > len > 10')
List: (opts: string, itemsType: Schema) => Schema
List: (itemsType: Schema) => Schema
See Dict
. Can also specify uniqueness
List('unique')
List('uniq, len < 30')
Enum: (...items: (null | boolean | number | string)[]) => Schema
Elements of the items
must be unique
AllOf: (...schemas: Schema[]) => Schema
AnyOf: (...schemas: Schema[]) => Schema
OneOf: (...schemas: Schema[]) => Schema
Not: (schema: Schema) => Schema
ANY
BOOL
NULL
DATE
TIME
DATE_TIME
URI
URI_REFERENCE
URI_TEMPLATE
URL
EMAIL
HOSTNAME
IPV4
IPV6
REGEX
UUID