Execute contract rules also when the schema is not valid?

Hello everyone!

I read from the dry-validation rules page that key-specific rules are not executed when the schema is not valid.

is there a way to enforce the rules also when the schema is not valid?

We’re looking to return all the errors in our API response. This would let the client see every issue at once, rather than first getting one schema error and then having to make more requests to find out about additional contract errors.

Hope that clarifies what we’re trying to do :slight_smile:

Hi @sekmo, and thanks for the question! You’ve explained yourself well, I totally understand what you’re looking to do.

In fact, what you’re looking to do here sounds a lot like dry-schema’s hints extension:

In addition to error messages, you can also access hints, which are generated from your rules. While errors tell you which predicate checks failed, hints tell you which additional predicate checks were not evaluated because an earlier predicate failed.

However, this is a little trickier to achieve within dry-validation rules, because unlike dry-schema predicates, which are defined more as “data”—just as predicate names and options—dry-validation rules are instead defined by arbitrary Ruby code. The reason that Ruby code doesn’t execute if their dependent keys are not valid is because that Ruby code may actually crash if those values are not in their expected states.

So all of this is to say: right now, what you’re looking to achieve isn’t something that dry-validation offers, for the reason above.

Of course, you (or someone else) would certainly be welcome to explore introducing this feature. :smile: Doing this well would come down to figuring out an approach for extracting the potential error messages from rule methods while still maintaining a straightforward and streamlined developer experience for actually writing those rule methods.

I’ll just cc @flash-gordon and @solnic in case they have any other thoughts to add here.

Generally, dry-validation type checks code against a given schema. You cannot run rules against untyped data, this doesn’t make sense to me, to be honest. But if we’re talking about non-type predicates then yes, I think it should be possible, in theory. One can imagine an extension that extracts all non-type predicates and replaces them with rules, so all rules are run simultaneously. This does sound like a whole bunch of work, though :slight_smile:

Sorry I just realized I never replied!
Thank you very much for the detailed answers @timriley and @flash-gordon, after reading them it totally makes sense to run the rest of the rules only when the schema is successfully processed :slight_smile:

1 Like