Coder Social home page Coder Social logo

Comments (8)

solnic avatar solnic commented on August 28, 2024

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.

greyblake avatar greyblake commented on August 28, 2024

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.

waiting-for-dev avatar waiting-for-dev commented on August 28, 2024

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.

solnic avatar solnic commented on August 28, 2024

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.

waiting-for-dev avatar waiting-for-dev commented on August 28, 2024

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.

solnic avatar solnic commented on August 28, 2024

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.

waiting-for-dev avatar waiting-for-dev commented on August 28, 2024

from dry-validation.

solnic avatar solnic commented on August 28, 2024

no worries, it's not even documented yet

from dry-validation.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.