Comments (8)
Not sure if I want to kill anything with my projects but I'd be happy to see people using this gem instead of AR's validations ;)
Anyhow, you have access to all violated rules via schema.(input).failures
. Let me know if that works for you.
from dry-validation.
Thanks for the quick reply!)
Ah, sorry, I've mislookeed, that there was .messages
call at the end in the example.
So, yes, it works as I wanted it to be! It returns result, as vanguard
does :)
Here is just example, if somebody will have the same issue:
require 'dry-validation'
class Schema < Dry::Validation::Schema
key(:email) { |email| email.filled? }
key(:age) do |age|
age.int? & age.gt?(18)
end
end
schema = Schema.new
result = schema.call(email: '', age: 19) # without .messages
p result
# => #<Dry::Validation::Schema::Result params={:email=>"", :age=>19} messages={:email=>[["email must be filled"], ""]}>
p result
[#<Dry::Validation::Rule::Result::Value success?=false input="" rule=#<Dry::Validation::Rule::Value name=:email predicate=#<Dry::Validation::Predicate id=:filled?>>>
P.S.
Unfortunately I don't use the gem yet, but I want to give it a try! The idea is really brialiant.
Thanks!
from dry-validation.
If I'm not wrong, currently the way to access given predicate for an error is:
error = schema.call(input).errors.first
puts error.result.rule.predicate.id
So it is possible to reach to it, but I'm not sure if along the way our old pal Demeter would survive to a heart attack :D
But more than about judge Demeter health I'm worried about relating predicates with error messages, because, as far as I have deduced, there is no straight way.
Now, the output of result.messages
is:
{ attribute: [message_1, message_2] }
Would you consider changing it to:
{ attribute: { predicate_1 => message_1, predicate_2 => message_2 } }
If so, with some time, I could try a pull request if it is not trivial for one of the maintainers.
from dry-validation.
Current messages structure is optimized for displaying error messages in the UI, including predicates there would complicate things. We can introduce another error message structure for cases where people need to know which predicate failed.
from dry-validation.
And what about introducing a way of getting the error message from a Dry::Validation::Error
instance? That way one could iterate over the array of errors and for each one get its predicate and its message, building the JSON response structure as needed. What do you think about it?
from dry-validation.
We already have an API for that, it's ErrorCompiler
, you can create your own and that's all. Validation::Error
is sort-of private
from dry-validation.
from dry-validation.
no worries, it's not even documented yet
from dry-validation.
Related Issues (20)
- 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
- Define contract with namespace but got TypeError when use errors(full: true)
- Wrong number of arguments HOT 3
- Getting proc instead of string message
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.