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

Key validation shouldn't happen when no schema for a hash is set #452

Open
flash-gordon opened this issue Jan 19, 2023 · 6 comments
Open

Comments

@flash-gordon
Copy link
Member

Describe the bug

There can be a case when no schema for a hash is set. In such a case, there's not need to validate keys. I might be wrong but it's just logical to me.

To Reproduce

DemoSchema = Dry::Schema.define do
  config.validate_keys = true

  required(:addresses).array(:hash)
end

DemoSchema.call({addresses: [{number: "oo"}]}) 
=> #<Dry::Schema::Result{:addresses=>[{:number=>"oo"}]} errors={:addresses=>{0=>{:number=>["is not allowed"]}}} path=[]>

Expected behavior

The validation passes. We didn't specify any key for nested hashes, key validation shouldn't be applied in such cases. I would expect such values to be validated at later stages with rules or custom code.

My environment

  • Affects my production application: NO
  • Ruby version: 3.2.0
  • OS: N/A
@flash-gordon
Copy link
Member Author

Discovered in #450

@jmtame
Copy link

jmtame commented May 17, 2023

Bumping this

@JanecekJ
Copy link

This would be really helpful.

@JanecekJ
Copy link

Is there any way how to bump this up more than giving the like?

Or is there any way how to achieve similar behaviour?

What I would need is for validation to throw error, if the input hash has a key (attribute) I am not expecting, but also allow one of the defined keys which value is hash with undefined keys/structure.

Example to allow ({*} is hash with undefined keys/structure)

{
  'foo' => 'abc',
  'bar' => {*}
}

Example to disallow (because baz is not specified/allowed)

{
  'foo' => 'abc',
  'bar' => {*},
  'baz' =>'def'
}

JanecekJ added a commit to JanecekJ/dry-schema that referenced this issue Oct 18, 2023
@JanecekJ
Copy link

@flash-gordon Hello, I managed to put together some code, that is trying to tackle this issue. I am not really sure if it is the right approach. Also not really sure if it is already in a state for PR.

https://github.com/JanecekJ/dry-schema/tree/fix-hash-without-schema-validation

Can you maybe give me a pointer if this could work or it is not a way to go?

@DangerDawson
Copy link

@JanecekJ did you persevere with this fix? as I to have stumbled across this issue and it is blocking a feature I want to progress with

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

4 participants