1.0.1 • Published 6 years ago
dsl-schema v1.0.1
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-schemaimport 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) => SchemaMin and max length can be specified using opts
Str('10 > len')Str('3 < length')Str('3 < len <=10')
like.Float
Float: (opts: string) => SchemaMin, 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) => SchemaSee Float
like.req
req: (s: Schema) => TaggedSchemaTags schema as required
like.opt
req: (s: Schema) => TaggedSchemaTags schema as optional
like.Tuple
Tuple: (...items: TaggedSchema[]) => Schemalike.Record
Record: (props: { [key: string]: TaggedSchema }) => Schemalike.Dict
Dict: (opts: string, itemsType: Schema) => Schema
Dict: (itemsType: Schema) => SchemaSize 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) => SchemaSee Dict. Can also specify uniqueness
List('unique')List('uniq, len < 30')
like.Enum
Enum: (...items: (null | boolean | number | string)[]) => SchemaElements of the items must be unique
like.AllOf
AllOf: (...schemas: Schema[]) => Schemalike.AnyOf
AnyOf: (...schemas: Schema[]) => Schemalike.OneOf
OneOf: (...schemas: Schema[]) => Schemalike.Not
Not: (schema: Schema) => SchemaConstant schemas
ANYBOOLNULLDATETIMEDATE_TIMEURIURI_REFERENCEURI_TEMPLATEURLEMAILHOSTNAMEIPV4IPV6REGEXUUID