simple-json-validate

1.0.5 • Public • Published

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

Readme

Keywords

Package Sidebar

Install

npm i simple-json-validate

Weekly Downloads

3

Version

1.0.5

License

WTFPL

Unpacked Size

12.7 kB

Total Files

6

Last publish

Collaborators

  • freeweed