enum.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _rule = require('../rule/');
  6. var _rule2 = _interopRequireDefault(_rule);
  7. var _util = require('../util');
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
  9. var ENUM = 'enum';
  10. /**
  11. * Validates an enumerable list.
  12. *
  13. * @param rule The validation rule.
  14. * @param value The value of the field on the source object.
  15. * @param callback The callback function.
  16. * @param source The source object being validated.
  17. * @param options The validation options.
  18. * @param options.messages The validation messages.
  19. */
  20. function enumerable(rule, value, callback, source, options) {
  21. var errors = [];
  22. var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
  23. if (validate) {
  24. if ((0, _util.isEmptyValue)(value) && !rule.required) {
  25. return callback();
  26. }
  27. _rule2['default'].required(rule, value, source, errors, options);
  28. if (value) {
  29. _rule2['default'][ENUM](rule, value, source, errors, options);
  30. }
  31. }
  32. callback(errors);
  33. }
  34. exports['default'] = enumerable;
  35. module.exports = exports['default'];