Inconsistent behavior when inheriting with .Schema, .Form, and .JSON


#1

I was originally going to file an issue on Github, but I don’t know if this is inconsistent behavior or intended.

Consider this basic schema:

schema = Dry::Validation.Schema { required(:name).filled(:str?) }

If I want to extend this schema, I can do something like this:

new_schema = Dry::Validation.Schema(schema) { required(:age).filled(:int?) }

However, if I try to do the same using .Form or .JSON, there’s a problem:

form = Dry::Validation.Form(schema) { required(:age).filled(:int?) }

=> TypeError: superclass must be a Class (#<Class:0x007f82969fc818> given)
from /gems/dry-validation-0.11.0/lib/dry/validation.rb:28:in `initialize'

This works fine:

form = Dry::Validation.Form(schema.class) { required(:age).filled(:int?) }

.Schema handles this situation with the following code:

schema_class = Class.new(base.is_a?(Schema) ? base.class : base)

However, .Form and .JSON assume they’re dealing with a class so they fail before calling .Schema. To me, this behavior was unexpected and required a code dive to understand what was going on. With that said, I can’t honestly say if it’s a bug.

If this behavior is considered a bug, I can put together a PR pretty quickly. (The guidelines say “A Pull Request will only be accepted if it addresses a specific issue that was reported previously” and since I can’t tell if this is even a bug I dunno if it’s an issue, lol. Maybe I’m being way too pedantic.)