Consider adding a way to configure how strict number-related constraints are with respect to the number type being validated #65
Labels
enhancement
New feature or enhancement for the Ion Schema _specification_
A
valid_values
number range will accept any type of number. (E.g. forvalid_values: range::[0, 2]
any of1
,1.0
, and1e0
are valid.) However, thescale
andprecision
constraints only acceptdecimal
values, and the proposedieee754_float
constraint only acceptsfloat
values.An ISL user may wish to restrict the range of numerical values using these constraints without actually limiting the value to a specific type. (This is particularly an issue when up-converting from JSON to Ion—some values that are equivalent in JSON are interpreted as different types in Ion.)
To solve this sort of use case, it may be useful to have a way to configure how numbers are validated. Here are a strawman proposal to explain what I mean.
Idea 1
In the header, we can have a field to configure which "number mode" is to be used. This "number mode" would be used for the entire schema document, and they configure how the
Examples:
number_mode: strict
number_mode: default
number_mode: relaxed
valid_values: range::[1, 2]
1
valid_values: range::[1, 2]
1.0
valid_values: range::[1, 2]
1e0
valid_values: range::[1, 2.0]
1
ieee754_float: binary16
1
ieee754_float: binary16
1.0
ieee754_float: binary16
1e0
Idea 2
We could add a modifier to all of the numeric constraints that specifies that the type of number shouldn't be taken into account—only the mathematical value of the number. For example,
ieee754_float: coerce_to_float::binary16
would mean that the value should be converted to a numerically equivalent float before checking if the value satisfies the constraint.Number ranges could be be modified using a
strict::
modifier so that they must have endpoints that are the same type and they only accept values of that type.Open Questions
What happens when a number value doesn't have an exact representation or has more than one representation in some other number type? Intuitively, we might expect that
1
could be coerced to1d0
or1e0
, but it's not as clear cut for100
. Would100
be coerced as1d2
or1.00d2
, or would the implementation try to determine if any possible representation was valid (e.g. choose a precision that is known to be valid from all possible "faithful" representations of the original value)?The text was updated successfully, but these errors were encountered: