#769 ✓resolved
Nicholas Orr

Autovalidation of BigDecimal should mention scale and precision in error message

Reported by Nicholas Orr | January 14th, 2009 @ 05:43 PM | in 0.10.0


class Pop
  include DataMapper::Resource

  property :percent, BigDecimal, :scale => 2, :precision => 4

end
percent = <BigDecimal:2afb413904b0,'0.2857142857 1428571428 5714285714 2857142857 1428571428 5714285714 2857142857 143E1',81(90)>
DataMapper::Validate::ValidationErrors:0x2afb41397670 @errors={:percent=>["Percent must be a number"]}

As you can see percent is a BigDecimal and it is valid. What isn't valid is that it doesn't fit the params of scale & precision.

What needs to be added to the validation is messages about scale/precision.

Error: "scale is 2, precision is 5, your object is scale 1000, precision 1020"

Comments and changes to this ticket

  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) January 14th, 2009 @ 06:30 PM

    • Tag changed from bigdecimal, validation to bigdecimal, dm-more, dm-validations
    • Assigned user set to “Michael Klishin (antares)”

    I wrote alot of the too-clever number validation code, and I think it can probably be done in a way that provides a better error messages, and is simpler to read and debug. I think numeric validation should be done in four steps:

    1. Match the value against something like /\A[+-]?(?:\d+|\d*.\d+)\z/, otherwise add the error "is not a number".

    2. If the :integer_only option is specified, make sure there is not a decimal point, otherwise add the error "is not an integer".

    3. If the :scale option is specified, make sure the digits to the right of the decimal point are between 1 and scale in length, otherwise add an error (not sure on the best wording).

    4. If the :precision option is specified, make sure the digits to the left of the decimal point are between 0 and (precision - scale) in length, otherwise add an error (not sure on the best wording here either).

    This should cover most of the use cases that the code does now. Am I missing anything?

  • Nicholas Orr

    Nicholas Orr January 14th, 2009 @ 06:40 PM

    Wording for error's 3 & 4:

    "scale is larger then n" "precision is larger then n"

    or

    "scale is greater than defined"

    or

    "is greater than :scale => n" (this one kinda points exactly to what is holding things up...)

  • Michael Klishin (antares)

    Michael Klishin (antares) January 15th, 2009 @ 05:01 AM

    • State changed from “unconfirmed” to “confirmed”
    • Title changed from “Validating a BigDecimal, Add Specific Error Message” to “Autovalidation of BigDecimal should mention scale and precision in error message”

    It would be easy to implement right after I add callable expansion to error messages (so you can use a proc as message value to construct it dynamically), but this week I only work on re-writing specs so changes won't introduce more problem that fixes.

  • Michael Klishin (antares)

    Michael Klishin (antares) March 25th, 2009 @ 05:00 PM

    • Milestone set to 0.10.0
    • State changed from “confirmed” to “accepted”
  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) March 26th, 2009 @ 04:55 PM

    • State changed from “accepted” to “resolved”

    (from [fb4f8899bba05d205379af8723449e5ab43f735e]) [dm-validations] Make it possible to use callable objects for messages, requires Edge Extlib

    This makes it possible to construct complex custom error messages and not add more and more and more ad hoc error messages to default set

    • ValidationErrors objects are now aware of models they are attached to (#model accessor)
    • For pure Ruby objects, if :message is a callable, model (object) is yielded to it
    • For DM resources, if :message is a callable, model AND property are yielded to it

    [#769 state:resolved] http://github.com/datamapper/dm-...

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Referenced by

Pages