chain-creator.js 894 B

12345678910111213141516171819202122232425262728293031323334
  1. const check = require('../check/check');
  2. module.exports = (field, location, message, contexts, { validators, sanitizers }) => {
  3. const chain = check([field], Array.isArray(location) ? location : [location], message);
  4. contexts.push(chain._context);
  5. chain.notEmpty = () => chain.isLength({ min: 1 });
  6. chain.len = chain.isLength;
  7. Object.keys(validators).forEach(name => {
  8. chain[name] = (...options) => {
  9. chain._context.validators.push({
  10. options,
  11. custom: true,
  12. negated: chain._context.negateNext,
  13. validator: validators[name]
  14. });
  15. chain._context.negateNext = false;
  16. return chain;
  17. };
  18. });
  19. Object.keys(sanitizers).forEach(name => {
  20. chain[name] = (...options) => {
  21. chain._context.sanitizers.push({
  22. options,
  23. sanitizer: sanitizers[name]
  24. });
  25. return chain;
  26. };
  27. });
  28. return chain;
  29. };