enum.js 751 B

12345678910111213141516171819202122
  1. import * as util from '../util';
  2. const ENUM = 'enum';
  3. /**
  4. * Rule for validating a value exists in an enumerable list.
  5. *
  6. * @param rule The validation rule.
  7. * @param value The value of the field on the source object.
  8. * @param source The source object being validated.
  9. * @param errors An array of errors that this rule may add
  10. * validation errors to.
  11. * @param options The validation options.
  12. * @param options.messages The validation messages.
  13. */
  14. function enumerable(rule, value, source, errors, options) {
  15. rule[ENUM] = Array.isArray(rule[ENUM]) ? rule[ENUM] : [];
  16. if (rule[ENUM].indexOf(value) === -1) {
  17. errors.push(util.format(options.messages[ENUM], rule.fullField, rule[ENUM].join(', ')));
  18. }
  19. }
  20. export default enumerable;