Coder Social home page Coder Social logo

schema-in-js's Introduction

schema-in-js

Build Status Coverage Downloads Downloads npm version dependencies dev dependencies License

Write json schema fast in js.

In Developing! Every version may have breaking change!

Why?

  • JSON is tedious and error prone.
  • JSON schema is tedious and error prone.

Install

npm install schema-in-js

Usage

Suppose we want to write json schema for a entity called user which has username, password, age, gender, province and city. City can be provided only if province exsits.

If wrote in json schema, the result is:

{
    "type": "object",
    "description": "User",
    "properties": {
        "username": {
            "type": "string",
            "maxLength": 20,
            "minLength": 4
        },
        "password": {
            "type": "string",
            "pattern": "^(?:\\d+|[a-zA-Z]+|[!@#$%^&*]+)$"
        },
        "age": {
            "type": "integer",
            "max": 100,
            "min": 16
        },
        "gender": {
            "enum": ["male", "female"]
        },
        "province": {
            "type": "string",
            "maxLength": 10
        },
        "city": {
            "type": "string",
            "maxLength": 10
        }
    },
    "required": ["username", "password"],
    "dependencies": {
        "city": ["province"]
    }
}

With schema in js, the result is:

import {default as sj, description, dependencies} from 'schema-in-js';
export const userSchema = {
    [description]: 'User',
    username: sj.str.length(10, 4),
    password: sj.str.pattern('^(?:\\d+|[a-zA-Z]+|[!@#$%^&*]+)$'),
    age: sj.int.range(100, 16).mayBe(),
    gender: sj.enum('male', 'female').mayBe(),
    province: sj.str.length(10).mayBe(),
    city: sj.str.length(10).mayBe(),
    [dependencies]: {city: ['province']}
};

We can tranform the schema wrote by schema-in-js to rightful json schema through sj.tranformToJSONSchema.

Core apis

Module entry

default

Instance of SchemaInJS.

SchemaInJS apis
  • tranformToJSONSchema: Function

object relevant keywords

  • id
  • title
  • description
  • dependencies
  • additionalProperties
  • patternProperties
  • maxProperties
  • minProperties

Builtin types

  • Str
  • Obj
  • Num
  • Int
  • Arr
  • Bool

Combining keywords

In developing.

TODO

JSON Schema keywords to support.

  • allOf
  • anyOf
  • oneOf
  • not

Features

  • Type customization.
  • Extensional keyword.

License

MIT

schema-in-js's People

Contributors

yanghuabei avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  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.