JSON data validator
Validating JSON data
If you have a json data, let's say from a POST request in your nodejs server and want to ensure that the user provided valid data, you can validate it using
body-validator
Here's the json data from the client or somewhere else in your app
let userData = {
username: "Someone",
email: "someone@example.com"
more: { age: 12, profession: ["singing", "dancing"] }
}
Now, concerning this data we want to ensure
- username is provided and minimum length is 12 and max length is 24
- email is valid and provided
- there is more information with age and profession
- age should be greater than 13 but less than 30
- profession might not be provided but should be greater than 1, less than 5
Here's what the validation schema looks like
let validationSchema = [
{ name: "username", minlength: 12, maxlength: 24, required: true },
{ name: "email", stringType: validator.StringType.Email, required: true },
{ name: "more", value: [
{ name: "age", lt: 30, gt: 13 },
{ name: "profession", minlength: 1, maxlength: 5 }
]
]
To validate our data we do
let validator = require("body-validator");
function main(){
let data;
let validationSchema;
let validation = validator.DataValidator(validationSchema, data);
validation.Validate();
console.log(validation.ValidationResult);
}
main();
And we're done!
To check whether there were errors with the data we do
if (validation.ValidationResult.errors){
console.log("Shit!");
}
The returned validation result is a JSON data. What does that mean? You can check the feilds that failed like looking up a dictionary
Fields that passed the validation test won't appear in the validation result
let username = validation.ValidationResult.username;
if (username){
console.log(username);
}
let age = validation.ValidationResult.more.age;
if (age){
console.log(age);
}
Sanitizing JSON data
To sanitize the provided JSON data means to create a new data object using the validation result
What does this mean? It will create a new JSON object populated with the fields that did not fail the validation process
This means that if you still want to use the data even if the user provided invalid data, you can!
Let's do it!
validation.Sanitize();
console.log(validation.CleanData);
Getting JSON data
It's very clear from the result above that the user provided bad data. But we still have default values in store that would do the trick
Or maybe the user didn't provide some required fields, but you don't wan't those fields to be empty in your database. Guess what? You can provide default values
Let's see it!
let username = validation.GetValue("username", "Someone_else");
let userAge = validation.GetValue("more.age", 14);
let aboutUser = validation.GetValue("email", "any.user@example.com");
console.log(username);
console.log(userAge);
console.log(aboutUser);
More
NOTE: When writing the validation schema, the definition for one object key-value pair is called a field
Other validation that can be used for a field:
- name // The name of the key of a key-value pair to validate
- required // Is the value required?
- minlength // When it's a string or array, the minimum length it should be
- maxlength // When it's a string or array, the maximum length it should be
- lt // When it's a number, the maximum
- gt // When it's a number, the minimum
- regex // When it's a string, your regex validation schema
- type // This checks that the value provided should be either: String, Number, List, Object, Float
- stringType // This checks that the string value provided should either be: Alpha, Numeric, AlphaNumeric, Date, Email
- value // A sub object that should have any of the above listed validation field schema
This is how to use the type
and stringType
let validator = require("body-validator");
function main(){
let data = {
email: "someone@example.com",
userId: "something"
}
let schema = [
{name: "email", required: true, stringType: validator.FieldType.Email}
{name: "userId", required: true, type: Number }
]
let validation = new validator.DataValidator(schema, data);
validation.Validate();
let result = validation.ValidationResult;
if (result.email){
console.log(result.email);
}
if (result.userId){
console.log(result.userId);
}
Contribute
body-validator on Github
Licence
BSD-3-Clause