Guest User

Untitled

a guest
Oct 17th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. const Parameter = require('parameter')
  2.  
  3. const p = new Parameter()
  4.  
  5. // custom checker to check from field
  6. p.addRule('from', function(rule, value) {
  7. if (typeof value === 'string') {
  8. return Parameter.TYPE_MAP.email.call(this, { allowEmpty: false }, value)
  9. } else if (typeof value === 'object') {
  10. const objectRule = {
  11. name: {
  12. type: 'string',
  13. required: true,
  14. },
  15. email: {
  16. type: 'email',
  17. required: true,
  18. },
  19. }
  20. return Parameter.TYPE_MAP.object.call(this, { rule: objectRule }, value)
  21. } else {
  22. return this.t('should provide a string or object')
  23. }
  24. })
  25.  
  26. // custom checker to check to field
  27. p.addRule('to', function(rule, value) {
  28. const objectRule = {
  29. email: {
  30. type: 'email',
  31. required: true,
  32. },
  33. }
  34. if (typeof value === 'string') {
  35. return Parameter.TYPE_MAP.email.call(this, { allowEmpty: false }, value)
  36. } else if (Object.prototype.toString.call(value) === '[object Object]') {
  37. return Parameter.TYPE_MAP.object.call(this, { rule: objectRule }, value)
  38. } else if (Array.isArray(value)) {
  39. const aryRule = {
  40. itemType: 'to',
  41. }
  42. return Parameter.TYPE_MAP.array.call(this, aryRule, value)
  43. } else {
  44. return this.t('should provide a string or an array')
  45. }
  46. })
  47.  
  48. // ps. parameter only support validate an object(can not be an array)
  49. const rule = {
  50. subject: 'string',
  51. from: 'from',
  52. to: 'to',
  53. data: 'object',
  54. id: 'string',
  55. }
Add Comment
Please, Sign In to add comment