Skip to content

Commit a33f5b4

Browse files
committed
feat: implement number range constraints
1 parent 5c2f38d commit a33f5b4

File tree

15 files changed

+1017
-43
lines changed

15 files changed

+1017
-43
lines changed

docs/src/examples/json-schema-change-compatibility-checks.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ Examples:
4141
- [the new value of multipleOf is divisible by the old one](#the-new-value-of-multipleof-is-divisible-by-the-old-one)
4242
- [the old value of multipleOf is divisible by the new one](#the-old-value-of-multipleof-is-divisible-by-the-new-one)
4343
- [old and new value of multipleOf are not each other's factors](#old-and-new-value-of-multipleof-are-not-each-other's-factors)
44+
- [the range of allowed number values gets extended](#the-range-of-allowed-number-values-gets-extended)
45+
- [the range of allowed number values gets reduced](#the-range-of-allowed-number-values-gets-reduced)
46+
- [the range of allowed number values gets extended and reduced at the same time](#the-range-of-allowed-number-values-gets-extended-and-reduced-at-the-same-time)
4447
---
4548
### No JSON schema differences
4649
When there is not JSON schema differences, schema change is fully compatible.
@@ -267,3 +270,57 @@ In this situation, there are potentially some numbers that are not divisible by
267270
```
268271
none
269272
```
273+
---
274+
### the range of allowed number values gets extended
275+
In this situation, all numbers from the new, longer range fall into the old, shorter range. Therefore, such a change is backward compatible.
276+
277+
#### Input
278+
##### JSON schema differences
279+
```
280+
-
281+
Schema path: #
282+
maximum changed from 15.0to20.0
283+
-
284+
Schema path: #
285+
minimum changed from 10.0to5.0
286+
```
287+
#### Output
288+
```
289+
backward
290+
```
291+
---
292+
### the range of allowed number values gets reduced
293+
In this situation, all numbers from the new, shorted range fall into the old, longer range. Therefore, such a change is forward compatible.
294+
295+
#### Input
296+
##### JSON schema differences
297+
```
298+
-
299+
Schema path: #
300+
maximum changed from 20.0to15.0
301+
-
302+
Schema path: #
303+
minimum changed from 5.0to10.0
304+
```
305+
#### Output
306+
```
307+
forward
308+
```
309+
---
310+
### the range of allowed number values gets extended and reduced at the same time
311+
In this situation, there are some numbers which do not fall into neither old nor new range. Therefore, such a change is incompatible.
312+
313+
#### Input
314+
##### JSON schema differences
315+
```
316+
-
317+
Schema path: #
318+
maximum changed from 15.0to20.0
319+
-
320+
Schema path: #
321+
minimum changed from 5.0to10.0
322+
```
323+
#### Output
324+
```
325+
none
326+
```

docs/src/examples/json-schema-difference-calculation.md

Lines changed: 84 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ Examples:
88
- [Comparing identical schemata](#comparing-identical-schemata)
99
- [Changing expected JSON value type from null to boolean](#changing-expected-json-value-type-from-null-to-boolean)
1010
- [Changing multipleOf value](#changing-multipleof-value)
11+
- [Changing exclusiveMaximum value](#changing-exclusivemaximum-value)
12+
- [Changing exclusiveMinimum value](#changing-exclusiveminimum-value)
13+
- [Changing maximum value](#changing-maximum-value)
14+
- [Changing minimum value](#changing-minimum-value)
1115
---
1216
### Comparing identical schemata
1317
When two identical schemata are compared, no difference should be found.
@@ -41,7 +45,7 @@ Any change in expected JSON value type should be accounted as a difference.
4145
#### Output
4246
```
4347
-
44-
Schema path: #
48+
Schema path: #/type
4549
Change of accepted JSON value types from
4650
- null
4751
to
@@ -54,15 +58,91 @@ TODO
5458
#### Input
5559
##### Previous JSON schema
5660
```json
57-
{"multipleOf":2,"type":["number"]}
61+
{"multipleOf":2}
5862
```
5963
##### Next JSON schema
6064
```json
61-
{"multipleOf":4,"type":["number"]}
65+
{"multipleOf":4}
6266
```
6367
#### Output
6468
```
6569
-
66-
Schema path: #
70+
Schema path: #/multipleOf
6771
multipleOf changed from 2.0 to 4.0
6872
```
73+
---
74+
### Changing exclusiveMaximum value
75+
TODO
76+
77+
#### Input
78+
##### Previous JSON schema
79+
```json
80+
{"exclusiveMaximum":2}
81+
```
82+
##### Next JSON schema
83+
```json
84+
{"exclusiveMaximum":4}
85+
```
86+
#### Output
87+
```
88+
-
89+
Schema path: #/exclusiveMaximum
90+
exclusiveMaximum changed from 2.0to4.0
91+
```
92+
---
93+
### Changing exclusiveMinimum value
94+
TODO
95+
96+
#### Input
97+
##### Previous JSON schema
98+
```json
99+
{"exclusiveMinimum":2}
100+
```
101+
##### Next JSON schema
102+
```json
103+
{"exclusiveMinimum":4}
104+
```
105+
#### Output
106+
```
107+
-
108+
Schema path: #/exclusiveMinimum
109+
exclusiveMinimum changed from 2.0to4.0
110+
```
111+
---
112+
### Changing maximum value
113+
TODO
114+
115+
#### Input
116+
##### Previous JSON schema
117+
```json
118+
{"maximum":2}
119+
```
120+
##### Next JSON schema
121+
```json
122+
{"maximum":4}
123+
```
124+
#### Output
125+
```
126+
-
127+
Schema path: #/maximum
128+
maximum changed from 2.0to4.0
129+
```
130+
---
131+
### Changing minimum value
132+
TODO
133+
134+
#### Input
135+
##### Previous JSON schema
136+
```json
137+
{"minimum":2}
138+
```
139+
##### Next JSON schema
140+
```json
141+
{"minimum":4}
142+
```
143+
#### Output
144+
```
145+
-
146+
Schema path: #/minimum
147+
minimum changed from 2.0to4.0
148+
```

docs/src/examples/json-values-validation.md

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ Examples:
1414
- [An array with forbidden duplicate value.](#an-array-with-forbidden-duplicate-value.)
1515
- [Number 7.5 against a schema accepting only multiples of 2.5](#number-7.5-against-a-schema-accepting-only-multiples-of-2.5)
1616
- [Number 7 against a schema accepting only multiples of 2.5](#number-7-against-a-schema-accepting-only-multiples-of-2.5)
17+
- [A JSON number value is matches exactly the maximum keyword value](#a-json-number-value-is-matches-exactly-the-maximum-keyword-value)
18+
- [A JSON number value is greater than the maximum keyword value](#a-json-number-value-is-greater-than-the-maximum-keyword-value)
19+
- [A JSON number value is less than the exclusiveMaximum keyword value](#a-json-number-value-is-less-than-the-exclusivemaximum-keyword-value)
20+
- [A JSON number value is matches exactly the exclusiveMaximum keyword value](#a-json-number-value-is-matches-exactly-the-exclusivemaximum-keyword-value)
21+
- [A JSON number value is matches exactly the minimum keyword value](#a-json-number-value-is-matches-exactly-the-minimum-keyword-value)
22+
- [A JSON number value is less than the minimum keyword value](#a-json-number-value-is-less-than-the-minimum-keyword-value)
23+
- [A JSON number value is greater than the exclusiveMinimum keyword value](#a-json-number-value-is-greater-than-the-exclusiveminimum-keyword-value)
24+
- [A JSON number value is matches exactly the exclusiveMinimum keyword value](#a-json-number-value-is-matches-exactly-the-exclusiveminimum-keyword-value)
1725
---
1826
### A null value against a schema accepting only null values
1927
A null value conforms to the schema.
@@ -209,3 +217,151 @@ Number 7 does not conform the schema as 7.5 is not a multiple of 2.5.
209217
JSON path: $
210218
7.0 is not a multiple of 2.5
211219
```
220+
---
221+
### A JSON number value is matches exactly the maximum keyword value
222+
Because maximum constraint is inclusive, such a value is valid.
223+
224+
#### Input
225+
##### JSON schema
226+
```json
227+
{"maximum":4}
228+
```
229+
##### JSON
230+
```json
231+
4
232+
```
233+
#### Output
234+
```
235+
✓ no violations
236+
```
237+
---
238+
### A JSON number value is greater than the maximum keyword value
239+
Because the value is out of the valid range, it is invalid.
240+
241+
#### Input
242+
##### JSON schema
243+
```json
244+
{"maximum":4}
245+
```
246+
##### JSON
247+
```json
248+
5
249+
```
250+
#### Output
251+
```
252+
253+
Schema path: #/maximum
254+
JSON path: $
255+
5.0 is outside of the valid range of (-Infinity,4.0]
256+
```
257+
---
258+
### A JSON number value is less than the exclusiveMaximum keyword value
259+
Because the value is within the valid range, it is valid.
260+
261+
#### Input
262+
##### JSON schema
263+
```json
264+
{"exclusiveMaximum":4}
265+
```
266+
##### JSON
267+
```json
268+
3
269+
```
270+
#### Output
271+
```
272+
✓ no violations
273+
```
274+
---
275+
### A JSON number value is matches exactly the exclusiveMaximum keyword value
276+
Because the exclusiveMaximum constraint is exclusive, such a value is invalid.
277+
278+
#### Input
279+
##### JSON schema
280+
```json
281+
{"exclusiveMaximum":4}
282+
```
283+
##### JSON
284+
```json
285+
4
286+
```
287+
#### Output
288+
```
289+
290+
Schema path: #/exclusiveMaximum
291+
JSON path: $
292+
4.0 is outside of the valid range of (-Infinity,4.0)
293+
```
294+
---
295+
### A JSON number value is matches exactly the minimum keyword value
296+
Because the minimum constraint is inclusive, such a value is invalid.
297+
298+
#### Input
299+
##### JSON schema
300+
```json
301+
{"minimum":4}
302+
```
303+
##### JSON
304+
```json
305+
4
306+
```
307+
#### Output
308+
```
309+
✓ no violations
310+
```
311+
---
312+
### A JSON number value is less than the minimum keyword value
313+
Because the value is out of the valid range, it is invalid.
314+
315+
#### Input
316+
##### JSON schema
317+
```json
318+
{"minimum":4}
319+
```
320+
##### JSON
321+
```json
322+
3
323+
```
324+
#### Output
325+
```
326+
327+
Schema path: #/minimum
328+
JSON path: $
329+
3.0 is outside of the valid range of [4.0,Infinity)
330+
```
331+
---
332+
### A JSON number value is greater than the exclusiveMinimum keyword value
333+
Because the value is within the valid range, it is valid.
334+
335+
#### Input
336+
##### JSON schema
337+
```json
338+
{"exclusiveMinimum":4}
339+
```
340+
##### JSON
341+
```json
342+
5
343+
```
344+
#### Output
345+
```
346+
✓ no violations
347+
```
348+
---
349+
### A JSON number value is matches exactly the exclusiveMinimum keyword value
350+
Because the exclusiveMinimum constraint is exclusive, such a value is not valid.
351+
352+
#### Input
353+
##### JSON schema
354+
```json
355+
{"exclusiveMinimum":4}
356+
```
357+
##### JSON
358+
```json
359+
4
360+
```
361+
#### Output
362+
```
363+
364+
Schema path: #/exclusiveMinimum
365+
JSON path: $
366+
4.0 is outside of the valid range of (4.0,Infinity)
367+
```

src/purs/JsonSchema.purs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ instance Show JsonSchema where
2626
show schema = genericShow schema
2727

2828
type Keywords =
29-
{ items Maybe JsonSchema
29+
{ exclusiveMaximum Maybe Number
30+
, exclusiveMinimum Maybe Number
31+
, items Maybe JsonSchema
32+
, maximum Maybe Number
33+
, minimum Maybe Number
3034
, multipleOf Maybe Number
3135
, not Maybe JsonSchema
3236
, required Set String
@@ -36,7 +40,11 @@ type Keywords =
3640

3741
defaultKeywords Keywords
3842
defaultKeywords =
39-
{ items: Nothing
43+
{ exclusiveMaximum: Nothing
44+
, exclusiveMinimum: Nothing
45+
, items: Nothing
46+
, maximum: Nothing
47+
, minimum: Nothing
4048
, multipleOf: Nothing
4149
, not: Nothing
4250
, required: Set.empty

0 commit comments

Comments
 (0)