1
+ package com .relogiclabs .json .schema .positive ;
2
+
3
+ import com .relogiclabs .json .schema .JsonAssert ;
4
+ import org .junit .jupiter .api .Test ;
5
+
6
+ public class ComponentTests {
7
+ @ Test
8
+ public void When_ComponentObjectInObject_ValidTrue () {
9
+ var schema =
10
+ """
11
+ %define $cmp1: { "key1": #integer, "key2": #string }
12
+ %schema: {
13
+ "key1": $cmp1,
14
+ "key2": $cmp1,
15
+ "key3": $cmp1
16
+ }
17
+ """ ;
18
+ var json =
19
+ """
20
+ {
21
+ "key1": { "key1": 10, "key2": "value11" },
22
+ "key2": { "key1": 20, "key2": "value22" },
23
+ "key3": { "key1": 30, "key2": "value33" }
24
+ }
25
+ """ ;
26
+ JsonAssert .isValid (schema , json );
27
+ }
28
+
29
+ @ Test
30
+ public void When_ComponentObjectInArray_ValidTrue () {
31
+ var schema =
32
+ """
33
+ %define $cmp1: { "key1": #integer, "key2": #float }
34
+ %schema: [$cmp1, $cmp1, $cmp1]
35
+ """ ;
36
+ var json =
37
+ """
38
+ [
39
+ { "key1": 10, "key2": 2.5 },
40
+ { "key1": 20, "key2": 3.5 },
41
+ { "key1": 30, "key2": 4.5 }
42
+ ]
43
+ """ ;
44
+ JsonAssert .isValid (schema , json );
45
+ }
46
+
47
+ @ Test
48
+ public void When_ComponentArrayInArray_ValidTrue () {
49
+ var schema =
50
+ """
51
+ %define $cmp1: [#number, #number]
52
+ %schema: [$cmp1, $cmp1, $cmp1]
53
+ """ ;
54
+ var json =
55
+ """
56
+ [
57
+ [10, 2.5],
58
+ [20, 3.5],
59
+ [30, 4.5]
60
+ ]
61
+ """ ;
62
+ JsonAssert .isValid (schema , json );
63
+ }
64
+
65
+ @ Test
66
+ public void When_ComponentArrayInObject_ValidTrue () {
67
+ var schema =
68
+ """
69
+ %define $cmp1: [#number, #number]
70
+ %schema: {
71
+ "key1": $cmp1,
72
+ "key2": $cmp1,
73
+ "key3": $cmp1
74
+ }
75
+ """ ;
76
+ var json =
77
+ """
78
+ {
79
+ "key1": [10, 2.5],
80
+ "key2": [20, 3.5],
81
+ "key3": [30, 4.5]
82
+ }
83
+ """ ;
84
+ JsonAssert .isValid (schema , json );
85
+ }
86
+
87
+ @ Test
88
+ public void When_RangeWithComponentObjectInArray_ValidTrue () {
89
+ var schema =
90
+ """
91
+ %define $cmp1: { "key1": @range(10, 30) #integer, "key2": @range(2.5, 4.5) #float }
92
+ %schema: [$cmp1, $cmp1, $cmp1]
93
+ """ ;
94
+ var json =
95
+ """
96
+ [
97
+ { "key1": 10, "key2": 2.5 },
98
+ { "key1": 20, "key2": 3.5 },
99
+ { "key1": 30, "key2": 4.5 }
100
+ ]
101
+ """ ;
102
+ JsonAssert .isValid (schema , json );
103
+ }
104
+
105
+ @ Test
106
+ public void When_NestedComponentObjectWithObjectInArray_ValidTrue () {
107
+ var schema =
108
+ """
109
+ %define $cmp1:
110
+ {
111
+ "key1": @range(10, 30) #integer,
112
+ "key2": @enum("val1", "val2", "val3") #string
113
+ }
114
+ %schema: #object*($cmp1) #array
115
+ """ ;
116
+ var json =
117
+ """
118
+ [
119
+ { "key1": 10, "key2": "val1" },
120
+ { "key1": 20, "key2": "val2" },
121
+ { "key1": 30, "key2": "val3" }
122
+ ]
123
+ """ ;
124
+ JsonAssert .isValid (schema , json );
125
+ }
126
+
127
+ @ Test
128
+ public void When_ComponentInComponentWithArrayInObject_ValidTrue () {
129
+ var schema =
130
+ """
131
+ %define $cmp1: {
132
+ "key": @range(10, 100) #number
133
+ }
134
+ %define $cmp2: [ $cmp1, $cmp1 ]
135
+ %schema: {
136
+ "key1": $cmp2,
137
+ "key2": $cmp2,
138
+ "key3": $cmp2
139
+ }
140
+ """ ;
141
+ var json =
142
+ """
143
+ {
144
+ "key1": [{"key": 10}, {"key": 11}],
145
+ "key2": [{"key": 20}, {"key": 21}],
146
+ "key3": [{"key": 30}, {"key": 31}]
147
+ }
148
+ """ ;
149
+ JsonAssert .isValid (schema , json );
150
+ }
151
+
152
+ @ Test
153
+ public void When_CompositeAndPrimitiveDataTypeWithPrimitiveInput_ValidTrue () {
154
+ var schema = "@range*(5, 10) #array #string" ;
155
+ var json =
156
+ """
157
+ "value1"
158
+ """ ;
159
+ JsonAssert .isValid (schema , json );
160
+ }
161
+
162
+ @ Test
163
+ public void When_CompositeAndPrimitiveDataTypeWithCompositeInput_ValidTrue () {
164
+ var schema = "@range*(5, 10) #array #string" ;
165
+ var json =
166
+ """
167
+ [5, 6, 7]
168
+ """ ;
169
+ JsonAssert .isValid (schema , json );
170
+ }
171
+ }
0 commit comments