Conditional required field using dry-schema

Hi,

I’d like to create a simple schema where a bar field is needed if other foo bar is not present. For example,

{foo: 'some value'}

should pass validation, but {baz: 'value'} should not, because bar is not present. In my mind, that would be something like

schema = Dry::Schema.Params do
  optional(:foo) {nil?.then ( required(:bar).string ) }
end

Is that possible using only dry-validations ? Thanks in advance!

It’s certainly out of the scope of dry-schema. dry-schema only checks “shape” of data, all validations must be done in “deeper” layers of an up. dry-validation covers most use cases for me but when even it comes short I just write normal validation code, this is the approach.

1 Like

Thanks for you response! I’ll switch to dry-validations :slight_smile: