Answer the question
In order to leave comments, you need to log in
How to write a standalone validate module based on validate.js?
Good afternoon.
I'm trying to write a separate module for validation:
const validate = require("validate.js");
const constraints = {
Name: {
presence: true,
format: {
pattern: "^[^0-9][^@#]+$",
flags: "i",
message: "Pattern error"
},
length: {
minimum: 3,
maximum: 20,
message: "Length error"
}
},
email: {
presence: true,
email: true
}
};
function success(attributes) {
LOG("Success!", attributes);
}
function error(errors) {
if (errors instanceof Error) {
// This means an exception was thrown from a validator
LOG("An error ocurred", errors);
} else {
LOG("Validation errors", errors);
}
}
module.exports.validData = (newData) => {
validate.async(newData, constraints).then(success, error);
};
validData(newData, function(err){
if(!err) return next(err);
console.log("validate successful");
})
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question