validate-fields-body
Advanced tools
Weekly downloads
Readme
This is a JavaScript function that validates a given form data object against a set of validation keys. The function returns an array with three elements:
npm i validate-fields-body
The first element is a boolean value that indicates whether there are any validation errors or not. The second element is an array of strings that contains the error messages for each validation failure, or null if there are no errors. The third element is an object that contains only the valid fields from the form data, or null if there are any errors. The function performs three types of validation:
Required fields: The validation keys that have a “!” prefix indicate that the corresponding field in the form data must be present and not empty. For example, “!name” means that the name field is required. Required types: The validation keys that have a “:” suffix indicate that the corresponding field in the form data must have a specific type. For example, “age:number” means that the age field must be a number. Matching keys: The validation keys that do not have any prefix or suffix indicate that the corresponding field in the form data must exist. For example, “email” means that the email field must be present. The function loops through each validation key and checks if it matches with the form data. If not, it adds an error message to the logs array and skips to the next key. If all keys are validated successfully, it returns false for errors and copies only the valid fields to the result object.
Here is an example of how to use this function:
const { useValidate } = require("validate-fields-body");
const register = (req, res) => {
// Set your fields to validate
const fieldsValidate = ["username", "email", "password", "age", "skills"];
const [isValid, logs, result] = useValidate(fieldsValidate, req.body);
if (isValid) return res.status(403).json({ success: false, message: logs });
return res.status(201).send({ success: true, data: result });
};
const fieldsValidate = ["!username", "!email", "!password", "!age", "skills"];
const fieldsValidate = [
"!username:string",
"!email:string",
"!password:string",
"!age:number",
"skills:object",
];
const fieldsValidate = ["password*3-15"];
const fieldsValidate = ["email@"];
Option | Keyword |
---|---|
! | Required Value |
:[TYPE] | Check Type |
@ | Validate Emai |
*[min-max] | Validate Length |
Thanks you!.
USDT Address
0x92072439f5cf2f35defa28a4915004971de543b0
Author by: Nero Sengtianthr.
FAQs
Validate-Fields-Body is a Javascript library for dealing with validation.
The npm package validate-fields-body receives a total of 17 weekly downloads. As such, validate-fields-body popularity was classified as not popular.
We found that validate-fields-body demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket installs a Github app to automatically flag issues on every pull request and report the health of your dependencies. Find out what is inside your node modules and prevent malicious activity before you update the dependencies.