Skip to content

Instantly share code, notes, and snippets.

@john-raymon
Last active July 7, 2022 21:26
Show Gist options
  • Save john-raymon/7897c797c1d9671631253f9bb327e900 to your computer and use it in GitHub Desktop.
Save john-raymon/7897c797c1d9671631253f9bb327e900 to your computer and use it in GitHub Desktop.
otherPropertyAddresses: mixed().when('numOtherProperties', () => {
return array().of(
addressValidator(true).test(
'is-address-matching',
'Address must not match rental property address',
(value, options) => {
const { numRentalProperties, rentalPropertyAddresses } = options.from[1].value;
const { address: otherAddress, city: otherCity, state: otherState, zip: otherZip } = value;
const hasAmatch = numRentalProperties && rentalPropertyAddresses.some(({ address = '', city = '', state = '', zip = '' }) => {
return (otherAddress === address && otherCity === city && otherState === state && otherZip === zip);
});
// if hasAMatch is true then fail test
return !hasAMatch;
}
)
);
@john-raymon
Copy link
Author

john-raymon commented Jul 7, 2022

Below you can see the changes I made that resulted in the code above

    otherPropertyAddresses: mixed().when('numOtherProperties', () => {
      return array().of(
        addressValidator(true).test(
          'is-address-matching',
          'Address must not match rental property address',
          (value, options) => {
            const { numRentalProperties, rentalPropertyAddresses } = options.from[1].value;
            const { address: otherAddress, city: otherCity, state: otherState, zip: otherZip } = value;
-          let hasAMatch = false;
-          if (numRentalProperties) {
-              rentalPropertyAddresses.forEach(({ address = '', city = '', state = '', zip = '' }) => {
+              const hasAmatch = numRentalProperties && rentalPropertyAddresses.some(({ address = '', city = '', state = '', zip = '' }) => {
-              if (otherAddress === address && otherCity === city && otherState === state && otherZip === zip) {
-                 hasAMatch = true;
-              }
+              return (otherAddress === address && otherCity === city && otherState === state && otherZip === zip);
              });
            }
            // if hasAMatch is true then fail test
            return !hasAMatch;
          }
        )
      );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment