enum.js 900 B

123456789101112131415161718192021222324252627282930
  1. import rules from '../rule/index.js';
  2. import { isEmptyValue } from '../util';
  3. const ENUM = 'enum';
  4. /**
  5. * Validates an enumerable list.
  6. *
  7. * @param rule The validation rule.
  8. * @param value The value of the field on the source object.
  9. * @param callback The callback function.
  10. * @param source The source object being validated.
  11. * @param options The validation options.
  12. * @param options.messages The validation messages.
  13. */
  14. function enumerable(rule, value, callback, source, options) {
  15. const errors = [];
  16. const validate = rule.required || (!rule.required && source.hasOwnProperty(rule.field));
  17. if (validate) {
  18. if (isEmptyValue(value) && !rule.required) {
  19. return callback();
  20. }
  21. rules.required(rule, value, source, errors, options);
  22. if (value) {
  23. rules[ENUM](rule, value, source, errors, options);
  24. }
  25. }
  26. callback(errors);
  27. }
  28. export default enumerable;