-
Notifications
You must be signed in to change notification settings - Fork 1
API Reference Release 2.1.0
- Validator
- Validation
-
Instances of this class represent a single validation and provide various methods for validating its value.
-
middleware(next, app) ⇒
function -
Middleware to validate requests. Depending on the request method it automatically decides if it uses
req.queryParams(default) orreq.postParams(only for POST and PUT). The validator requires theparamsmiddleware to be configured.
Kind: global class
-
Validator
- new Validator(objectToValidate)
-
.validate(key, trim) ⇒
Validation -
.hasUnvalidatedProperties() ⇒
boolean -
.validateAll(key, trim) ⇒
Validation -
.hasFailures(name) ⇒
boolean -
.getMessages(key) ⇒
Array|Object - .getValue(key) ⇒
-
.getValues() ⇒
Object
Creates a new Validator instance. Validator instances provide the functionality to validate values and collect messages explaining eventually occurred failures.
Returns: Validator - A newly created validator instance
| Param | Type | Description |
|---|---|---|
| objectToValidate | Object |
the object to validate |
Example
let validator = new Validator({
"email": "[email protected]"
});
// validate a value
validator.validate("email").
isDefined("Email address is missing!").
isEmail("Invalid email address provided");
// returns true if a validation failed
validator.hasFailures();
// returns an object with all failure messages
validator.getMessages();validator.validate(key, trim) ⇒ Validation
Main validation entry point. The result of this method contains various validation functions that can be chained. Note that this validation object stops validating at the first fail.
Kind: instance method of Validator
Returns: Validation - A Validation instance
See: #Validator.prototype.validateAll
| Param | Type | Description |
|---|---|---|
| key | String |
The property's key of the value |
| trim | boolean |
Optional, trim the value if it's a string |
Checks if all properties of the given object have been validated.
Kind: instance method of Validator
Returns: boolean - true, if all properties had a validation; false otherwise
Example
let validator = new Validator({
"email": "[email protected]",
"additional_key": "value"
});
// validate a value
validator.validate("email").
isDefined("Email address is missing!").
isEmail("Invalid email address provided");
// returns false, since the email is valid
validator.hasFailures();
// but: returns true since additional_key has no validation
validator.hasUnvalidatedProperties();validator.validateAll(key, trim) ⇒ Validation
Main validation entry point. The result of this method contains various validation functions that can be chained. Note that this validation object does not stop at the first fail but continues to validate all validation functions.
Kind: instance method of Validator
Returns: Validation - A Validation instance
See: #Validator.prototype.validate
| Param | Type | Description |
|---|---|---|
| key | String |
The property's key of the value |
| trim | boolean |
Optional, trim the value if it's a string |
Returns true if any of the validations of this validator failed
Kind: instance method of Validator
Returns: boolean - True if any (or the specified value) validation failed
| Param | Type | Description |
|---|---|---|
| name | String |
optional name of a single value to check |
Returns either an array with the messages for a given key, or an object with the validation names as keys and the according message array as value.
Kind: instance method of Validator
Returns: Array | Object - The messages
| Param | Type | Description |
|---|---|---|
| key | String |
Optional key to limit the failure messages to a single validation |
Returns the value for a validation.
Kind: instance method of Validator
Returns: The correspondending value for a validation
| Param | Type | Description |
|---|---|---|
| key | String |
The key of the validation |
Returns the values of all validations
Kind: instance method of Validator
Returns: Object - An object containing all validation values
Instances of this class represent a single validation and provide various methods for validating its value.
Kind: global class
-
Validation
- new Validation(validation, stopOnFail)
-
.hasLength(length, message) ⇒
Validation -
.hasLengthBetween(minLength, maxLength, message) ⇒
Validation -
.hasMaxLength(maxLength, message) ⇒
Validation -
.hasMinLength(minLength, message) ⇒
Validation -
.hasValue(message) ⇒
Validation -
.isAlpha(message) ⇒
Validation -
.isAlphanumeric(message) ⇒
Validation -
.isDateFormat(message) ⇒
Validation -
.isDefined(message) ⇒
Validation -
.isEmail(message) ⇒
Validation -
.isEqual(obj, message) ⇒
Validation -
.isFalse(message) ⇒
Validation -
.isFileName(message) ⇒
Validation -
.isFloat(message) ⇒
Validation -
.isGreaterThan(right, message) ⇒
Validation -
.isHexColor(message) ⇒
Validation -
.isInt(message) ⇒
Validation -
.isLessThan(right, message) ⇒
Validation -
.isNaN(message) ⇒
Validation -
.isNotNaN(message) ⇒
Validation -
.isNull(message) ⇒
Validation -
.isNotNull(message) ⇒
Validation -
.isNumeric(message) ⇒
Validation -
.isNumber(message) ⇒
Validation -
.isStrictEqual(obj, message) ⇒
Validation -
.isStrictNotNull(message) ⇒
Validation -
.isStrictNull(message) ⇒
Validation -
.isString(message) ⇒
Validation -
.isTrue(message) ⇒
Validation -
.isUrl(message) ⇒
Validation -
.matches(regex, message) ⇒
Validation -
.optional(defaultValue, strict) ⇒
Validation -
.passes(func, message) ⇒
Validation -
.toBoolean(strictTrue) ⇒
Validation -
.toDate(format, locale, timezone, lenient) ⇒
Validation -
.toFloat() ⇒
Validation -
.toInt() ⇒
Validation -
.toValue(converter) ⇒
Validation - .getValue() ⇒
Returns: Validation - A newly created validation instance
| Param | Type | Description |
|---|---|---|
| validation | Object |
An object containing the name of the property, the value, a validity state and an array of messages. |
| stopOnFail | boolean |
If false the all validation method calls in a chain will be executed (optional, defaults to true) |
validation.hasLength(length, message) ⇒ Validation
Passes if the value has the given length.
Kind: instance method of Validation
Returns: Validation - This validation instance
| Param | Type | Description |
|---|---|---|
| length | Number |
The required length of value |
| message | String |
The failure message |
validation.hasLengthBetween(minLength, maxLength, message) ⇒ Validation
Passes if the value's length is between the minLength and maxLength.
Kind: instance method of Validation
Returns: Validation - This validation instance
| Param | Type | Description |
|---|---|---|
| minLength | Number |
The minimal length of value |
| maxLength | Number |
The maximum length of value |
| message | String |
The failure message |
validation.hasMaxLength(maxLength, message) ⇒ Validation
Passes if the value's length is not longer than maxLength.
Kind: instance method of Validation
Returns: Validation - This validation instance
| Param | Type | Description |
|---|---|---|
| maxLength | Number |
The maximum length of value |
| message | String |
The failure message |
validation.hasMinLength(minLength, message) ⇒ Validation
Passes if the value's length is not shorter than minLength.
Kind: instance method of Validation
Returns: Validation - This validation instance
| Param | Type | Description |
|---|---|---|
| minLength | Number |
The minimal length of value |
| message | String |
The failure message |
validation.hasValue(message) ⇒ Validation
Passes if the value is not undefined, not null or the not empty string.
Kind: instance method of Validation
Returns: Validation - This validation instance
| Param | Type | Description |
|---|---|---|
| message | String |
The failure message |
validation.isAlpha(message) ⇒ Validation
Passes if the value is alphabetic.
Kind: instance method of Validation
Returns: Validation - This validation instance
| Param | Type | Description |
|---|---|---|
| message | String |
The failure message |
validation.isAlphanumeric(message) ⇒ Validation
Passes if the value contains only letters of the alphabet and numbers.
Kind: instance method of Validation
Returns: Validation - This validation instance
| Param | Type | Description |
|---|---|---|
| message | String |
The failure message |
validation.isDateFormat(message) ⇒ Validation
Passes if the value is a valid date format pattern.
Kind: instance method of Validation
Returns: Validation - This validation instance
| Param | Type | Description |
|---|---|---|
| message | String |
The failure message |
validation.isDefined(message) ⇒ Validation
Passes if the value is not undefined.
Kind: instance method of Validation
Returns: Validation - This validation instance
| Param | Type | Description |
|---|---|---|
| message | String |
The failure message |
validation.isEmail(message) ⇒ Validation
Passes if the value is a valid email address.
Kind: instance method of Validation
Returns: Validation - This validation instance
| Param | Type | Description |
|---|---|---|
| message | String |
The failure message |
validation.isEqual(obj, message) ⇒ Validation
Passes if the value is equal to the given object.
Kind: instance method of Validation
Returns: Validation - This validation instance
| Param | Type | Description |
|---|---|---|
| obj | Object |
The object to compare |
| message | String |
The failure message |
validation.isFalse(message) ⇒ Validation
Passes if the value is false.
Kind: instance method of Validation
Returns: Validation - This validation instance
| Param | Type | Description |
|---|---|---|
| message | String |
The failure message |
validation.isFileName(message) ⇒ Validation
Passes if the value contains no characters that are forbidden in file names.
Kind: instance method of Validation
Returns: Validation - This validation instance
| Param | Type | Description |
|---|---|---|
| message | String |
The failure message |
validation.isFloat(message) ⇒ Validation
Passes if the value is a valid floating point string literal or of type Number with a remainder.
Kind: instance method of Validation
Returns: Validation - This validation instance
| Param | Type | Description |
|---|---|---|
| message | String |
The failure message |
validation.isGreaterThan(right, message) ⇒ Validation
Passes if the value is greater than the given number: value > right
Kind: instance method of Validation
Returns: Validation - This validation instance
| Param | Type | Description |
|---|---|---|
| right | Number |
The right number of the numeric comparison. |
| message | String |
The failure message |
validation.isHexColor(message) ⇒ Validation
Passes if the value is a valid color value in hexadecimal format. It may also contain # as first character.
Kind: instance method of Validation
Returns: Validation - This validation instance
| Param | Type | Description |
|---|---|---|
| message | String |
The failure message |
validation.isInt(message) ⇒ Validation
Passes if the value is a valid integer string literal or Number.isSafeInteger(value) returns true.
Kind: instance method of Validation
Returns: Validation - This validation instance
| Param | Type | Description |
|---|---|---|
| message | String |
The failure message |
validation.isLessThan(right, message) ⇒ Validation
Passes if the value is less than the given number: value < right
Kind: instance method of Validation
Returns: Validation - This validation instance
| Param | Type | Description |
|---|---|---|
| right | Number |
The right number of the numeric comparison. |
| message | String |
The failure message |
validation.isNaN(message) ⇒ Validation
Passes if the value is NaN.
Kind: instance method of Validation
Returns: Validation - This validation instance
| Param | Type | Description |
|---|---|---|
| message | String |
The failure message |
validation.isNotNaN(message) ⇒ Validation
Passes if the value is not NaN.
Kind: instance method of Validation
Returns: Validation - This validation instance
| Param | Type | Description |
|---|---|---|
| message | String |
The failure message |
validation.isNull(message) ⇒ Validation
Passes if the value is null.
Kind: instance method of Validation
Returns: Validation - This validation instance
| Param | Type | Description |
|---|---|---|
| message | String |
The failure message |
validation.isNotNull(message) ⇒ Validation
Passes if the value is not null.
Kind: instance method of Validation
Returns: Validation - This validation instance
| Param | Type | Description |
|---|---|---|
| message | String |
The failure message |
validation.isNumeric(message) ⇒ Validation
Passes if the value contains only the characters [0-9] or is of type number.
Kind: instance method of Validation
Returns: Validation - This validation instance
| Param | Type | Description |
|---|---|---|
| message | String |
The failure message |
validation.isNumber(message) ⇒ Validation
Passes if the value is a valid floating point or integer string literal.
Kind: instance method of Validation
Returns: Validation - This validation instance
| Param | Type | Description |
|---|---|---|
| message | String |
The failure message |
validation.isStrictEqual(obj, message) ⇒ Validation
Passes if the value is strict equal to the given object.
Kind: instance method of Validation
Returns: Validation - This validation instance
| Param | Type | Description |
|---|---|---|
| obj | Object |
The object to compare |
| message | String |
The failure message |
validation.isStrictNotNull(message) ⇒ Validation
Passes if the value is strict not null.
Kind: instance method of Validation
Returns: Validation - This validation instance
| Param | Type | Description |
|---|---|---|
| message | String |
The failure message |
validation.isStrictNull(message) ⇒ Validation
Passes if the value is strict null.
Kind: instance method of Validation
Returns: Validation - This validation instance
| Param | Type | Description |
|---|---|---|
| message | String |
The failure message |
validation.isString(message) ⇒ Validation
Passes if the value is a string.
Kind: instance method of Validation
Returns: Validation - This validation instance
| Param | Type | Description |
|---|---|---|
| message | String |
The failure message |
validation.isTrue(message) ⇒ Validation
Passes if the value is true.
Kind: instance method of Validation
Returns: Validation - This validation instance
| Param | Type | Description |
|---|---|---|
| message | String |
The failure message |
validation.isUrl(message) ⇒ Validation
Passes if the value is a valid URL.
Kind: instance method of Validation
Returns: Validation - This validation instance
| Param | Type | Description |
|---|---|---|
| message | String |
The failure message |
validation.matches(regex, message) ⇒ Validation
Passes if the value matched the given regular expression.
Kind: instance method of Validation
Returns: Validation - This validation instance
| Param | Type | Description |
|---|---|---|
| regex | RegExp |
The regular expression to match |
| message | String |
The failure message |
validation.optional(defaultValue, strict) ⇒ Validation
Marks the validation of a key as optional. If provided and if a key is not defined or its value is falsy,
the validation's value will be replaced with the defaultValue.
Kind: instance method of Validation
Returns: Validation - This validation instance
| Param | Type | Description |
|---|---|---|
| defaultValue | * |
an optional default value for the given validation |
| strict | boolean |
if true, the key's value must be undefined to apply the default value, otherwise a falsy value will be sufficient to fall back. |
validation.passes(func, message) ⇒ Validation
Passes if the given function returns true. The function receives the value as first argument.
Kind: instance method of Validation
Returns: Validation - This validation instance
| Param | Type | Description |
|---|---|---|
| func | function |
The validation function |
| message | String |
The failure message |
validation.toBoolean(strictTrue) ⇒ Validation
Converts the value to a boolean.
Kind: instance method of Validation
Returns: Validation - This validation instance
| Param | Type | Description |
|---|---|---|
| strictTrue | boolean |
only return true if value is strict "true" or "1" |
validation.toDate(format, locale, timezone, lenient) ⇒ Validation
Converts the value to a Date object or NaN if parsing failed.
Kind: instance method of Validation
Returns: Validation - This validation instance
| Param | Type | Description |
|---|---|---|
| format | String |
(optional) a specific format pattern for the parser |
| locale |
String | java.util.Locale
|
(optional) the locale as java.util.Locale object or an ISO-639 alpha-2 code (e.g. "en", "de") as string |
| timezone |
String | java.util.TimeZone
|
(optional) the timezone as java TimeZone object or an abbreviation such as "PST", a full name such as "America/Los_Angeles", or a custom ID such as "GMT-8:00". If the id is not provided, the default timezone is used. If the timezone id is provided but cannot be understood, the "GMT" timezone is used. |
| lenient | boolean |
(optional) disables lenient parsing if set to false. |
validation.toFloat() ⇒ Validation
Converts the value to a floating point number or NaN if if parsing failed.
Kind: instance method of Validation
Returns: Validation - This validation instance
validation.toInt() ⇒ Validation
Converts the value to an integer number or NaN if if parsing failed.
Kind: instance method of Validation
Returns: Validation - This validation instance
validation.toValue(converter) ⇒ Validation
Converts the value with the given function.
Kind: instance method of Validation
Returns: Validation - This validation instance
| Param | Type | Description |
|---|---|---|
| converter | function |
The function to apply on the current value |
Returns the value of the validation.
Kind: instance method of Validation
Returns: Current value of the validation
Middleware to validate requests.
Depending on the request method it automatically decides if it
uses req.queryParams (default) or req.postParams (only for POST and PUT).
The validator requires the params middleware to be configured.
Kind: global function
Returns: function - a JSGI middleware function
| Param | Type | Description |
|---|---|---|
| next | function |
the wrapped middleware chain |
| app | Object |
the Stick Application object |
Example
app.configure("params", require("validator"), "route");
app.get("/", function(req) {
req.validate("countryCode", true)
.hasLength(3, "Invalid country code!");
req.validate("email")
.isDefined("Email missing!")
.isEmail("Invalid email!");
if (req.hasFailures()) {
return response.html(req.failureMessages().join("<br>"));
}
return response.html("Ok");
});