Skip to content

Commit 8fdc152

Browse files
committed
feat: add array items validation
1 parent 75f7ed2 commit 8fdc152

File tree

5 files changed

+289
-113
lines changed

5 files changed

+289
-113
lines changed

Examples.generated.md

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ null
1818
#### Output
1919
```
2020
✓ no violations
21+
2122
```
2223

2324
### ► A boolean value against a schema accepting only null values
@@ -33,9 +34,12 @@ true
3334
```
3435
#### Output
3536
```
36-
✗ Invalid type. Expected null but got boolean.
37-
Schema path: #/type
38-
JSON path: $
37+
38+
Schema path:
39+
#/type
40+
JSON path:
41+
$
42+
Invalid type. Expected null but got boolean.
3943
```
4044

4145
### ► A boolean value against a schema accepting only null and string values
@@ -51,8 +55,44 @@ true
5155
```
5256
#### Output
5357
```
54-
✗ Invalid type. Expected null or string but got boolean.
55-
Schema path: #/type
56-
JSON path: $
58+
59+
Schema path:
60+
#/type
61+
JSON path:
62+
$
63+
Invalid type. Expected null or string but got boolean.
64+
```
65+
66+
### ► An array with 2 out of 5 items not matching the desired item type
67+
When schema requires items to conform to a certain schema, every single value in the array has to.
68+
#### Input
69+
##### JSON schema
70+
```json
71+
{"items":{"type":["null"]},"type":["array"]}
72+
```
73+
##### JSON
74+
```json
75+
[null,false,null,true,null]
76+
```
77+
#### Output
78+
```
79+
80+
Schema path:
81+
#
82+
JSON path:
83+
$
84+
Invalid array:
85+
-
86+
Schema path:
87+
#/items/type
88+
JSON path:
89+
$[1]
90+
Invalid type. Expected null but got boolean.
91+
-
92+
Schema path:
93+
#/items/type
94+
JSON path:
95+
$[3]
96+
Invalid type. Expected null but got boolean.
5797
```
5898

src/purs/JsonSchema/JsonPath.purs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module JsonSchema.JsonPath
2+
( JsonPath
3+
, JsonPathSegment(..)
4+
, render
5+
) where
6+
7+
import Prelude
8+
9+
import Data.Foldable (foldMap)
10+
import Data.Generic.Rep (class Generic)
11+
import Data.List (List)
12+
import Data.List as List
13+
import Data.Show.Generic (genericShow)
14+
15+
type JsonPath = List JsonPathSegment
16+
17+
render JsonPath String
18+
render = ("$" <> _) <<< foldMap f <<< List.reverse
19+
where
20+
f JsonPathSegment String
21+
f = case _ of
22+
ItemIndex idx →
23+
"[" <> show idx <> "]"
24+
Property name →
25+
"/" <> name
26+
27+
data JsonPathSegment
28+
= ItemIndex Int
29+
| Property String
30+
31+
derive instance Eq JsonPathSegment
32+
derive instance Generic JsonPathSegment _
33+
derive instance Ord JsonPathSegment
34+
35+
instance Show JsonPathSegment where
36+
show = genericShow
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module JsonSchema.SchemaPath
2+
( SchemaPath
3+
, SchemaPathSegment(..)
4+
, render
5+
) where
6+
7+
import Prelude
8+
9+
import Data.Foldable (foldMap)
10+
import Data.Generic.Rep (class Generic)
11+
import Data.List (List)
12+
import Data.List as List
13+
import Data.Show.Generic (genericShow)
14+
15+
type SchemaPath = List SchemaPathSegment
16+
17+
render SchemaPath String
18+
render = ("#" <> _) <<< foldMap f <<< List.reverse
19+
where
20+
f SchemaPathSegment String
21+
f = case _ of
22+
Items
23+
"/items"
24+
TypeKeyword
25+
"/type"
26+
27+
data SchemaPathSegment = Items | TypeKeyword
28+
29+
derive instance Eq SchemaPathSegment
30+
derive instance Generic SchemaPathSegment _
31+
derive instance Ord SchemaPathSegment
32+
33+
instance Show SchemaPathSegment where
34+
show = genericShow

0 commit comments

Comments
 (0)