validation-is-required
v1.0.0
Published
Standalone javascript validation for checking the property on incoming request is required or, with zero depedencies
Downloads
1
Readme
The JavaScript validationIsRequired Library
Standalone javascript validation for checking the property on incoming request is required or, with zero depedencies
Installation
NPM
npm i validation-is-required
Yarn
yarn add validation-is-required
Example
import validationIsRequired from 'validation-validation-is-required';
const properties = [
'name',
'year',
'publisher',
];
const payload = {
year: 2021,
publisher: 'Unknown',
}
try {
validationIsRequired(properties, payload, (property) => {
// property => name
const message = `Failed to added. the ${property} is required.`;
throw new Error(message);
});
} catch (e) {
// Failed to added. the name is required.
console.log(e.message);
}
}
Params
properties are any rules that must be filled in
const properties = [ 'name', 'year', 'publisher', ];
if you plan to multiply a property for the callback
const properties = [ ['name', 'nama' /* alias */], 'year', 'publisher', ];
payload
is an object that can be obtained from the request
example
{ name: 'Kevin Abrar Khansa', year: 2021, publisher: 'Example Inc.' }
Callback
is an output if the property is not filled
(property) => { // property => name const message = `Failed to added. the ${property} is required.`; throw new Error(message); }