My use-case is meta -- I am trying to write Swift that can properly produce schemas adhering to an existing specification (draft-wright-json-schema-validation-00) that allows for an exclusive minimum value to be defined. Note that I am writing a type that serializes to a schema following the rules of the JSON Schema specification, not serializing values that need to be valid according to some existing schema (... ignoring the subtlety that the schemas my type serializes can indeed be validated against the JSON Schema meta-schema).
I could come up with a hypothetical reason for needing to say "provide my JSON API with a number strictly greater than x" but I don't have one readily available. I just know that I need to be able to represent that reality because it is stated as part of the specification I am following.
Assuming that we are not debating whether someone could possibly want to write a schema that said "provide me a floating-point value greater than 2.0," I lose information if I try to use Swift RangeExpression
types.
Assume I am trying to decode the following schema (and I am going to ignore maximums to focus on the problem being discussed):
{ "type": "number", "minimum": 2.0, "exclusiveMinimum": true }
So I create a type like
struct NumberSchema {
let range: PartialRangeFrom<Double>
}
and I write my own encoding and decoding functions. The best I can do when decoding the example schema above is to make the range
equivalent to 2.0.nextUp...
. This will work if I then go to validate some data that is supposed to fit the schema, but I run into problems on encoding because I have thrown away some information. I will produce something like
{ "type": "number", "minimum": 2.0000000000000004, "exclusiveMinimum": false }
This is not accurate, so using an existing RangeExpression
type was a non-starter even though in every respect except for representing exclusive-lower-bound there exists a RangeExpression
type that is a perfect fit.
No big deal, because I can just make my Swift type more closely resemble the schema. However, now I want to provide the convenience of writing the schema in Swift in the first place using this type (for all the reasons I like writing anything in Swift).
The initializer for this NumberSchema
type is worlds more concise if it can take advantage of the expressiveness of built-in RangeExpression
operators (as I was describing in my previous comment in this thread).