Multi-Step Validation
Creating validators that reference data from earlier validation steps
Last updated
Creating validators that reference data from earlier validation steps
Last updated
Say we're receiving data that is supposed to conform to the following TypeScript interface:
We'd like to create a validator instance that verifies that the data we're receiving actually conforms to the above interface. In addition to this, we'd also like to verify that the strings found in <csvData>.metadata.keys
are keys on each object found in <csvData>.entries
. How would we do this? How do we configure part of a validator to change its behavior, based on data that was found earlier in the validation process?
Turns out, gives us just what we need to accomplish this.
So what's going on here?
We begin by using &
to break up the validation into two steps. First, everything to the left of the &
must be validated. Only after the left-hand side is validated will the right-hand side start executing. The callback provided to validator.lazy()
will receive, as a parameter, the data that it is in charge of validating. Because validator.lazy()
is interpolated after we've validated the metadata
property, we know with certainty that the csvData
received by the callback has, at a minimum, a metadata
property that's well-formed. Inside the .lazy()
callback, we can extract csvData.metadata.keys
and use it to configure how the next half will be validated.
Any time data from earlier in the validation process needs to be used to configure how the validator behaves later on, this general pattern can be used.
Keep in mind that in simpler cases, .expectTo()
can also be used to accomplish the same objective. Let's consider a similar problem:
The idea is similar, but this time, in addition to validating that the data conforms to the above interface, we want to assert that the length of <csvData>.entries
is equal to <csvData>.metadata.count
. The solution?
We can use a .expectTo()
in this scenario because it's fairly easy for the callback to access and verify the length of .entries
.