GithubHelp home page GithubHelp logo

typed-json-schema's Introduction

Typed-JSON-Schema


Version

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.

typed-json-schema's People

Contributors

sberan avatar calebboyd avatar cah-sam-beran avatar

Stargazers

Simon Sturmer avatar bitlab avatar Geoff Goodman avatar Sam Wolstencroft avatar yinz avatar Daniel Yogel avatar İsmail Codar avatar Andrejs Agejevs avatar Kasper Isager Dalsgarð avatar Tomasz Cudok avatar Lars Vierbergen avatar Sam Wolstencroft avatar Maria Bitsch avatar Kyle Lacy avatar Sebastian Lasse avatar Kyle Dunn avatar  avatar Ben Oakes avatar  avatar

Watchers

 avatar James Cloos avatar  avatar Kyle Dunn avatar  avatar

Forkers

calebboyd

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.