method.js 876 B

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