Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Form Validation crashes on invalid values in decimal-fields #65

Closed
mavuio opened this issue Dec 30, 2022 · 3 comments
Closed

Form Validation crashes on invalid values in decimal-fields #65

mavuio opened this issue Dec 30, 2022 · 3 comments
Labels
bug Something isn't working

Comments

@mavuio
Copy link

mavuio commented Dec 30, 2022

Describe the bug

Form validation on an "for_update" form crashes with

** (Decimal.Error) : number parsing syntax: "x"

when passing a value for a decimal field which cannot be parsed to decimal (e.g. "x")

To Reproduce

error is triggered in call to Comp.not_equal? in AshPhoenix.Form.attributes_changed?/1
you can trigger the error standalone like this:

Comp.not_equal?(Elixir.Decimal.new("1.5"),"x")

Expected behavior

technically it is correct for Comp to fail when comparing apples and oranges.

But in this case i'd like attributes_changed?/1 to return true, because a change of attribute is present, although an invalid one.

the invalid format can then be catched later on and presents an "invalid value" message in the UI.

i solved it in my project by redefining Comparable.Comparable.Type.BitString.To.Decimal so that
comparing a decimal with a non-decimal always returns :gt

lib/my_app/my_decimal_comp.ex:

import Ash.Type.Comparable

defcomparable left :: Decimal, right :: BitString do
  case Decimal.cast(right) do
    :error -> :gt
    _ -> Decimal.compare(left, Ash.Type.Decimal.new(right))
  end
end


** Runtime **

  • Ash version
    2.4.20
@mavuio mavuio added the bug Something isn't working label Dec 30, 2022
@zachdaniel
Copy link
Contributor

Thanks for the detailed report! I think what we'll do is rescue any errors produced by comparing values, and assume they are unequal if one occurs :)

@zachdaniel
Copy link
Contributor

Fix pushed to main, it will be in the next release.

@mavuio
Copy link
Author

mavuio commented Dec 31, 2022

Thanks for the quick fix.

Have a great 2023 ! ( we all hope ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants