Advertisement
Guest User

Untitled

a guest
Aug 11th, 2016
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1.  
  2. var any = (...vs) => ["any", ...vs]
  3. var all = (...vs) => ["all", ...vs]
  4. var prop = (msg, p) => ["prop", msg, p]
  5. var describe = (message, v) => ["describe", message, v]
  6. var fields = (scheme) => ["fields", scheme]
  7. var not = (msg, v) => ["not", msg, v]
  8.  
  9. var matches = (string) =>
  10. prop("matches " + string, (input) =>
  11. input.match(string))
  12.  
  13. var length = (msg, lenPred) =>
  14. map((str) => str.length, prop(msg, lenPred))
  15.  
  16. var map = (f, v) => ["map", f, v]
  17.  
  18. var validateWith = (template, object) => {
  19. let [head, ...tail] = template
  20. switch (head) {
  21. case "all": {
  22. let errors = tail.
  23. map(t => validateWith(t, object)).
  24. filter(x => x)
  25.  
  26. return errors.length? ["all", ...errors] : null
  27. }
  28.  
  29. case "any": {
  30. var acc = []
  31. for (var i = 0; i < tail.length; i++) {
  32. let sub = validateWith(tail[i], object)
  33. if (!sub)
  34. return null
  35.  
  36. acc.push(sub)
  37. }
  38. return ["any", ...acc]
  39. }
  40.  
  41. case "prop": {
  42. let [msg, prop] = tail
  43. return prop(object) ? null : msg
  44. }
  45.  
  46. case "describe": {
  47. let [msg, v] = tail
  48. let sub = validateWith(v, object)
  49. return sub ? msg : null
  50. }
  51.  
  52. case "fields": {
  53. let [o] = tail
  54. var acc = {}
  55. var hasErrors = false
  56. for (var key in o) {
  57. let sub = validateWith(o[key], object[key])
  58. if (sub) {
  59. acc[key] = sub
  60. hasErrors = true
  61. }
  62. }
  63. return hasErrors? ["fields", acc] : null
  64. }
  65.  
  66. case "not": {
  67. let [msg, v] = tail
  68. let sub = validateWith(v, object)
  69.  
  70. return sub? null: msg;
  71. }
  72.  
  73. case "map": {
  74. let [f, v] = tail
  75. let projected = f(object)
  76. return validateWith(v, projected)
  77. }
  78. }
  79. }
  80.  
  81. var makeValidatorFromScheme = (scheme) => (o) =>
  82. validateWith(scheme, o)
  83.  
  84. var validateUser = makeValidatorFromScheme(
  85. fields({
  86. email: describe("proper email",
  87. matches(".*@.*")
  88. ),
  89. name: all(
  90. describe("contains only alphanumeric and '-'",
  91. matches("[A-Za-z0-9-]*")
  92. ),
  93. length(">= 3 chars", (len) => len >= 3),
  94. length("<= 20 chars", (len) => len <= 20),
  95. describe("no shit",
  96. not("bad word", matches("shit|crap"))
  97. )
  98. ),
  99. password: all(
  100. describe("has lower chars", matches("[a-z]")),
  101. describe("has capital chars", matches("[A-Z]")),
  102. describe("has numeric chars", matches("[0-9]")),
  103. length(">= 8 chars", (len) => len >= 8)
  104. )
  105. })
  106. )
  107.  
  108. // validator will produce "null" on success and non-null structure on failure
  109.  
  110. console.log(validateUser({
  111. email: "hui-mail.ru",
  112. name: "crappy-nickname",
  113. password: "lol567"
  114. }));
  115.  
  116. // Will print:
  117. // [ 'fields',
  118. // { name: [ 'all', 'no shit' ],
  119. // password: [ 'all', 'has capital chars', '>= 8 chars' ] } ]
  120.  
  121. console.log(validateUser({
  122. email: "hui@mail.ru",
  123. name: "cparry-nickname",
  124. password: "loLd5671"
  125. }));
  126.  
  127. // Will print:
  128. // null
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement