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
npm install json-validator-tsJsonValidator 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.
npm install json-validator-ts
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.
Type | String, Float, Int, Array, Number, Date | { Type : "String" } | validate data type |
Required | true, false | { Required : true } | validate data not null || "" || undefined |
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]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})$/ |
Valid_url | true, false | { Valid_url : true } | validate data according regx /^((http|https):\/\/(\w+:{0,1}\w*@)?(\S+)|)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!-\/]))?$/ |
Valid_phoneno | true, false | { Valid_phoneno : true } | validate data according regx /^(\\+)|(9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|4[987654310]|3[9643210]|2[70]|7|1)\d{1,14}$/ |
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. |
ErrorMessageSetting.ParentKeyWithStringFormatError
ErrorMessageSetting.SingalKeyError
ErrorMessageSetting.ParentKryWithJsonFromatError
javascript
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
---
`javascript
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.
---
`javascript
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
---
`javascript
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
---
`javascript
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
---
`javascript
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
``