There are several ways to inherit schema without necessarily writing the class.
The key to the first one is the build
option in the Dry::Validation.Schema
method:
S1 = Dry::Validation.Schema(build: false) do
required(:name).filled
end
S2 = Dry::Validation.Params(S1) do
required(:age).filled(:int?, gt?: 0)
end
Be warned, however, that S1
in this case would be the class of the schema, not the schema itself. So instead of doing this:
S1.({name: 'William'})
You will have to do this:
S1.new.({name: 'William'})
The second option is just use the parent schema’s class in the child schema:
S1 = Dry::Validation.Schema do
required(:name).filled
end
S2 = Dry::Validation.Params(S1.class) do
required(:age).filled(:int?, gt?: 0)
end