index.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /**
  2. * Module dependencies
  3. */
  4. var _ = require('@sailshq/lodash');
  5. var match = require('./lib/match');
  6. /**
  7. * Public access
  8. */
  9. module.exports = function (entity, ruleset) {
  10. var errors = [];
  11. // If ruleset doesn't contain any explicit rule keys,
  12. // assume that this is a type
  13. // Look for explicit rules
  14. for (var rule in ruleset) {
  15. // TODO: In next major version, remove this: It should definitely not be here.
  16. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  17. // Normalize the value if it looks like a boolean
  18. if(ruleset[rule] === 'true') {
  19. ruleset[rule] = true;
  20. }
  21. if(ruleset[rule] === 'false') {
  22. ruleset[rule] = false;
  23. }
  24. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  25. // If the value is false, then we shouldn't even run the validation
  26. if(ruleset[rule] === false) {
  27. break;
  28. }
  29. // If the rule value is a boolean we don't need to pass the value along.
  30. // Otherwise we can pass it along so it's options are available in
  31. // the validation.
  32. var ruleVal = _.isBoolean(ruleset[rule]) ? undefined : ruleset[rule];
  33. errors = errors.concat(match(entity, rule, ruleVal));
  34. }
  35. // If errors exist, return the list of them
  36. if (errors.length) {
  37. return errors;
  38. }
  39. // No errors, so return false
  40. else {
  41. return [];
  42. }
  43. };