DiogoMaMartins

Untitled

Nov 2nd, 2020
566
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. 'use strict';
  2.  
  3. let errors = [];
  4.  
  5. function ValidationContract(){
  6. errors = [];
  7. }
  8.  
  9. ValidationContract.prototype.isRequired = (value, message) => {
  10. if(!value || value.length <= 0)
  11. erros.push({ message:message});
  12. }
  13.  
  14. ValidationContract.prototype.hasMinLen = (value, min, message) => {
  15. if(!value || value.length < min)
  16. errors.push({ message: message});
  17.  
  18. }
  19.  
  20. ValidationContract.prototype.hasMaxLen = (value, max, message) => {
  21. if(!value || value.length > max)
  22. errors.push({ message: message});
  23.  
  24. }
  25.  
  26. ValidationContract.prototype.hasFixedLen = (value, len, message) => {
  27. if(value.length != len)
  28. errors.push({ message: message});
  29.  
  30. }
  31.  
  32. ValidationContract.prototype.isEmail = (value, message) => {
  33. const reg = new RegExp(/^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/);
  34. if(!reg.test(value))
  35. errors.push({ message: message});
  36.  
  37. }
  38.  
  39.  
  40. ValidationContract.prototype.errors = () => {
  41. return errors;
  42. }
  43.  
  44. ValidationContract.prototype.clear = () => {
  45. errors = [];
  46. }
  47.  
  48. ValidationContract.prototype.isValid = () => {
  49. return errors.length == 0;
  50. }
  51.  
  52. module.exports = ValidationContract;
  53.  
Advertisement
Add Comment
Please, Sign In to add comment