Advertisement
Guest User

Untitled

a guest
Jul 1st, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. function linkmodel() {
  2. return {
  3. title: {
  4. content: undefined,
  5. validation: {
  6. type: "string",
  7. required: true,
  8. minLength: 1,
  9. maxLength: 3,
  10. validationErrorMessage: "Your title must be a valid string between 1 and 35 characters"
  11. }
  12. },
  13.  
  14. email: {
  15. content: undefined,
  16. validation: {
  17. type: "email",
  18. required: true,
  19. minLength: 1,
  20. maxLength: 60,
  21. validationErrorMessage: "Your email must be between 1 and 50 characters"
  22. }
  23. },
  24.  
  25.  
  26. link: {
  27. content: undefined,
  28. validation: {
  29. type: "url",
  30. required: true,
  31. minLength: 1,
  32. maxLength: 500,
  33. validationErrorMessage: "Your link name must be a valid email between 1 and 50 characters"
  34. }
  35. },
  36.  
  37. description: {
  38. content: undefined
  39. }
  40. }
  41. }
  42.  
  43.  
  44. export default linkmodel
  45.  
  46. app.post( "/", function( req, res ) {
  47. let form = new forms.IncomingForm()
  48.  
  49. form.parse( req, function( err, fields, files ) {
  50.  
  51.  
  52. let errorMessageBag = _.flow( objectWithFieldsToValidate, requiredFields, stringLengthValidationCheck, emailValidation )
  53. let result = errorMessageBag(fields, linkmodel())
  54. console.log( "result", result ) // That's the end result
  55. })
  56.  
  57.  
  58.  
  59. // Return object containing all fields to validate and their criterias
  60. function objectWithFieldsToValidate( fields, model ) {
  61. // Remove all model fields that have no validation criteria in odel
  62. let modelFieldsToValidate = _.pickBy( model, function( value, key ) { return value.validation !== undefined })
  63.  
  64. // Remove from form field any entry that doesn't have a corresponding key in model
  65. let formFieldsToValidate = _.pick( fields, Object.keys( modelFieldsToValidate ) )
  66. _.forOwn( modelFieldsToValidate, function( value1, key1 ) {
  67. _.forOwn( formFieldsToValidate, function( value, key ) {
  68. if ( key1 === key ) {
  69. modelFieldsToValidate[ key ].content = value
  70. }
  71.  
  72. })
  73. })
  74. return modelFieldsToValidate
  75. }
  76.  
  77. // Take care of required fields
  78. function requiredFields( objectWithFieldsToValidate ) {
  79. let okField = {}
  80. let errors = {}
  81.  
  82. // error: field required but empty: add it to errors literal object
  83. _.forOwn( objectWithFieldsToValidate, function( value, key ) {
  84. if ( objectWithFieldsToValidate[ key ].validation.required === true && objectWithFieldsToValidate[ key ].content === "" ) {
  85. errors[ key ] = objectWithFieldsToValidate[ key ].validation.validationErrorMessage
  86. } else {
  87. // no error: add field to litteral okField
  88. okField[ key ] = value
  89.  
  90. }
  91. })
  92. return ( { "okField": okField, "errors": errors })
  93. }
  94.  
  95.  
  96. function stringLengthValidationCheck( requiredFields ) {
  97. let validatedFields = requiredFields
  98. _.forOwn( validatedFields.okField, function( value, key ) {
  99. // Error: field length is not valid
  100. if ( !validator.isLength( value[ "content" ],
  101. { min: value[ "validation" ].minLength, max: value[ "validation" ].maxLength }
  102. ) ) {
  103. // Add error message to errors errors literal object
  104. validatedFields[ "errors" ][ key ] = value[ "validation" ].validationErrorMessage
  105. // Remove problematic field from okFields
  106. delete validatedFields[ "okField" ][ key ]
  107. }
  108. })
  109.  
  110. return validatedFields
  111. }
  112.  
  113.  
  114. function emailValidation( stringLengthValidationCheck ) {
  115. let validatedFields = stringLengthValidationCheck
  116. _.forOwn( validatedFields.okField, function( value, key ) {
  117. // Error
  118. if ( value["validation"]["type"] === "email" && !validator.isEmail( value[ "content" ])) {
  119. // Add error message to errors
  120. validatedFields[ "errors" ][ key ] = value[ "validation" ].validationErrorMessage
  121. // Remove problematic field from okFields
  122. delete validatedFields[ "okField" ][ key ]
  123. }
  124. })
  125.  
  126. return validatedFields
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement