You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
22 lines
748 B
22 lines
748 B
1 month ago
|
import * as util from '../util';
|
||
|
var ENUM = 'enum';
|
||
|
|
||
|
/**
|
||
|
* Rule for validating a value exists in an enumerable list.
|
||
|
*
|
||
|
* @param rule The validation rule.
|
||
|
* @param value The value of the field on the source object.
|
||
|
* @param source The source object being validated.
|
||
|
* @param errors An array of errors that this rule may add
|
||
|
* validation errors to.
|
||
|
* @param options The validation options.
|
||
|
* @param options.messages The validation messages.
|
||
|
*/
|
||
|
function enumerable(rule, value, source, errors, options) {
|
||
|
rule[ENUM] = Array.isArray(rule[ENUM]) ? rule[ENUM] : [];
|
||
|
if (rule[ENUM].indexOf(value) === -1) {
|
||
|
errors.push(util.format(options.messages[ENUM], rule.fullField, rule[ENUM].join(', ')));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export default enumerable;
|