README
dsl-schema
So I herd U liek
JSON Schema!
dsl-schema provides simple, yet powerful API for building up your JSON Schemas 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',
})
Getting started
npm install dsl-schema # OR
yarn add dsl-schema
import like from 'dsl-schema'
const schema = like(1337)
Docs
like
like: (example: any) => Schema
- For object like
exampleit builds up aRecordof its properties - For array like
exampleit builds up aTupleof its elements - For primitive
exampleit tries to guess the best schema for it:
like.Str
Str: (opts: string) => Schema
Min and max length can be specified using opts
Str('10 > len')Str('3 < length')Str('3 < len <=10')
like.Float
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')
like.Int
Int: (opts: string) => Schema
See Float
like.req
req: (s: Schema) => TaggedSchema
Tags schema as required
like.opt
req: (s: Schema) => TaggedSchema
Tags schema as optional
like.Tuple
Tuple: (...items: TaggedSchema[]) => Schema
like.Record
Record: (props: { [key: string]: TaggedSchema }) => Schema
like.Dict
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')
like.List
List: (opts: string, itemsType: Schema) => Schema
List: (itemsType: Schema) => Schema
See Dict. Can also specify uniqueness
List('unique')List('uniq, len < 30')
like.Enum
Enum: (...items: (null | boolean | number | string)[]) => Schema
Elements of the items must be unique
like.AllOf
AllOf: (...schemas: Schema[]) => Schema
like.AnyOf
AnyOf: (...schemas: Schema[]) => Schema
like.OneOf
OneOf: (...schemas: Schema[]) => Schema
like.Not
Not: (schema: Schema) => Schema
Constant schemas
ANYBOOLNULLDATETIMEDATE_TIMEURIURI_REFERENCEURI_TEMPLATEURLEMAILHOSTNAMEIPV4IPV6REGEXUUID