React-jsonschema-form: Allow decimals to end in "zero"

Created on 26 Dec 2016  路  8Comments  路  Source: rjsf-team/react-jsonschema-form

Prerequisites

  • [x] I have read the documentation;
  • [x] In the case of a bug report, I understand that providing a SSCCE example is tremendously useful to the maintainers.

Steps to Reproduce

  1. Navigate to "Numbers" tab in https://mozilla-services.github.io/react-jsonschema-form/
  2. Change 3.14 to 3.10
  3. Change to 3.0 or any other decimal ending in zero
  4. Note that it is not possible

Expected behavior

Don't throw type error for 3.10

Actual behavior

Type error thrown for 3.10

bug

All 8 comments

+1

This is due to https://github.com/mozilla-services/react-jsonschema-form/blob/master/src/utils.js#L210-L215. It seems to me that there are two use cases here -- one for numeric values for which a trailing zero is semantically meaningless (and so we should allow it), and one for fixed-point numbers for which a trailing zero is meaningful, like for dollars-and-cents amounts. NumberField corresponds to the JSON type "number", which means that it matches the first use case. I'd like to reserve the ability to use fields for fixed-width decimal formats, but I don't see how we could deserialize its data as anything except string, and if you use a StringField, then I think that already covers this case. Maybe you'd want to add a custom validator to prevent things like 3.0.1 or 3.012. So I'd be OK with simplifying asNumber and adding a "decimal field" example to the documentation/playground.

The problem is trailing zeros are not preserved when serializing to JSON:

> JSON.parse("3.10")
3.1
> JSON.parse(JSON.stringify(3.10))
3.1
> 3.10
3.1

This library relies on JSON schema, hence on the JSON spec wrt value validation. My take is if you want trailing zeros in your numbers, you may actually want strings instead. I know this is not ideal, but I don't have any other idea how to preserve trailing zeros in numbers when the language & spec themselves don't allow it.

Also another solution if you always want two digits after the comma would be to make to multiply the value by 100 (round it to its integer value) and then devide it by 100 and it will put it back.

I've hit this issue recently as well. I don't need the trailing 0 to be preserved but it would be good if what is clearly a numeric value doesn't fail validation with the error "should be number"

Has anyone had any updates on this, in our use case we need to maintain the trailing 0s. But even if that wasnt supported I would expect it to strip them and move on.

But like mentioned above

... it would be good if what is clearly a numeric value doesn't fail validation with the error "should be number"

that seems like a bug.

@travisdahl I believe this was solved in #1183 and is included in the latest release.

@epicfaace my bad, we upgraded and saw the fix. It still drops the zeros but besides that it seems to be working

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pablen picture pablen  路  3Comments

n1k0 picture n1k0  路  3Comments

MedinaGitHub picture MedinaGitHub  路  3Comments

ClockerZadq picture ClockerZadq  路  3Comments

epicfaace picture epicfaace  路  3Comments