6.0.3 • Published 4 years ago

tjs v6.0.3

Weekly downloads
49
License
MIT
Repository
github
Last release
4 years ago

TODO BEFORE MERGE:

  • ability to generate code using programmatic API (handy for converting old schemas)
  • allow objects to redefine keywords
  • dates (Joi.date)
  • support refs
  • resolve refs when generating
  • custom keywords
  • docs!

Typed-JSON-Schema

TypeScript-Friendly JSON-Schema Definitions

This library is able to validate JSON-Schema at runtime, and also emit type definitions for the validated data. This means that if you define your data using our JSON-schema builder, you automatically get TypeScript safety on those types.

Examples

Schema generation:

await validator.validate(string.minLength(3), 'asdf') // type: string

await validator.validate(array(string.pattern(/\w+/))) // result type: string[]

await validator.validate(object.required('a', 'b').properties({
  a: string,
  b: array(number) 
}) // result type: {a: string, b: number[] }

Validation

import { schema, string, Validator } from 'typed-json-schema'

const validator = new Validator()

const StringOrNull = schema.type(['string', 'null'])

const result = await validator.validate(StringOrNull, 'Hello'),

result.toFixed(1) // error: Property 'toFixed' does not exist on type 'string'.
result.toLowerCase() //error: object is possibly null
result && validation.result.toLowerCase() //success!

Usage

Schema Creation

In general, all JSON-Schema keywords can be used as builders on the schema object.

For example:

import { schema } from 'typed-json-schema'

const mySchema = schema
  .type('string')
  .minLength(3)
  .pattern(/regex/)

Refer to JSON-Schema keywords for a list of available keywords.

Shortcuts:

import { string, boolean, number, array, object }

const mySchema = string //same as schema.type('string')
const mySchema = number //same as schema.type('number')
const mySchema = boolean //same as schema.type('boolean')
const mySchema = array(string) //same as schema.type('array').items(string)
const mySchema = object({ a: string, b: array(number) }) //same as schema.type('object').properties({ a: string, b: array(number) })

Validation:

Use a Validator to validate a json schema:

import { schema, Validator }

const validator = new Validator({ coerceTypes: true }) //any AJV options can be supplied
const result = await validator.validate(schema.type('number')) //result is a number

see AJV for a list of options that can be passed to the validator.

6.0.3

4 years ago

6.0.1

4 years ago

6.0.2

4 years ago

6.0.0

4 years ago

5.4.0

4 years ago

5.3.0

4 years ago

5.2.0

4 years ago

5.1.0

4 years ago

5.0.11

4 years ago

5.0.10

4 years ago

5.0.9

4 years ago

5.0.8

4 years ago

5.0.7

4 years ago

5.0.6

4 years ago

5.0.5

4 years ago

5.0.4

4 years ago

5.0.3

4 years ago

5.0.2

4 years ago

5.0.1

4 years ago

5.0.0

4 years ago

2.0.0-b5

5 years ago

2.0.0-b4

5 years ago

2.0.0-b3

5 years ago

2.0.0-b2

5 years ago

2.0.0-b1

5 years ago

2.0.0-b

5 years ago

0.10.0

12 years ago

0.9.1

12 years ago

0.9.0

12 years ago

0.8.0

12 years ago

0.7.3

12 years ago

0.7.2

12 years ago

0.7.1

12 years ago

0.7.0

12 years ago

0.6.0

12 years ago

0.5.0

12 years ago

0.4.0

12 years ago

0.3.4

12 years ago

0.3.3

12 years ago

0.3.2

12 years ago

0.3.1

12 years ago

0.3.0

12 years ago

0.2.1

12 years ago

0.2.0

12 years ago

0.1.1

12 years ago

0.1.0

12 years ago