array.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. /**
  10. * Validates an array.
  11. *
  12. * @param rule The validation rule.
  13. * @param value The value of the field on the source object.
  14. * @param callback The callback function.
  15. * @param source The source object being validated.
  16. * @param options The validation options.
  17. * @param options.messages The validation messages.
  18. */
  19. function array(rule, value, callback, source, options) {
  20. var errors = [];
  21. var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
  22. if (validate) {
  23. if ((0, _util.isEmptyValue)(value, 'array') && !rule.required) {
  24. return callback();
  25. }
  26. _rule2['default'].required(rule, value, source, errors, options, 'array');
  27. if (!(0, _util.isEmptyValue)(value, 'array')) {
  28. _rule2['default'].type(rule, value, source, errors, options);
  29. _rule2['default'].range(rule, value, source, errors, options);
  30. }
  31. }
  32. callback(errors);
  33. }
  34. exports['default'] = array;
  35. module.exports = exports['default'];