boolean.js 862 B

1234567891011121314151617181920212223242526272829
  1. import { isEmptyValue } from '../util';
  2. import rules from '../rule/';
  3. /**
  4. * Validates a boolean.
  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 boolean(rule, value, callback, source, options) {
  14. var errors = [];
  15. var 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 boolean;