validate(data) { this.messages = [] for (let i in data) { if (data.hasOwnProperty(i)) { let type = this.config[i] let checker = this.types[type]
if (!type) { continue }
if (!checker) { throw { name: "ValidationError", message: "No handler to validate type:" + type } } let result = checker.validate(data[i]) if (!result) { let msg = "Invalid value for *" + i + "*, " + checker.instructions this.messages.push(msg) } } } returnthis.hasErrors() } hasErrors() { returnthis.messages.length !== 0 } } module.exports = Validator
// /src/ValidatorStrategy.js module.exports = strategy = { isNonEmpty: { validate: function (value) { return value !== "" }, instructions: "the value cannot be empty" }, isNumber: { validate: function (value) { return !isNaN(value) }, instructions: "the value can only be a valid number, e.g. 1, 3.14 or 201" }, isAlphaNum: { validate: function (value) { return !/[^a-z0-9]/i.test(value) }, instructions: "the value can only contain characters and numbers, no spe" } }