codeforsanjose / disaster-response-sj

Exploration of adding civic tech component to San Jose disaster response
MIT License
7 stars 12 forks source link

Refactor validation related functions to more generalized form, expand some validation logic, add unit tests #52

Closed sunnymui closed 5 years ago

sunnymui commented 5 years ago

Refactored validatePostDetails in AdminContainer.js to pull the corresponding validation function from a hash table of validation utility functions. Also avoids doing spread operations every time the reduce callback is executed to build the accumulated errors object with a more hash table construction style.

Validation logic for each postField that was happening in the if statements was moved to individual validation functions in the validationUtilities.js file. It's now grouped in the the isInvalid object. Had to add the option to call isInvalid functions with a default which works if a postField does not have a specifically defined validation.

Expanded some of the validations:

Wanted to make sure I didn't mess anything up so I added 8 unit tests in AdminContainer.test.js to check that everything was validating as expected and to check that all the stuff I changed didn't break something. I just checked it by running npm test

I also had to reinstall the dependencies to make it run. I think a module got added on the dev branch that I didn't have on my repo so I had to add it.

sunnymui commented 5 years ago

To resolve merge conflicts, I just defaulted every conflict to newer written code.

Since I wrote updated validation utilities for an older version of the posting logic, I just took out the usage of the validation utility functions for now in the updated logic. Things for the admincontainer were were moved around into separate files (adminform.js) so I'll leave implementing the utilities into the actual in-use logic for a future commit.

Should just have to look at the validateField function in adminForm.js, around line 81 - 120 and replace the if statements with lookups in the validation utilties.

The actual utility functions are in the separate validationUtilities.js file.