目录

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

关于
101.0 KB
邀请码
    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

版权所有:中国计算机学会技术支持:开源发展技术委员会
京ICP备13000930号-9 京公网安备 11010802032778号