number.js 931 B

123456789101112131415161718192021222324252627282930
  1. import rules from '../rule/index.js';
  2. import { isEmptyValue } from '../util';
  3. /**
  4. * Validates a number.
  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 number(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. rules.range(rule, value, source, errors, options);
  24. }
  25. }
  26. callback(errors);
  27. }
  28. export default number;