-
Notifications
You must be signed in to change notification settings - Fork 23
Configuring Tests
Each test in ApproveJs has its own default error message. If you want to return a custom message for a test, you can pass the message in the constraint. For example:
var rules = {
email: {
message: 'Email is not a valid email address'
}
};
var result = approve.value('user@domain.com', rules);
Now, if the test should fail, your message will be passed along with the result.
For tests that expect parameters, the code is the same:
Single parameter tests
var rules = {
min: {
min: 5,
message: 'The text must be at least 5 characters'
}
};
Multiple parameter tests
var rules = {
range: {
min: 5,
max: 20,
message: 'The text must be at least 5 characters, and no more than 20 characters'
}
};
If you do not pass a custom error message with your rules, the default message for each test will be used. ApproveJs automatically formats these messages for you by replacing placeholders with values. Each default message has a {title}
placeholder. This placeholder is replaced with the title of your value, but before ApproveJs can format the message, you need to provide the title, otherwise it will simply be replaced with an empty string. For more information, read test results.
You can pass the title of your value by adding a title
property to the rules object:
var rules = {
title: 'Username',
test: constraint
};
Some tests might have configurable options. For instance, in addition to the default error message, the strength
test returns other errors regarding the score. Those error messages can be replaced with your own.
To send configuration along to the test, simply add a config
property to the test constraints in the rules object, and ApproveJs will pass it to the test.
For configurable options, check the built-in tests options.
For the strength test, let's configure the default score error messages:
var rules = {
strength: {
min: 8,
bonus: 10,
// Config
config: {
// Configurable property
messages: {
isMinimum: '{title} must be a minimum of {min} characters',
hasLower: '{title} must have at least 1 lower case character',
hasUpper: '{title} must have at least 1 upper case character',
hasNumber: '{title} must have at least 1 number',
hasSpecial: '{title} must have at least 1 special character'
}
}
}
};
var result = approve.value('Some Text', rules);