Giter VIP home page Giter VIP logo

adequate_errors's People

Contributors

lulalala avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

iq-scm

adequate_errors's Issues

Ability to turn off attribute prefix in message

Sometime we want to not prefix the error message with attribute name. Assigning error to :base works but what if attribute is important (for example to be included in json).

That is, to make attribute prefixing optional.

Attribute/Model interpolation always capitalized

When %{attribute} or %{model} are used in the error message, they are always capitalized. This can look weird when they are placed at the middle of the sentence.

This is minor though, as the easy workaround is to hard code attribute name in the translation.

Error related value used in both formatted and raw ways

I have a validation on an amount value. It is checked against a dynamic minimum value (think it as some currency exchange). So if the amount is not over the minimum value, the error message will say "You still need X more in order to reach the minimum".

The X here is the difference between user entered amount and the minimum. There is a requirement to have it formatted as currency, with dividers, e.g. $1,234.56.

Usually I can just say errors.add(:foo, :not_enough, lacking: format_currency(num)) to have the formatted text in the error messages.

However I also have a json api, which needs this value as float, for some branching logic. I can't just use the lacking value in details, because it would be a string. I have to add the raw value errors.add(:foo, :not_enough, lacking: num, lacking_formatted: format_currency(num)).

If I want to avoid adding two keys, one for raw value and one for formatted valu, what kind of API would help?

Error messages not evaluated lazily

When errors are added, its messages are evaluated immediately.

Ideally it should only be evaluated at the very last moment, e.g. when full_messages are called.

user = User.new

I18n.with_locale(:en) { user.errors.add(:email, :invalid) }

expecting:

I18n.with_locale(:pl) { user.error.full_messages } # => outputs PL errors
I18n.with_locale(:pt) { user.error.full_messages } # => outputs PT errors

actual:

I18n.with_locale(:pl) { user.error.full_messages } # => outputs EN errors always

Courtesy from @morgoth

Non-Rails usage.

Hi.

Since this gem intercepts Rails errors, does it mean that it is is Rails only? Or can it be used in other settings, like my own gems or Roda/Sinatra/Hanami?

Accessor for specific error

Sometimes it is desirable to access a specific error details

foo.errors.details[:amount].find { |error| error[:error] == :too_small }

It would be better to have a method to allow for this.

NoMethodError when calling messages on Form Object

undefined method`foo' for #

triggered when calling form_object.errors.adequate.messages.

Form objects usually need to copy error messages from inner objects to itself.

In the past when messages are eagerly evaluated, it works fine.

However in AdequateErrors, messages are lazily evaluated. So when we call form_object.errors.adequate.messages, it will try to access the attribute from form object, which results in undefined method error.

Possible solution is to:

  1. Store attribute value in Error Object
  2. Implement a copy/dup for Error Object to copy across errors.
  3. Utilize this value when generating messages, instead of re-reading it from model.

Merge errors from other models and keep details

Sometimes it is desired to merge errors from associations or child models. For example form objects need to merge validation errors from its child models.

However, if we are to merge, it may not be possible to copy across the details and message at the same time. If we merely copied the details, those details in the form object probably won't be able to generate messages, because it does not have corresponding locale translations on form object scope.

That leaves us having to copy across the full messages only, losing details.

An example of this is in ActiveInteraction https://github.com/AaronLasseigne/active_interaction/blob/v4.0.0/lib/active_interaction/errors.rb#L136-L143

This can be solved by introducing the idea of nested error object.

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.