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

Invalid custom error messages for duplicate keys in multiple namespaces #53

Closed
zkusznir opened this issue Mar 23, 2018 · 3 comments
Closed
Labels
Milestone

Comments

@zkusznir
Copy link

Hi guys, I've encountered a strange behavior in custom error messages for duplicate keys within different namespaces. Example:

I'm having 2 schemas:

# test.rb

student = Dry::Validation.Schema do
  configure do
    config.messages_file = './errors.yml'
    config.namespace = :student
  end

  required(:name).filled
end

teacher = Dry::Validation.Schema do
  configure do
    config.messages_file = './errors.yml'
    config.namespace = :teacher
  end

  required(:name).filled
end

And an errors.yml file with custom error messages:

en:
  errors:
    rules:
      student:
        rules:
          name:
            filled?: Student - name is missing
      teacher:
        rules:
          name:
            filled?: Teacher - name is missing

It seems as if the order in which schemas are called has an effect on the output of error messages. See: run the test.rb script (with a breakpoint after schemas definitions):

[1] pry(main)> teacher.call(name: "").messages
=> {:name=>["Teacher - name is missing"]}
[2] pry(main)> student.call(name: "").messages
=> {:name=>["Teacher - name is missing"]}

Run the script again and call schemas in different order:

[1] pry(main)> student.call(name: "").messages
=> {:name=>["Student - name is missing"]}
[2] pry(main)> teacher.call(name: "").messages
=> {:name=>["Student - name is missing"]}

After calling the first schema an error message for subsequent schemas with duplicate key is repeated (as if it would be cached somewhere) - namespace is ignored. I'm not sure whether it is a bug or I'm just defining my schemas/error messages file in incorrect way. Has anyone encountered the same problem?

@zkusznir
Copy link
Author

zkusznir commented Apr 9, 2018

@solnic Mind me pinging you but this issue is bothering us in the project - do you have any idea whether it's my fault and I should fix something or it's a bug and I should go for a workaround? Thanks in advance for the response!

@jerryskye
Copy link

Hi @zkusznir! I'm having a similar problem. It looks like it's caching the results by object hash of the arguments here:
https://github.com/dry-rb/dry-validation/blob/1d0dcd30fd46a85654edda9389eb484f2eb2a0cc/lib/dry/validation/messages/abstract.rb#L62-L67
That's why it returns the cached result, in your case it receives the :name again.
Please let me know if you solved this issue 😁

@solnic
Copy link
Member

solnic commented Oct 2, 2018

Sorry for late reply, I was completely "off duty" in March/April. It's clearly a bug. I'm wrapping up dry-schema where this will be fixed.

@solnic solnic transferred this issue from dry-rb/dry-validation Feb 8, 2019
@solnic solnic added the bug label Feb 16, 2019
@solnic solnic added this to the 1.0.0 milestone Feb 16, 2019
solnic added a commit that referenced this issue Feb 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants