Members
checkers :Checkers
The default checker collection.
Type:
Properties:
Name | Type | Description | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
before |
Step | Checkers that are called before any other validation.
Properties
|
|||||||||||||||
during |
Step | The main validation step.
Properties
|
- Source:
- See:
config :Object
Default configuration object.
Type:
- Object
Properties:
Name | Type | Default | Description |
---|---|---|---|
env |
string | NODE_ENV | The environement for current runtime |
silent |
boolean | false | Whether to silence warnings when devtools are enabled |
checkersSteps |
Array.<string> | 'before','during' | The list of validation steps chronolgicaly ordered. |
confirmationRE |
regexp | /(.+)_confirmation$/ | The RegExp to test against to detect if a fieldname is a shorthand for the confirms checker. |
emailRE |
regexp | /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i | The RegExp to validate emails. |
emptyStringRE |
regexp | /^[\s\t\n\r]+$/ | The RegExp that defines which strings are considered as empty. This is used by Isntit.isEmpty |
emptyValues |
Array.<string> | null,undefined,'undefined' | The values to consider as empty. This is used by Isntit.isEmpty |
messages |
Object.<string, MessageTypes> | The fallback error messages tu use when validation against default checkers fails. Use the checker name as key. | |
noLabelChar |
string | ^ | Any error message starting with this chaaracter will never be prepended with the corresponding field name (even when options.fullMessages) is true. |
- Copyright:
- Many thanks to Jan Goyvaerts for his email regular expression
- Source:
- See:
confirms :Constraints
Check that the value is the same as the value in another field.
Type:
Properties:
Name | Type | Attributes | Description |
---|---|---|---|
field |
string | The field this field should confirm. | |
strict |
boolean |
<optional> |
Whether to compare strictly ('===') both field values or not ('=='). |
message |
ErrorMessageProvider |
<optional> |
A customized error message. |
Example
"password_confirmation": true
// is converted to
"password_confirmation": {
confirms: {
field: "password"
}
}
email :Constraints
Check that the value is formated as an email (uses config.emailRE).
Type:
Properties:
Name | Type | Attributes | Description |
---|---|---|---|
message |
ErrorMessageProvider |
<optional> |
A customized error message. |
format :regexp|Constraints
Check that the value matches against given RegExp.
Type:
- regexp | Constraints
Properties:
Name | Type | Attributes | Description |
---|---|---|---|
pattern |
regexp | The RegExp to test against. | |
message |
ErrorMessageProvider |
<optional> |
A customized error message. |
length :Constraints
Check that value comply with legth constraints.
Type:
Properties:
Name | Type | Attributes | Description |
---|---|---|---|
is |
number |
<optional> |
The exact length of the value. |
min |
number |
<optional> |
The minimum length of the value. |
max |
number |
<optional> |
The maximum length of the value. |
message |
ErrorMessageProvider |
<optional> |
A customized error message. |
numeric :Constraints
Check value by comparing it with given constraints.
Type:
Properties:
Name | Type | Attributes | Description |
---|---|---|---|
equalTo |
number |
<optional> |
|
notEqualTo |
number |
<optional> |
|
greaterThan |
number |
<optional> |
|
greaterThanOrEqualTo |
number |
<optional> |
|
lessThan |
number |
<optional> |
|
lessThanOrEqualTo |
number |
<optional> |
|
greaterThan |
number |
<optional> |
|
noStrings |
boolean |
<optional> |
Whether to accept numbers as strings |
onlyIntegers |
boolean |
<optional> |
Whether to accept only integers. |
message |
ErrorMessageProvider |
<optional> |
A customized error message. |
required :boolean|Constraints
Check that a field is not empty (uses Isntit.isEmpty).
Type:
- boolean | Constraints
Properties:
Name | Type | Attributes | Description |
---|---|---|---|
message |
ErrorMessageProvider |
<optional> |
A customized error message. |
Methods
checkType(object, typeRules) → {boolean}
Check that an object comply with given type rules
Parameters:
Name | Type | Description |
---|---|---|
object |
* | Any javascript object. |
typeRules |
TypeRule | A description of allowed types. |
Returns:
Whether if given object complies with given type rules.
- Type
- boolean
Type Definitions
Checker
A checker is responsible for the validation of data. It holds the validation logic and the optional pre-validation check and type rules.
Type:
- Object.<string, (Object|function())>
Properties:
Name | Type | Attributes | Description |
---|---|---|---|
validate |
function | Object.<string, function()> | The logic that validates the value. Receives value and the current context as arguments. | |
preporcess |
function |
<optional> |
A function that is called before actually validating the value. This is generaly used to check the type of given value and fail if the type is wrong without even trying to validate it. |
types |
TypeRule |
<optional> |
When devtools are enabled, field rules are checked with checkType to enforce the right types of constraints values. |
Example
length: {
validate: {
is: function(value, context) {
return (value.length == context.ruleSet['length'].is);
},
min: function(value, context) {
return (value.length >= context.ruleSet['length'].min);
},
max: function(value, context) {
return (value.length <= context.ruleSet['length'].max);
}
},
preprocess: function(value, context) {
var I = this;
if (value.length === undefined) {
I.cache.messages['length::preprocess'] = 'Values checked for length MUST have a length property. Given: ' + value;
return false;
}
return true;
},
types: {
__all: 'number'
}
}
Checkers
Collection of checkers ordered in separated steps.
The validation process simply loops over this Object. Step names have no effect
on the order.
Type:
- Object
Properties:
Name | Type | Attributes | Description |
---|---|---|---|
before |
Step | A collection of checkers to call 'before' validation. | |
during |
Step | A collection of checkers called 'during' validation. | |
`stepName` |
Step |
<optional> |
A collection of checkers to call in the 'stepName'. |
Constraints
The constraints to pass to corresponding checker.
Type:
- Object.<string, *>
Properties:
Name | Type | Attributes | Description |
---|---|---|---|
message |
ErrorMessageProvider |
<optional> |
A customized error message. |
`constraintName` |
string | number | boolean |
<optional> |
The value of a constraint to pass to the checker. |
Context
The context in which the validate function of a checker is called (passed as second argument).
Type:
- Object
Properties:
Name | Type | Description |
---|---|---|
value |
* | The value of the field under validation. |
`fieldName` |
string | The name of the field under validation. |
data |
Object.<string, *> | The whole data object that is currently validated. |
`ruleName` |
string | The name of the checker currently called. |
ruleSet |
Object.<string, Constraints> | The collection of rules to check current field against. |
rules |
Rules | All the rules curently used during validation. |
step |
string | The name of the current step. |
ErrorMessageProvider
To customize the error messages you can either provide a string,
a function that returns a string or an object with `constraintName`s
as keys to a appropriated error message string. All error messages strings
are parsed by Isntit.printf to replace any `%{placeholders}`
with corresponding value taken from {@Constraints} properties (other than `message`).
Type:
MessageFunction(context, constraintopt) → {MessageString}
A function that returns an error message should corresponding checker validation fail.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
context |
Context | The context in which the failed checker has been called. | |
constraint |
string |
<optional> |
The name of the constraint that failed for which a error message should be returned. |
Returns:
- Type
- MessageString
MessageObject
An object with `constraintName`s as keys and MessageStrings or MessageFunctions as value.
Type:
- Object.<string, (MessageString|MessageFunction)>
Properties:
Name | Type | Attributes | Description |
---|---|---|---|
`constraintName` |
MessageString |
<optional> |
A constraintName-MessageString pair. |
MessageString
The message to show if corresponding checker fails.
This string is passed to Isntit.printf to replace `%{placeholders}`
with corresponding value taken from {@Constraints} properties (other than message).
Type:
- string
Options
The options object to pass to the Isntit constructor.
Type:
- Object
Properties:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
capitalize |
boolean |
<optional> |
true | Whether to capitalize the first character of error messages. |
devtools |
boolean |
<optional> |
true | Whether to enable devtools (only for development build). |
fullMessages |
boolean |
<optional> |
false | Whether to prefix error messages with corresponding field name. |
config |
Object |
<optional> |
The config properties to override. |
Rules
The validation rules object to pass to the Isntit constructor or to Isntit's validate method.
Type:
- Object.<string, Object>
Properties:
Name | Type | Description | ||||||
---|---|---|---|---|---|---|---|---|
`fieldName` |
Object.<string, Constraints> | boolean | The rules to validate the value of field.
Properties
|
Step
A collection of checkers. Validation happens by steps. If one rule fails during current step, validation ends at the end of this step.
Type:
- Object.<string, Checker>
TypeRule
A set of rules defining the allowed types to check an object against.
Type:
Example
'number' // The object should be a number.
or
['boolean', ['string', 'number']] // The object should either be a boolean
// or an array of string and/or numbers.
or
{ // noStrings and onlyIntegers should be
noStrings: 'boolean', // booleans, all other properties should
onlyIntegers: 'boolean', // be numbers. Use __all instead of __others
__others: 'numbers' // if all properties should comply with
} // given TypeRule.