1.3.2 ā€¢ Published 3 years ago

jsonschema-definer v1.3.2

Weekly downloads
613
License
ISC
Repository
github
Last release
3 years ago

This package provides simple, well typed API for creating and validating JSON Schemas

šŸ”„ Install

npm install jsonschema-definer

šŸ‘Œ Usage

This package was inspired by fluent-schema and prop-types, and is used to create and validate JSON Schema. It was written in typescript and provide a lot of usefull info from typings, such as infering interface types from schema. Here is an example:

import S from 'jsonschema-definer'

// Lets define a simple object schema
const UserSchema = S.shape({
  name: S.string(),
  email: S.string().format('email').optional(),
  password: S.string().minLength(8),
  role: S.enum('client', 'suplier'),
  birthday: S.instanceOf(Date)
})

// Now lets get interface of User from schema
type User = typeof UserSchema.type
/*
  type User = {
    name: string,
    email?: string | undefined,
    password: string,
    role: 'client' | 'suplier',
    birthday: Date
  }
*/

// We can validate user using .validate(data) function (ajv used)
const [valid, errors] = UserSchema.validate({
  name: 'Igor',
  email: 'fumo.sujimoshi@gmail.com',
  password: '12345678',
  role: 'client',
  birthday: new Date()
})

console.log(valid, errors) // [boolean, Error[]]

// Or get plain JSON Schema using .valueOf()
console.log(UserSchema.valueOf())

ā­ļø Show your support

Give a ā­ļø if this project helped you!

šŸ“š Documentation

Full documentation available here

Main exported variable S: SchemaFactory extends BaseSchema

šŸ¤ Contributing

Contributions, issues and feature requests are welcome!Feel free to check issues page.

Run tests

npm run test

Author

šŸ‘¤ Igor Solomakha fumo.sujimoshi@gmail.com

šŸ“ License

Copyright Ā© 2020 Igor Solomakha <fumo.sujimoshi@gmail.com>. This project is ISC licensed.


This README was generated with ā¤ļø by readme-md-generator