enum.js 1.2 KB

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