enum.js 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _util = require('../util');
  4. var util = _interopRequireWildcard(_util);
  5. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } }
  6. var ENUM = 'enum';
  7. /**
  8. * Rule for validating a value exists in an enumerable list.
  9. *
  10. * @param rule The validation rule.
  11. * @param value The value of the field on the source object.
  12. * @param source The source object being validated.
  13. * @param errors An array of errors that this rule may add
  14. * validation errors to.
  15. * @param options The validation options.
  16. * @param options.messages The validation messages.
  17. */
  18. function enumerable(rule, value, source, errors, options) {
  19. rule[ENUM] = Array.isArray(rule[ENUM]) ? rule[ENUM] : [];
  20. if (rule[ENUM].indexOf(value) === -1) {
  21. errors.push(util.format(options.messages[ENUM], rule.fullField, rule[ENUM].join(', ')));
  22. }
  23. }
  24. exports['default'] = enumerable;
  25. module.exports = exports['default'];