0.0.6 • Published 7 years ago

json-validator-ts v0.0.6

Weekly downloads
18
License
-
Repository
-
Last release
7 years ago

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 NameValue TypeDeclarationDescription
TypeString, Float, Int, Array, Number, Date{ Type : "String" }validate data type
Requiredtrue, false{ Required : true }validate data not null""undefined
Exact_lengthNumber(10)...etc{ Exact_length : 10 }validate data characters exact length
Greater_thanNumber(10)...etc{ Greater_than : 10 }validate data(number, int and float) greater than 10
Less_thanNumber(10)...etc{ Less_than : 10 }validate data(number, int and float) less than 10
MatchesRegular Expression{ Matches : /^A-Za-z0-9+$/g }validate data according regx expression
Max_lengthNumber(15)...etc{ Max_length : 15 }validate data characters max length
Min_lengthNumber(15)...etc{ Min_length : 15 }validate data characters min length
Custom_validationRegular Expression{ Custom_validation : /^A-Za-z0-9+$/g }validate data according regx expression
Aphatrue, false{ Apha : true }validate data according regx /^a-z+$/i
Alpha_dashtrue, false{ Alpha_dash : true }validate data according regx /^a-z0-9_-+$/i
Alpha_numerictrue, false{ Alpha_numeric : true }validate data according regx /^a-z0-9_-+$/i
Decimaltrue, false{ Decimal : true }validate data according regx /^a-z0-9+$/i
Integertrue, false{ Integer : true }validate data according regx /^-?0-9+$/
Is_naturaltrue, false{ Is_natural : true }validate data according regx /^0-9+$/i
Is_natural_no_zerotrue, false{ Is_natural_no_zero : true }validate data according regx /^1-9*$/i
Numerictrue, false{ Numeric : true }validate data according regx /^0-9+$/
Valid_base64true, false{ Valid_base64 : true }validate data according regx /^a-zA-Z0-9\/+=/i
Valid_ca_ziptrue, false{ Valid_ca_zip : true }validate data according regx /^ABCEGHJKLMNPRSTVXY{1}\d{1}A-Z{1} *\d{1}A-Z{1}\d{1}$/
Valid_emailtrue, false{ Valid_email : true }validate data according regx /\w+(-+.'\w+)@\w+(-.\w+).\w+(-.\w+)*$/
Valid_iptrue, false{ Valid_ip : true }validate data according regx /^((250-520-410-9{2}0-9{1,2}).){3}(250-520-410-9{2}0-9{1,2})$/
Valid_urltrue, false{ Valid_url : true }validate data according regx /^((httphttps):\/\/(\w+:{0,1}\w*@)?(\S+))(:0-9+)?(\/\/(\w#!:.?+=&%@!-\/))?$/
Valid_phonenotrue, false{ Valid_phoneno : true }validate data according regx /^(\+)(9976\d8987530\d6987\d590\d42\d3875\d298654321\d98543210864216654321058765432149876543103964321027071)\d{1,14}$/
Valid_credit_cardtrue, 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
PostString{ Post : "Any Strung" }post string will be add in last of data string
PreString{ Pre : "Any String" }pre string will be add in top of data string
ErrorString{ Error : "Error Message" }error message will be show when validation rules will be fail in Array Object
Trimtrue, 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

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago