Skip to content

Conversation

rindek
Copy link
Contributor

@rindek rindek commented Apr 8, 2021

This was not reported on https://discourse.dry-rb.org/, though I think it can be treated as "typo", thus just straight PR:

When using Types.Interface, error is not produced in the Dry::Schema::Result

Ex.

Dry::Schema.Params { required(:a).filled(Types.Interface(:inv)) }.call(a: 1)
=> #<Dry::Schema::Result:0x1680>

The reason behind that is because errors message compiler can't find translation:

Dry::Schema::MissingMessageError (Message template for :a under "" was not found. Searched in:)
"en.dry_schema.errors.rules.a.respond_to?.arg.default"
"en.dry_schema.errors.rules.a.respond_to?"
"en.dry_schema.errors.respond_to?.failure"
"en.dry_schema.errors.respond_to?.value.a"
"en.dry_schema.errors.respond_to?.value.default.arg.default"
"en.dry_schema.errors.respond_to?.value.default"
"en.dry_schema.errors.respond_to?.arg.default"
"en.dry_schema.errors.respond_to?"

Expected:

Dry::Schema.Params { required(:a).filled(Types.Interface(:inv)) }.call(a: 1)
=> #<Dry::Schema::Result{:a=>1} errors={:a=>["must respond to inv"]}>

Adding a translation to respond_to? fixes the above issue

@rindek rindek requested a review from solnic as a code owner April 8, 2021 12:31
Copy link
Member

@solnic solnic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this makes sense. Any chance you could create a simple spec under spec/integration/schema/predicates/respond_to_spec.rb?

@rindek rindek force-pushed the fix-missing-translation branch from 31b7598 to 7cd0c1c Compare April 9, 2021 13:48
@rindek
Copy link
Contributor Author

rindek commented Apr 9, 2021

@solnic I've added tests but I managed to test only macro-way, as I found a bug

It looks like defining schema passing predicate directly to require/optional is broken

Dry::Schema.define { required(:foo) { respond_to?(:bar) } }
NoMethodError: undefined method `to_ast' for false:FalseClass
Did you mean?  to_s
from /Users/rindek/projects/dry-rb/dry-schema/lib/dry/schema/trace.rb:89:in `map'

My guess is that respond_to? in this case is calling Object#respond_to? instead of predicate definition from dry-logic

I've skipped failing tests, although I'm not sure what should be done with that bug

@solnic
Copy link
Member

solnic commented Apr 12, 2021

My guess is that respond_to? in this case is calling Object#respond_to? instead of predicate definition from dry-logic

Correct. If you add undef :respond_to? here "lib/dry/schema/macros/dsl.rb" then it'll work.

@rindek
Copy link
Contributor Author

rindek commented Apr 12, 2021

@solnic I've added undef in the dsl, tests are green on MRI, but some fail on jruby (others than what I added)
Unfortunately I don't know jruby that much to figure out what exactly is the root cause, do you maybe have an idea?

@solnic
Copy link
Member

solnic commented Apr 13, 2021

some fail on jruby (others than what I added)
Unfortunately I don't know jruby that much to figure out what exactly is the root cause, do you maybe have an idea?

thanks, this is confusing - I need to debug this under jruby :( The truth is, we should replace respond_to? predicate with some non-conflicting name. It was reported at least once that this predicate caused issues. Definitely something to do in 2.0.0.

flash-gordon added a commit to dry-rb/dry-logic that referenced this pull request Apr 13, 2021
This doesn't really change much but fixes a subtle issue with jruby found in dry-rb/dry-schema#348 It's hard to say whether it's a bug in jruby because it's an intersection of `undef :respond_to?`, `BasicObject` and implicit coercion to keywords. Keywords situation will improve over time so I think jruby shouldn't bother fixing this particular case.
@flash-gordon
Copy link
Member

I'll cut a minor release in dry-logic, this should fix jruby failures dry-rb/dry-logic#85

@solnic solnic merged commit 781cfc9 into dry-rb:master Apr 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants