json-validator-ts

0.0.6 • Public • Published

# JsonValidator

Overview

JsonValidator currently available for typescript based application. It will be validate any level of JSON, JSON Array and Complex JSON . You need to follow some instruction and check out following example which help you. JsonValidator ease to use, easy to insatll. It's cover all kinds of validation rules and pre define rules. Test Case also there which help to you for understanding working process.

Installing

npm install json-validator-ts

Key Points

Step-:1 run cmd npm install json-validator-ts

Step-:2 import * as validation from 'json-validator-ts'.

Step-:3 create json schema with validation rules. See Example.

Step-:4 create modal object, pass json object with related schema and ErrorMessageSetting option.

Step-:5 call modeal object with validate() function wchich return true or false. See Example.

Note-: Error property must be declare with all property which you want to validate.

Validation Rules

Rule Name Value Type Declaration Description
Type String, Float, Int, Array, Number, Date { Type : "String" } validate data type
Required true, false { Required : true } validate data not null
Exact_length Number(10)...etc { Exact_length : 10 } validate data characters exact length
Greater_than Number(10)...etc { Greater_than : 10 } validate data(number, int and float) greater than 10
Less_than Number(10)...etc { Less_than : 10 } validate data(number, int and float) less than 10
Matches Regular Expression { Matches : /^[A-Za-z0-9]+$/g } validate data according regx expression
Max_length Number(15)...etc { Max_length : 15 } validate data characters max length
Min_length Number(15)...etc { Min_length : 15 } validate data characters min length
Custom_validation Regular Expression { Custom_validation : /^[A-Za-z0-9]+$/g } validate data according regx expression
Apha true, false { Apha : true } validate data according regx /^[a-z]+$/i
Alpha_dash true, false { Alpha_dash : true } validate data according regx /^[a-z0-9_-]+$/i
Alpha_numeric true, false { Alpha_numeric : true } validate data according regx /^[a-z0-9_-]+$/i
Decimal true, false { Decimal : true } validate data according regx /^[a-z0-9]+$/i
Integer true, false { Integer : true } validate data according regx /^-?[0-9]+$/
Is_natural true, false { Is_natural : true } validate data according regx /^[0-9]+$/i
Is_natural_no_zero true, false { Is_natural_no_zero : true } validate data according regx /^[1-9][0-9]*$/i
Numeric true, false { Numeric : true } validate data according regx /^[0-9]+$/
Valid_base64 true, false { Valid_base64 : true } validate data according regx /[^a-zA-Z0-9/+=]/i
Valid_ca_zip true, false { Valid_ca_zip : true } validate data according regx /^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$/
Valid_email true, false { Valid_email : true } validate data according regx /\w+([-+.']\w+)@\w+([-.]\w+).\w+([-.]\w+)*$/
Valid_ip true, false { Valid_ip : true } validate data according regx /^((25[0-5]
Valid_url true, false { Valid_url : true } validate data according regx /^((http
Valid_phoneno true, false { Valid_phoneno : true } validate data according regx /^(\+)
Valid_credit_card true, false { Valid_credit_card : true } validate data according regx /^[\d-\s]+$/
Date_range {from : "mm/dd/yyyy" , to : "mm/dd/yyyy"} { Date_range : {from : "mm/dd/yyyy" , to : "mm/dd/yyyy"} } validate date range between two date
Post String { Post : "Any Strung" } post string will be add in last of data string
Pre String { Pre : "Any String" } pre string will be add in top of data string
Error String { Error : "Error Message" } error message will be show when validation rules will be fail in Array Object
Trim true, false { Trim : true }
DobToAge {max_age : "mm/dd/yyy", min_age : "mm/dd/yyyy", message : "Happy birthday"} DobToAge : {formate : "mm/dd/yyy", max_age : "mm/dd/yyy", min_age : "mm/dd/yyyy", message : "Happy birthday"} validate to age, calculate current age from date of birth and check current date is birthday then show message.

Error Message Formate Setting

x-> y -> z

ErrorMessageSetting.ParentKeyWithStringFormatError Multi-Level json and there parent key will be show in singal string but it separate with dote"." see example [{ key_name : "x.y.z", message : "invalid", isError : false }]

ErrorMessageSetting.SingalKeyError
Multi-Level json and there parent key will be remove. It will show only 'key name'. see example [{ key_name : "z", message : "invalid", isError : false }]

ErrorMessageSetting.ParentKryWithJsonFromatError Multi-Level json and there parent key will be there. It will show all level json parent key with 'key name'. see example [{ z : { y : { key_name : "z", message : "invalid", isError : false } } }]


Example

"#key" replace with property name.

case -: 1 simple json data Type

        let _schema = new validation.JsonValidation.Schema({
            name: { Type: "String", Error: " #key is invalid." },
            age: { Type: "Number", Error: " #key is invalid." },
            dob: { Type: "Date", Error: " #key is invalid."},
            salary: { Type: "Int", Error: " #key is invalid."},
            temp: { Type: "Float", Error: " #key is invalid." }
        }, validation.JsonValidation.ErrorMessageSetting.ParentKeyWithStringFormatError);
        let _jsonData = {
            name: "ABCD",
            age: 5,
            dob: "10/24/2018",
            salary: 60000,
            temp: 32.5
        };
        let _modal = new validation.JsonValidation.Model(_jsonData, _schema);
        let result = _modal.validate();
        result.isValid;//true|| false
case -: 2 float data type check

        let _schema = new validation.JsonValidation.Schema({
            name: { Type: "String", Error: " #key is invalid." },
            age: { Type: "Number", Error: " #key is invalid." },
            dob: { Error: " #key is invalid.", Type: "Date", DobToAge: { formate:"mm/dd/yyyy", max_age:70, min_age: 18, message: "happy birthday to you" } },
            salary: { Type: "Int", Error: " #key is invalid."},
            temp: {
                Type: "Float", Required: true, Error: " #key is invalid."
            }
        }, validation.JsonValidation.ErrorMessageSetting.ParentKeyWithStringFormatError);
        let _jsonData = {
            name: "nitesh jain",
            age: 675,
            dob: "7/2/1998",
            salary: 7665,
            temp: 74567.46
        };
        let _modal = new validation.JsonValidation.Model(_jsonData, _schema);
        let result = _modal.validate();
        result.isValid;//true|| false
case -: 3 type string with required rule.

        let _schema = new validation.JsonValidation.Schema({
            name: { Type: "String", Required: true, Error: " #key is invalid." },
            age: { Type: "Number", Error: " #key is invalid."},
            dob: { Type: "Date", Error: " #key is invalid."},
            salary: { Type: "Int", Error: " #key is invalid."},
            temp: { Type: "Float", Error: " #key is invalid."}
        }, validation.JsonValidation.ErrorMessageSetting.ParentKeyWithStringFormatError);
        let _jsonData = {
            name: " ",
            age: 5,
            dob: "10/24/2018",
            salary: 60000,
            temp: 32.5
        };
        let _modal = new validation.JsonValidation.Model(_jsonData, _schema);
        let result = _modal.validate();
        result.isValid;//true|| false
case -: 4 type date is accepting in number formate

        let _schema = new validation.JsonValidation.Schema({
            name: { Type: "String", Error: " #key is invalid."},
            age: { Type: "Number", Error: " #key is invalid." },
            dob: { Type: "Date", Error: " #key is invalid."},
            salary_date: { Type: "Date", Error: " #key is invalid." },
            temp: { Type: "Float", Error: " #key is invalid." }
        }, validation.JsonValidation.ErrorMessageSetting.ParentKeyWithStringFormatError);
        let _jsonData = {
            name: "Nitesh Jain",
            age: 7,
            dob: "10/24/2018", 
            salary_date: 5667,      //number will be convert into date 
            temp: 32.5
        };
        let _modal = new validation.JsonValidation.Model(_jsonData, _schema);
        let result = _modal.validate();
        result.isValid;//true|| false
case -: 5 type date with alphabate

        let _schema = new validation.JsonValidation.Schema({
            name: { Type: "String", Error: "" },
            age: { Type: "Number", Error: "" },
            dob: { Type: "Date", Error: "" },
            salary_date: { Type: "Date", Error: "" },
            temp: { Type: "Float", Error: ""}
        }, validation.JsonValidation.ErrorMessageSetting.ParentKeyWithStringFormatError);
        let _jsonData = {
            name: "Nitesh Jain",  //check
            age: 6,
            dob: "10/24/2018",  //check
            salary_date: "jhgdsfgj",      //check
            temp: 32.5          //check
        };
        let _modal = new validation.JsonValidation.Model(_jsonData, _schema);
        let result = _modal.validate();
        result.isValid;//true|| false
case -: 6 complex and multi level json schema and data

        let _schema = new validation.JsonValidation.Schema({
            name: { Type: "String", Error: " #key is invalid."},
            age: { Type: "Number", Error: " #key is invalid." },
            dob: {  Error: "",Type: "Date", DobToAge: { formate: "mm/dd/yyyy", max_age: 70, min_age: 18, message: "happy birthday to you" } },
            salary: { Type: "Int", Error: " #key is invalid."},
            temp: {
                Type: "Float", Required: true, Error: " #key is invalid."
            },
            ab1: { Type: "String", Error: " #key is invalid." },
            degree: [{
                "a": { Type: "String", Required: true, Error: " #key is invalid." },
                "ab": { Type: "Number", Required: true, Error: " #key is invalid." }
            }],
            collage: {
                teacher: {
                    classTeacher: { Type: "String", Required: true, Error: " #key is invalid." }
                },
                "ayub": { Type: "String", Required: true, Error: " #key is invalid." }
            }
        }, validation.JsonValidation.ErrorMessageSetting.ParentKeyWithStringFormatError);
        let _jsonData = {
            name: "fgjf",
            age: 5,
            dob: "07/03/1998",
            salary: 7645,
            temp: 32343,
            ab1: ["vjhghjg", "fjfhfj", "dgf"],
            degree: [{
                "a": "hvgjgjgj"
                , "ab": [736478,742567,73485687]
            }, {
                "a": "hvgjgjgj"
                , "ab": [89567895,7436,834568]
            }],
            collage: {
                teacher: { classTeacher: "hfgsdgjf" },
                "ayub": ["hjdsagf", "hjgjgj"]
            }
        };
 
        let _modal = new validation.JsonValidation.Model(_jsonData, _schema);
        let result = _modal.validate();
        result.isValid;//true|| false

Help

please email-: jainnitesh1987@gmail.com

Readme

Keywords

none

Package Sidebar

Install

npm i json-validator-ts

Weekly Downloads

1

Version

0.0.6

License

none

Unpacked Size

188 kB

Total Files

12

Last publish

Collaborators

  • niteshjain