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: validatorStringTypeEmail 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 = ;{ let data; // We have already written it let validationSchema; // We have already done that let validation = validator; validation; console; } ;
And we're done!
To check whether there were errors with the data we do
if validationValidationResulterrors console;
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
// From the data we have worked with abovelet username = validationValidationResultusername;if username console; // ["minlength"]let age = validationValidationResultmoreage;if age console; // ["gt"]
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!
// Using the data and result from abovevalidation;console;// { email: "someone@example.com", // more: { profession: ["singing", "dancing"] }// These fields did not fail the validation
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!
// From the data above and the validation resultlet username = validation;let userAge = validation;let aboutUser = validation; console; // "Someone_else"console; // 14console; // "someone@example.com"
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 = ;{let data = email: "someone@example.com"userId: "something" let schema = name: "email" required: true stringType: validatorFieldTypeEmailname: "userId" required: true type: Number let validation = schema data;validation;let result = validationValidationResult;if resultemailconsole; // This won't work cause email is validif resultuserIdconsole; // "number" cause we expected number