Guest User

Untitled

a guest
Mar 23rd, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.63 KB | None | 0 0
  1. var validation_rules = checkSchema({
  2. company_name: {
  3. errorMessage: 'Company Name should be at least 3 chars long and maximum of 50 chars',
  4. isLength: {
  5. options: { min: 3, max : 50 }
  6. },
  7. trim: true
  8. },
  9. profile_name: {
  10. errorMessage: 'Public Profile Name should be at least 3 chars long and maximum of 50 chars',
  11. isLength: {
  12. options: { min: 3, max: 50 }
  13. },
  14. trim: true
  15. },
  16. description: {
  17. errorMessage: 'Company Description should be at least 2 chars long and maximum of 200 chars',
  18. isLength: {
  19. options: { min: 2, max: 200 }
  20. },
  21. trim: true
  22. },
  23. company_street_address: {
  24. errorMessage: 'Company Address should be at least 3 chars long and maximum of 100 chars',
  25. isLength: {
  26. options: { min: 3, max: 100 }
  27. },
  28. trim: true
  29. },
  30. company_city: {
  31. errorMessage: 'City should be at least 5 chars long and maximum of 50 chars',
  32. isLength: {
  33. options: { min: 5, max: 50 }
  34. },
  35. trim: true
  36. },
  37. company_state: {
  38. errorMessage: 'Please select state',
  39. matches: {
  40. options: [/^[0-9]+$/],
  41. errorMessage: "Please enter digits"
  42. },
  43. trim: true
  44. },
  45. company_zip: {
  46. errorMessage: 'Zip Code should be at least 4 chars long and maximum of 6 chars',
  47. isLength: {
  48. options: { min: 4, max: 6 }
  49. },
  50. trim: true
  51. },
  52. first_name: {
  53. errorMessage: 'First Name should be at least 3 chars long and maximum of 50 chars',
  54. isLength: {
  55. options: { min: 3, max: 50 }
  56. },
  57. trim: true
  58. },
  59. last_name: {
  60. errorMessage: 'Last Name should be at least 3 chars long and maximum of 50 chars',
  61. isLength: {
  62. options: { min: 3, max: 50 }
  63. },
  64. trim: true
  65. },
  66. email: {
  67. errorMessage: 'Please enter a valid email address',
  68. isEmail : true,
  69. trim: true,
  70. custom: {
  71. options: (value) => {
  72.  
  73. if(value)
  74. {
  75. return new Promise(function (resolve, reject) {
  76.  
  77. dns_validate_email.validEmail(value, function(valid) {
  78. console.log('valid:'+valid);
  79. if (valid) {
  80. resolve(value)
  81. }else{
  82. reject(new Error({errorMessage: 'Not a valid email'}));
  83. }
  84. });
  85.  
  86. }).then(function (email) {
  87.  
  88. return new Promise(function (resolve, reject) {
  89. User.findBy('email', email, function (err, result) {
  90. if(err){
  91. reject('Unable to validate email')
  92. }else{
  93. console.log(result);
  94. resolve(result);
  95. }
  96.  
  97. });
  98. }).then(function(result){return result.length===0});
  99.  
  100.  
  101.  
  102. })
  103. }
  104.  
  105. },
  106. errorMessage: 'This email is already in use',
  107. }
  108. },
  109. phone: {
  110. errorMessage: 'Please enter Phone Number in 10 digits',
  111. isLength: {
  112. options: { min: 10, max: 10 }
  113. },
  114. matches: {
  115. options: [/^d{10}$/],
  116. errorMessage: "Please enter digits"
  117. },
  118. trim: true
  119. },
  120. street_address: {
  121. errorMessage: 'Company Address should be at least 3 chars long and maximum of 100 chars',
  122. isLength: {
  123. options: { min: 3, max: 100 }
  124. },
  125. trim: true
  126. },
  127. city: {
  128. errorMessage: 'City should be at least 5 chars long and maximum of 50 chars',
  129. isLength: {
  130. options: { min: 5, max: 50 }
  131. },
  132. trim: true
  133. },
  134. state: {
  135. errorMessage: 'Please select state',
  136. matches: {
  137. options: [/^[0-9]+$/],
  138. errorMessage: "Please enter digits"
  139. },
  140. trim: true
  141. },
  142. zip: {
  143. errorMessage: 'Zip Code should be at least 4 chars long and maximum of 6 chars',
  144. isLength: {
  145. options: { min: 4, max: 6 }
  146. },
  147. trim: true
  148. },
  149. username: {
  150. errorMessage: 'Please enter username',
  151. isLength: {
  152. options: { max: 25 },
  153. errorMessage: 'Username should not be greater than 25 chars',
  154. },
  155. trim: true,
  156. custom: {
  157. options: (value) => {
  158. console.log(value.length)
  159. if(value.length>0)
  160. {
  161. return new Promise(function (resolve, reject) {
  162.  
  163. User.findBy('username', value, function (err, record) {
  164. if (err) {
  165. reject('Error validating username');
  166.  
  167. }else{
  168. console.log('--------'+record)
  169. resolve(record)
  170. }
  171.  
  172. });
  173.  
  174. }).then(function (result) {
  175. return result.length===0;
  176.  
  177. })
  178. }
  179.  
  180. },
  181. errorMessage: 'This username is already in use',
  182. }
  183. },
  184. password: {
  185. isLength: {
  186. errorMessage: 'Password should be at least 6 chars long',
  187. // Multiple options would be expressed as an array
  188. options: { min: 6 }
  189. }
  190. },
  191. confirm_password: {
  192. errorMessage: 'Must have the same value as the password field',
  193. custom: {
  194. options: (value, { req }) => value === req.body.password
  195. }
  196. },
  197. payment_mode:{
  198. errorMessage: 'Please select payment option',
  199. isIn: {
  200. options: [['Credit Card', 'Monthly Invoice']],
  201. }
  202. },
  203. // Wildcards/dots for nested fields work as well
  204. 'card.number': {
  205. errorMessage: 'Please enter card number',
  206. custom: {
  207. options: (value, { req }) => {
  208.  
  209. if(req.body.payment_mode==="Credit Card"){
  210. return /^[0-9]{12,19}$/.test(value);
  211. }else{
  212. return true;
  213. }
  214.  
  215. },
  216. errorMessage: 'Card number must be of 12 and maximum of 19 digits',
  217. }
  218. },
  219. 'card.type': {
  220. errorMessage: 'Please select card type',
  221. custom: {
  222. options: (value, { req }) => (req.body.payment_mode==="Credit Card" && value=='')?false:true,
  223. }
  224. },
  225. 'card.exp_month': {
  226. errorMessage: 'Please select expiration month',
  227. custom: {
  228. options: (value, { req }) => (req.body.payment_mode==="Credit Card" && value=='')?false:true,
  229. },
  230. trim :true
  231. },
  232. 'card.exp_year': {
  233. errorMessage: 'Please select expiration year',
  234. custom: {
  235. options: (value, { req }) => (req.body.payment_mode==="Credit Card" && value=='')?false:true,
  236. },
  237. trim :true
  238. },
  239. // Wildcards/dots for nested fields work as well
  240. 'card.cvv': {
  241. errorMessage: 'Please enter card number',
  242. custom: {
  243. options: (value, { req }) => {
  244.  
  245. if(req.body.payment_mode==="Credit Card"){
  246. return /^[0-9]{3,4}$/.test(value);
  247. }else{
  248. return true;
  249. }
  250.  
  251. },
  252. errorMessage: 'Security code number must be of 3 and maximum of 4 digits',
  253. }
  254. }
  255. });
Add Comment
Please, Sign In to add comment