Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"NoMethodError" error raised when validating nil value with maybe(:array) #390

Closed
hieuk09 opened this issue Dec 19, 2021 · 6 comments
Closed

Comments

@hieuk09
Copy link
Contributor

hieuk09 commented Dec 19, 2021

Describe the bug

When using maybe(:array), if the value is nil, error is raised instead of returning success object.

To Reproduce

schema = Dry::Schema.JSON do
  required(:list).maybe(:array).each(:string)
end

schema.call(list: nil) # raise NoMethodError

Expected behavior

It should be consistent with maybe behavior for other types and return a result object, something like:

#<Dry::Schema::Result{:list=>nil} errors={} path=[]>

My environment

  • Affects my production application: NO
  • Ruby version: 3.0.2
  • OS: MacOS
@flash-gordon
Copy link
Member

I think it's just maybe doesn't compose with each, use the lesser-known DSL instead required(:list).maybe(array[:string]).

@flash-gordon
Copy link
Member

Alternatively:

schema = Dry::Schema.JSON do
  required(:list).maybe(:array) do
    nil? | each(:string)
  end
end

@hieuk09
Copy link
Contributor Author

hieuk09 commented Dec 20, 2021

Thanks for the fast response! These options work for me. Do you mind if I update the doc to include them so that people are less confused when using maybe?

@flash-gordon
Copy link
Member

@hieuk09 sure, feel free!

@bautrey37
Copy link

There is inconsistent behaviour now, because on dry-validation it is possible to use a rule.each on a maybe value. dry-rb/dry-validation#709

Maybe this should be fixed instead of documenting a caveat.

@flash-gordon
Copy link
Member

I don't think it's a bug in the first place. OTOH, it's safe to be changed, it seems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants