jfairbank / revalidate

Elegant and composable validations
http://revalidate.jeremyfairbank.com
MIT License
363 stars 20 forks source link

Accessing other fields in array structure #63

Open shunxing opened 5 years ago

shunxing commented 5 years ago

Hi I'm trying to create a validator on an array of passenger (object structure). In each passenger, I have his firstName and lastName.

I have to trigger firstName and lastName error when we have the same passenger name (first name + last name).

I'd like to know if we can easily access the firstName in the lastName composeValidator and also in the other way back (accessing to the field lastName in the firstName composeValidator)

Is it a great way to do so ?

"passengers[]": {
    name: createValidator(
        () => passengerName => {
            return {
                lastName: lastNameValidator(passengerName.firstName),
                firstName: firstNameValidator(passengerName.lastName),
                gender: isRequired({
                    message: validationMessages.genderRequired,
                }),
            };
        },
        message
    )(message),