-
Notifications
You must be signed in to change notification settings - Fork 2
Validating payload
To validate the payload that comes in via POST or PUT methods, you can use a validation library such as joi. You would first import joi at the top of your route file and then define the validation schema for the payload. For example, you can define a schema for a payload that includes a "username" and "password" field like this:
const joi = require('joi');
module.exports = {
contentType: 'application/json',
name: 'Create user',
body: {
model: joi.object({
username: joi.string().required(),
password: joi.string().required()
}).required()
},
handler: (req, res) => {
// your logic here
}
}
Validation, especially by defining a body model, is important because it ensures that the data received from the user is in the correct format and contains all the necessary information. This helps to prevent errors and inconsistencies in the data, making it easier to work with and increasing the overall reliability of the application. It can also improve the application's security by checking for malicious input and preventing potential attacks. Overall, validation can greatly improve the quality and maintainability of the code, making it a best development practice.
Copyright © 2016-2022, Skitsanos™