Comments (2)
Hey there, thanks for the report. There a couple of issues here. First of all root.filled?
is not needed when you have root.hash?
, which implies that if it's there and it's a hash then it's definitely filled. Secondly, calling multiple predicates without composing them logically with &
/ |
operators is not supported; in general every block must return a rule and it's going to be composed with the outer one. The exception is key
construct, which is special, as it establishes a set of rules that can be applied to its value, that's why you can define nested rules for many keys.
In other words:
class Schema < Dry::Validation::Schema
key(:foo) do |foo|
# this won't work because the last rule returned is composed with the outer `key` rule:
foo.filled?
foo.size?(10..100)
# you want this:
foo.filled? & foo.size?(10..100)
end
end
And here's a working example of what you tried to do:
require 'json'
require 'dry-validation'
require 'dry/validation/schema/form'
class StatSchema < Dry::Validation::Schema::Form
key(:stats) do |stats|
stats.hash? do
stats.key(:strength) do |strength|
strength.key(:description, &:filled?)
end
end
end
end
json = JSON.parse <<-EOT
{
"stats": {
"strength": {
"description": "Increases physical damage"
}
}
}
EOT
schema = StatSchema.new
puts schema.messages(json).inspect
# #<Dry::Validation::Schema::Result params={:stats=>{:strength=>{:description=>"Increases physical damage"}}} errors=[]>
Notice that I used Schema::Form
(although we may want to introduce Schema::JSON
too!) which is needed for json input, as keys are stringified and you may want to use coercions too, which are automatically configured based on your type expectations. Also notice that each
is not needed as its purpose is to be used with arrays, not hashes.
Oh and it's using master, as I improved many things, I'll release 0.2.1 later today.
Having said all of that, I have to admit that we could do something to provide meaningful feedback when rules are defined incorrectly, I believe it should be possible. In example we could check if somebody is trying to use each
against an input that is expected to be something else than an array and in that case simply raise a meaningful error. The same applies to defining multiple predicates without composing them logically with operators, in that case we also want to raise an error, alternatively we may just assume that all predicates should be composed with &
implicitly, but I'm not sure if that's a good strategy since it wouldn't be explicit from the dsl pov and may confuse people. Remember that rules are applied using predicate logic, so foo.str? & foo.size?(10..100)
won't check the size when foo is not a string, this is a huge difference between dry-validation and most (or all) of the other validation libs in Ruby.
I hope this is helpful and thanks for trying out this lib :)
from dry-validation.
Oh wow! Thanks for the informative and quick reply. I'll keep this in mind, fantastic library by the way! Using it to validate JSON (for now) for a card game I'm creating with some friends. Thanks again! 😄
from dry-validation.
Related Issues (20)
- Contract do not find schema keys defined with nested Dry Params. HOT 6
- Inconsistent behavior in nested schemas via passed in blocks HOT 8
- Incorrect validation of rule keys substring of schema keys
- Injecting dependencies using dry-auto_inject with reserved names
- `rule.each` produces error when input is `nil` HOT 2
- Building contract failed when or-ing types HOT 2
- Validation passes when array type is invalid HOT 1
- NoMethodError is raised when validating non-hash objects with a Dry::Validation::Contract that has config.validate_keys set to true HOT 4
- Dry::Validation::MissingMessageError after update dry stack HOT 2
- Weird issue since 1.10 dry-schema release HOT 3
- Validator didn't recognize nil string param value as empty, and didn't set default value HOT 6
- JSON schema contract failing to recognise valid input HOT 2
- Rule validation does not show in errors when the key validated is in an array of hashes HOT 1
- Mixing of `~>` and `<` in gemspec versioning HOT 1
- Documentation missmatch: `predicates_as_macros` is not available until v1.2
- `Dry::Validation::Result` lacks an `#output` method similar to `Dry::Schema::Result` HOT 4
- `Dry::Validation::Contract` behaves different when defined with a `params Dry::Schema::Params(parent: RawSchema)` vs. `params ParamsSchema` HOT 2
- errors(full: true), for nested input, could be better HOT 1
- False-positive when validating a nested datetime in an array of hashes HOT 2
- Consider release a new version with updated dependencies HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dry-validation.