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.validatestring.minLength3, 'asdf' // type: stringawait validator.validatearraystring.pattern/\w+/ // result type: string[]await validator.validateobject.required'a', 'b'.properties// result type: {a: string, b: number[] }
Validation
result.toFixed1 // error: Property 'toFixed' does not exist on type 'string'.result.toLowerCase //error: object is possibly nullresult && validation.result.toLowerCase //success!
Usage
Schema Creation
In general, all JSON-Schema keywords can be used as builders on the schema
object.
For example:
.type'string' .minLength3 .pattern/regex/
Refer to JSON-Schema keywords for a list of available keywords.
Shortcuts:
Validation:
Use a Validator
to validate a json schema:
see AJV for a list of options that can be passed to the validator.