simple-json-validate
tool for validate json input
Last updated 2 years ago by freeweed .
WTFPL · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install simple-json-validate 
SYNC missed versions from official npm registry.

SIMPLE-JSON-VALIDATE

tool for check json input and set json data.

NOTE: This still beta version. Feel free to report bug thank!

Getting Started

Installing

npm install simple-json-validate --save

Usage

const validate = require('simple-json-validate');
const pattern = {
    "test": {
        "id": "required|number|max_length[13]|min_length[13]",
        "email": "required|email",
        "period": "default[30]",
        "value": "required|limit[100]"
    }
};
let input = {
    "id": "1111111111111",
    "email": "freeweed.m@gmail.com",
    "value": "10000"
}

let validated = new validate(pattern);
let output = validated.check('test', input);
console.log(output);//{"id":"1111111111111","email":"freeweed.m@gmail.com","value":"100","period":"30","isValid":true}

How To use

1. Require SIMPLE-JSON-VALIDATE

const validate = require('simple-json-validate');

2. Setup Your Json Pattern

  var pattern = {
    "first pattern name": {
      "json key": "setup format"
    },
    "second pattern name": {
        "json key": "setup format"
    }
  }
  new validate(pattern);

3. Call Check Function

   let output = validated.check("pattern name", input)
    NOTE: input can be only JSON Object

4. Check Your Input Is Valid Or Not

   if(output.isValid){
       //awesome code here
   }

The Format

You can use multiple format with | between format such as

"id": required|number|max_length[13]|min_length[13]

Avaliable Format

required

data is required

number

accept only number

email

accept only email

default[...]

set default data (This option will use only when input data is invalid or null. replace [...] with default data such as default[30])

avaliable[...]

set data that can be accepted (replace [...] with accepted data such as avaliable[banana, weed])

limit[...]

what ever input data will set to limit data (replace [...] with limit data such as limit[100])

max_length[...]

maximum length of data can be accepted (replace [...] with interger such ass max_length[13])

min_length[...]

minimum length of data can be accepted (replace [...] with interger such as min_length[13])

max_value[...]

maximum value of data can be accepted (replace [...] with interger such as man_value[10])

min_value[...]

minimum value of data can be accepted (replace [...] with interger such as min_value[1])

Authors

freeweed

License

This project is licensed under the WTFPL License - see the LICENSE.md file for details

Changelog

[1.0.3]

Fixed

  • some stupid bug

[1.0.2]

Fixed

  • validate number bug

TODO

  • add nested child validate
  • add unit test
  • improve performance
  • add more type

Current Tags

  • 1.0.5                                ...           latest (2 years ago)

6 Versions

  • 1.0.5                                ...           2 years ago
  • 1.0.4                                ...           2 years ago
  • 1.0.3                                ...           2 years ago
  • 1.0.2                                ...           2 years ago
  • 1.0.1                                ...           3 years ago
  • 1.0.0                                ...           3 years ago
Maintainers (1)
Downloads
Today 0
This Week 1
This Month 2
Last Day 1
Last Week 1
Last Month 7
Dependencies (0)
None
Dev Dependencies (0)
None
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |