GithubHelp home page GithubHelp logo

js-schema's Introduction

JS-SCHEMA

Build Status npm NpmLicense

JS Schema validation library, compatible with json-schema. It is more suitable for js use, pursues concise and stylish code style, make it reusable as possible.

welcome to fork,pr,issues;

try it now!

QUICK START

Install

install with npm

npm i -S '@tongchia/jsschema'

Usage

const {obj, str, num, int, date, arr, bool, buff, any} = require('jsschema');

const person = obj.properties({
  name      : str.maxLength(200).minLength(5),
  binary    : buff,
  living    : bool.default(true),
  updated   : date.default(Date.now),
  age       : int.min(0).max(65, true), // > 0 && <= 130
  email     : str.format('email'),
  birthday  : date.after('1890-01-01'),
  
  mixed     : any,
  _someId   : str.format('mongo-id'),
  _array    : arr,
  array     : [],
  _ofString : arr.items(str),
  ofString  : [str], // same as `array.items(string)` ↑
  ofNumber  : [num],
  ofDates   : [date],
  ofBuffer  : [buff],
  ofBoolean : [bool],
  ofMixed   : [any],
  ofObjectId: [str.format('mongo-id')],
  
  ofArrays  : [[]],
  ofArrayOfNumbers : [[num]],
  
  books     : [{ // => array.items(object.properties({...
    title     : str,
    author    : [str],
  }],
}).required(['name', 'age', 'birthday']);

person.isValid({
  name    : 'TongChia',
  age     : 30,
  birthday: new Date('10/18/1987')
}, (err) => {
  assert.ifError(err);
});

// to json-schema;
console.log(JSON.stringify(person))

VALIDATE

  • string
    • enum
    • pattern
    • minLength
    • maxLength
    • format
      • * from chriso/validator.js
        • alpha
        • email
        • url
        • base64
        • hex-color
        • md5
        • mongo-id
        • uuid
        • ip (ipv4, ipv6)
        • json
        • ...
      • data-time (full-date, full-time)
      • hostname
      • uri, iri
      • uri-template
      • regexp
    • String-Encoding Non-JSON Data
  • number (integer)
    • enum
    • minimum, maximum
    • exclusiveMinimum, exclusiveMaximum
    • min, max (alias to minimum, maximum, exclusiveMinimum, exclusiveMaximum)
    • range (alias to min, max)
    • integer
    • multipleOf
    • converter (numeric)
  • date (js-schema)
    • after
    • before
    • converter (date-time, full-date, date-string, time-stamp)
  • array
    • minItems
    • maxItems
    • unique (uniqueItems)
    • items
    • additionalItems
    • contains
    • entries ✨
    • Overload function ✨
  • object
    • properties
    • required
    • patternProperties
    • additionalProperties
    • size (minProperties, maxProperties)
    • propertyNames
    • dependencies
      • schema dependencies
  • buffer (js-schema)
    • converter (strings, base64)
  • null (nil)
  • boolean
  • function (js-schema)
  • any
    • allOf
    • anyOf
    • oneOf
    • not
  • Constant values
  • Enumerated values
  • Metadata
    • title
    • description
    • default
    • examples
  • json-schema
    • generate json-schema
    • parse json-schema
      • Combining schemas ⚡️
  • referenced schema
    • $id ⚡️
    • $ref ⚡️
    • resolve method (browser & nodeJs) ⚡️

Custom validate

const {string} = require('jsschema');

string.addValidate(
  'keyword', // the keyword
  {
    validator: (value, params) => check(value, parameter), // return true/false;
    message: '`{value}` should valid for schema:{type}.{keyword}({params})',
  }
)

string.addValidate(
  'keyword',
  {
    isAsync: true,
    validator: (value, params, callback) => {
      // check value
      if ('ok')
        return callback(null, value)
      return callback(new ValidationError('...'))
    }
  }
)

TODO

  • custom validate;
  • test report;
  • automatic publish;
  • browser test;
  • toModel
    • MobX;
    • MongoDB;
      • mQuery;
  • mongoose schema;
  • json-schema;
    • parse json-schema;
    • generate json-schema;
    • test with ajv;
  • google protocol-buffers;
  • GraphQL;

js-schema's People

Contributors

tongchia avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

js-schema's Issues

modify schema properties

schema1 = str.max(4).min(1);
schema2 = schema1.max(10);
schema2.isVaild('hello'); //=> false;

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.