1+ package io.github.optimumcode.json.schema.objects.wrapper
2+
3+ import io.github.optimumcode.json.pointer.JsonPointer
4+ import io.github.optimumcode.json.schema.JsonSchema
5+ import io.github.optimumcode.json.schema.OutputCollector
6+ import io.github.optimumcode.json.schema.SchemaType
7+ import io.github.optimumcode.json.schema.ValidationOutput
8+ import io.kotest.assertions.asClue
9+ import io.kotest.core.spec.style.FunSpec
10+ import io.kotest.matchers.collections.shouldContainExactlyInAnyOrder
11+ import io.kotest.matchers.collections.shouldHaveSize
12+ import io.kotest.matchers.shouldBe
13+
14+ class ValidationTest : FunSpec () {
15+ init {
16+ val schema =
17+ JsonSchema .fromDefinition(
18+ """
19+ {
20+ "properties": {
21+ "simple": {
22+ "type": "integer"
23+ },
24+ "collection": {
25+ "type": "array",
26+ "items": {
27+ "type": "string"
28+ }
29+ },
30+ "sub-map": {
31+ "properties": {
32+ "inner": {
33+ "type": "string"
34+ }
35+ }
36+ }
37+ },
38+ "additionalProperties": false,
39+ "required": ["simple"]
40+ }
41+ """ .trimIndent(),
42+ defaultType = SchemaType .DRAFT_2020_12 ,
43+ )
44+
45+ test(" valid object" ) {
46+ val result =
47+ schema.validate(
48+ wrapAsElement(
49+ mapOf (
50+ " simple" to 1 ,
51+ " collection" to
52+ listOf (
53+ " test1" ,
54+ " test2" ,
55+ ),
56+ " sub-map" to
57+ mapOf (
58+ " inner" to " inner1" ,
59+ ),
60+ ),
61+ ),
62+ OutputCollector .basic(),
63+ )
64+
65+ result.asClue {
66+ it.valid shouldBe true
67+ }
68+ }
69+
70+ test(" invalid object" ) {
71+ val result =
72+ schema.validate(
73+ wrapAsElement(
74+ mapOf (
75+ " simple" to 1.5 ,
76+ " collection" to
77+ listOf (
78+ " test1" ,
79+ 1 ,
80+ ),
81+ " sub-map" to
82+ mapOf (
83+ " inner" to 42 ,
84+ ),
85+ ),
86+ ),
87+ OutputCollector .basic(),
88+ )
89+
90+ result.asClue {
91+ it.valid shouldBe false
92+ it.errors shouldHaveSize 3
93+ it.errors.shouldContainExactlyInAnyOrder(
94+ ValidationOutput .OutputUnit (
95+ valid = false ,
96+ keywordLocation = JsonPointer (" /properties/simple/type" ),
97+ instanceLocation = JsonPointer (" /simple" ),
98+ error = " element is not a integer" ,
99+ ),
100+ ValidationOutput .OutputUnit (
101+ valid = false ,
102+ keywordLocation = JsonPointer (" /properties/collection/items/type" ),
103+ instanceLocation = JsonPointer (" /collection/1" ),
104+ error = " element is not a string" ,
105+ ),
106+ ValidationOutput .OutputUnit (
107+ valid = false ,
108+ keywordLocation = JsonPointer (" /properties/sub-map/properties/inner/type" ),
109+ instanceLocation = JsonPointer (" /sub-map/inner" ),
110+ error = " element is not a string" ,
111+ ),
112+ )
113+ }
114+ }
115+ }
116+ }
0 commit comments