Validation required on creation, optional on update

I’m trying to create a validation that requires an attribute to be present upon record creation, but upon updating the record, the attribute can either be not present in the passed params or if it is present, it is a valid size.

 required(:foo) { filled? & str? & size?(8..24) }

Is what I’m trying to do possible with one validator (if so, how?) or do I need to create two separate validators?

My feeling is that you need two separate validations: you have two different use cases. dry-validation is geared towards use cases, rather than database model validation. Since you have two separate use cases, it seems to make more sense to have two schemas.

There is an alternative, where you could use rules. I’m writing this from memory so it’s full of errors, but it might put you on the right track:

optional(:foo) { filled? & str? & size?(8..24) }
optional(:cond) { filled? & boolean? }

rule(:foo, :cond) do
  key.failure("required") if values[:cond] && !key[:foo]
end

Hope that helps!
François