Giter Site home page Giter Site logo

jsonschema-definer's Introduction

Welcome to jsonschema-definer ๐Ÿ‘‹

Version License: ISC Release License: ISC

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: '[email protected]',
  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

Method Description JSON Schema
S.any(): BaseSchema Correspond to any type { }
S.string(): StringSchema For strings validation { "type": "string" }
S.number(): NumericSchema For float/integer validation { "type": "number" }
S.integer(): NumericSchema For integer values validation { "type": "integer" }
S.boolean(): BaseSchema For boolean values { "type": "boolean" }
S.null(): BaseSchema For null value validation { "type": "null" }
S.array(): ArraySchema Array validation { "type": "array" }
S.list(itemType: T): ArraySchema Validation of lists. Example: S.list(S.string()): ArraySchema { "type": "array", "items": { ... } }
S.object(): ObjectSchema Validation of object { "type": "object" }
S.shape({ key: Schema }: T): ObjectSchema Validation of objects { "type": "object", properties: T, additionalProperties: false } }
S.instanceOf(type: T): BaseSchema For validating instanceOf data. (Custom keyword used) { instanceOf: T.name }
S.enum(...constants: T[]): BaseSchema Enumerable schema { enum: [ T[0], T[1] ] }
S.const(constant: T): BaseSchema Constant value { const: T }
S.anyOf(...schemas: BaseSchema[]): BaseSchema Any (one or more) of given types { anyOf: [ T[0], T[1], ... ] }
S.oneOf(...schemas: BaseSchema[]): BaseSchema Value shoud correspond to ONE of given types { oneOf: [ T[0], T[1], ... ] }
S.allOf(...schemas: BaseSchema[]): BaseSchema Value should correspond to ALL of given type { allOf: [ T[0], T[1], ... ] }
S.raw(values: any): BaseSchema Set custom schema values (For Swagger definitions for example) { ...values }
S.custom(...validators: (value: T) => boolean): BaseSchema Add custom validation functions to schema. Supported by AJV custom keyword Does not supported by standard JSON Schema (Ajv support)

๐Ÿค Contributing

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

Run tests

npm run test

Author

๐Ÿ‘ค Igor Solomakha [email protected]

๐Ÿ“ License

Copyright ยฉ 2020 Igor Solomakha [email protected].
This project is ISC licensed.


This README was generated with โค๏ธ by readme-md-generator

jsonschema-definer's People

Contributors

sujimoshi avatar dependabot[bot] avatar

Watchers

James Cloos avatar

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.