Validator is a lightweight validation and sanitization library for RingoJS.
It’s using the ringo/utils/strings
module for string validation.
In a Web application you can use the validator to validate the submitted data:
var validator = new Validator(req.postParams);
validator.validate("username", true) // true --> trim the value
.isDefined("Username is missing!")
.hasMinLength(3, "Username is too short!")
.hasMaxLength(255, "Username is too long!");
validator.validate("email")
.isDefined("Email address is missing!")
.isEmail("Invalid email address!");
validator.validate("age")
.isInt("Invalid age!")
.toInt().isGreaterThan(17, "You need to be 18 years old!");
// Retrieve a single value
log.debug("Age: " + validator.getValue("age"));
if (validator.hasFailures()) {
// Display errors
return respone.html(
// returns an object with grouped error messages
// e.g. { "age": ["Invalid age!"], "email": […] }
validator.getMessages()
);
}
- Release 2.1.0
- adds
hasUnvalidatedProperties()
to check if the validated object has no additional properties defined
- adds
- Release 2.0.0
- all validating functions are now prefixed with
is
orhas
e.g.minLength()
changed tohasMinLength()
- Changed the semantics of
.optional()
, which will no longer execute subsequent validation functions or converts. - The default value provided in
.optional()
will be returned byvalidator.getValue()
now
- all validating functions are now prefixed with