Skip to content

Commit f04a968

Browse files
Merge pull request #6 from relogiclabs/develop
Update Data Type Validation
2 parents cecb26a + ad6a762 commit f04a968

33 files changed

+573
-325
lines changed

JsonSchema.Tests/RelogicLabs/JsonSchema/Tests/Negative/ArrayTests.cs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ public void When_JsonNotArray_ExceptionThrown()
1111
{
1212
var schema = "#array";
1313
var json = "10";
14-
14+
1515
JsonSchema.IsValid(schema, json);
1616
var exception = Assert.ThrowsException<JsonSchemaException>(
1717
() => JsonAssert.IsValid(schema, json));
1818
Assert.AreEqual(DTYP04, exception.Code);
1919
Console.WriteLine(exception);
2020
}
21-
21+
2222
[TestMethod]
2323
public void When_JsonNotArrayInObject_ExceptionThrown()
2424
{
@@ -38,14 +38,14 @@ public void When_JsonNotArrayInObject_ExceptionThrown()
3838
"key3": 100000
3939
}
4040
""";
41-
41+
4242
JsonSchema.IsValid(schema, json);
4343
var exception = Assert.ThrowsException<JsonSchemaException>(
4444
() => JsonAssert.IsValid(schema, json));
4545
Assert.AreEqual(DTYP04, exception.Code);
4646
Console.WriteLine(exception);
4747
}
48-
48+
4949
[TestMethod]
5050
public void When_JsonNotArrayInArray_ExceptionThrown()
5151
{
@@ -57,14 +57,14 @@ public void When_JsonNotArrayInArray_ExceptionThrown()
5757
"""
5858
[{}, "value1", 10.5]
5959
""";
60-
60+
6161
JsonSchema.IsValid(schema, json);
6262
var exception = Assert.ThrowsException<JsonSchemaException>(
6363
() => JsonAssert.IsValid(schema, json));
6464
Assert.AreEqual(DTYP04, exception.Code);
6565
Console.WriteLine(exception);
6666
}
67-
67+
6868
[TestMethod]
6969
public void When_NestedJsonNotArrayInArray_ExceptionThrown()
7070
{
@@ -76,14 +76,14 @@ public void When_NestedJsonNotArrayInArray_ExceptionThrown()
7676
"""
7777
[true, "value1", false]
7878
""";
79-
79+
8080
JsonSchema.IsValid(schema, json);
8181
var exception = Assert.ThrowsException<JsonSchemaException>(
8282
() => JsonAssert.IsValid(schema, json));
8383
Assert.AreEqual(DTYP06, exception.Code);
8484
Console.WriteLine(exception);
8585
}
86-
86+
8787
[TestMethod]
8888
public void When_NestedJsonNotArrayInObject_ExceptionThrown()
8989
{
@@ -99,44 +99,44 @@ public void When_NestedJsonNotArrayInObject_ExceptionThrown()
9999
"key3": "value1"
100100
}
101101
""";
102-
102+
103103
JsonSchema.IsValid(schema, json);
104104
var exception = Assert.ThrowsException<JsonSchemaException>(
105105
() => JsonAssert.IsValid(schema, json));
106106
Assert.AreEqual(DTYP06, exception.Code);
107107
Console.WriteLine(exception);
108108
}
109-
109+
110110
[TestMethod]
111111
public void When_ElementsWithWrongArray_ExceptionThrown()
112112
{
113113
var schema = "@elements(10, 20, 30, 40) #array";
114114
var json = "[5, 10, 15, 20, 25]";
115-
115+
116116
JsonSchema.IsValid(schema, json);
117117
var exception = Assert.ThrowsException<JsonSchemaException>(
118118
() => JsonAssert.IsValid(schema, json));
119119
Assert.AreEqual(ELEM01, exception.Code);
120120
Console.WriteLine(exception);
121121
}
122-
122+
123123
[TestMethod]
124124
public void When_NestedElementsWithWrongArrayInArray_ExceptionThrown()
125125
{
126126
var schema = "@elements*(5, 10) #array";
127127
var json = "[[5, 10], [], [5, 10, 15, 20]]";
128-
128+
129129
JsonSchema.IsValid(schema, json);
130130
var exception = Assert.ThrowsException<JsonSchemaException>(
131131
() => JsonAssert.IsValid(schema, json));
132132
Assert.AreEqual(ELEM01, exception.Code);
133133
Console.WriteLine(exception);
134134
}
135-
135+
136136
[TestMethod]
137137
public void When_EnumWithWrongValueInArray_ExceptionThrown()
138138
{
139-
var schema =
139+
var schema =
140140
"""
141141
[
142142
@enum(5, 10, 15),
@@ -145,27 +145,27 @@ public void When_EnumWithWrongValueInArray_ExceptionThrown()
145145
] #array
146146
""";
147147
var json = """[11, 102, "efg"]""";
148-
148+
149149
JsonSchema.IsValid(schema, json);
150150
var exception = Assert.ThrowsException<JsonSchemaException>(
151151
() => JsonAssert.IsValid(schema, json));
152152
Assert.AreEqual(ENUM02, exception.Code);
153153
Console.WriteLine(exception);
154154
}
155-
155+
156156
[TestMethod]
157157
public void When_InvalidJsonInArray_ExceptionThrown()
158158
{
159159
var schema = "#array";
160160
var json = "[,,]";
161-
161+
162162
//JsonSchema.IsValid(schema, json);
163163
var exception = Assert.ThrowsException<JsonParserException>(
164164
() => JsonAssert.IsValid(schema, json));
165165
Assert.AreEqual(JPRS01, exception.Code);
166166
Console.WriteLine(exception);
167167
}
168-
168+
169169
[TestMethod]
170170
public void When_EmptyArrayInObject_ExceptionThrown()
171171
{

JsonSchema.Tests/RelogicLabs/JsonSchema/Tests/Negative/BooleanTests.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,27 @@ public void When_JsonNotBoolean_ExceptionThrown()
1111
{
1212
var schema = "#boolean";
1313
var json = "5";
14-
14+
1515
JsonSchema.IsValid(schema, json);
1616
var exception = Assert.ThrowsException<JsonSchemaException>(
1717
() => JsonAssert.IsValid(schema, json));
1818
Assert.AreEqual(DTYP04, exception.Code);
1919
Console.WriteLine(exception);
2020
}
21-
21+
2222
[TestMethod]
2323
public void When_JsonValueNotEqualForBoolean_ExceptionThrown()
2424
{
2525
var schema = "true #boolean";
2626
var json = "false";
27-
27+
2828
JsonSchema.IsValid(schema, json);
2929
var exception = Assert.ThrowsException<JsonSchemaException>(
3030
() => JsonAssert.IsValid(schema, json));
3131
Assert.AreEqual(BOOL01, exception.Code);
3232
Console.WriteLine(exception);
3333
}
34-
34+
3535
[TestMethod]
3636
public void When_JsonNotBooleanInObject_ExceptionThrown()
3737
{
@@ -51,14 +51,14 @@ public void When_JsonNotBooleanInObject_ExceptionThrown()
5151
"key3": true
5252
}
5353
""";
54-
54+
5555
JsonSchema.IsValid(schema, json);
5656
var exception = Assert.ThrowsException<JsonSchemaException>(
5757
() => JsonAssert.IsValid(schema, json));
5858
Assert.AreEqual(DTYP04, exception.Code);
5959
Console.WriteLine(exception);
6060
}
61-
61+
6262
[TestMethod]
6363
public void When_JsonNotBooleanInArray_ExceptionThrown()
6464
{
@@ -70,14 +70,14 @@ public void When_JsonNotBooleanInArray_ExceptionThrown()
7070
"""
7171
[[], 11.5, "false"]
7272
""";
73-
73+
7474
JsonSchema.IsValid(schema, json);
7575
var exception = Assert.ThrowsException<JsonSchemaException>(
7676
() => JsonAssert.IsValid(schema, json));
7777
Assert.AreEqual(DTYP04, exception.Code);
7878
Console.WriteLine(exception);
7979
}
80-
80+
8181
[TestMethod]
8282
public void When_NestedJsonNotBooleanInArray_ExceptionThrown()
8383
{
@@ -89,14 +89,14 @@ public void When_NestedJsonNotBooleanInArray_ExceptionThrown()
8989
"""
9090
["true", {}, [true]]
9191
""";
92-
92+
9393
JsonSchema.IsValid(schema, json);
9494
var exception = Assert.ThrowsException<JsonSchemaException>(
9595
() => JsonAssert.IsValid(schema, json));
9696
Assert.AreEqual(DTYP06, exception.Code);
9797
Console.WriteLine(exception);
9898
}
99-
99+
100100
[TestMethod]
101101
public void When_NestedJsonNotBooleanInObject_ExceptionThrown()
102102
{
@@ -112,7 +112,7 @@ public void When_NestedJsonNotBooleanInObject_ExceptionThrown()
112112
"key3": false
113113
}
114114
""";
115-
115+
116116
JsonSchema.IsValid(schema, json);
117117
var exception = Assert.ThrowsException<JsonSchemaException>(
118118
() => JsonAssert.IsValid(schema, json));
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
using RelogicLabs.JsonSchema.Exceptions;
2+
using static RelogicLabs.JsonSchema.Message.ErrorCode;
3+
4+
namespace RelogicLabs.JsonSchema.Tests.Negative;
5+
6+
[TestClass]
7+
public class DataTypeTests
8+
{
9+
[TestMethod]
10+
public void When_JsonWithWrongMainDataType_ExceptionThrown()
11+
{
12+
var schema =
13+
"""
14+
#string* #array
15+
""";
16+
var json =
17+
"""
18+
10
19+
""";
20+
21+
JsonSchema.IsValid(schema, json);
22+
var exception = Assert.ThrowsException<JsonSchemaException>(
23+
() => JsonAssert.IsValid(schema, json));
24+
Assert.AreEqual(DTYP04, exception.Code);
25+
Console.WriteLine(exception);
26+
}
27+
28+
[TestMethod]
29+
public void When_JsonWithWrongNestedDataType_ExceptionThrown()
30+
{
31+
var schema =
32+
"""
33+
#string* #array
34+
""";
35+
var json =
36+
"""
37+
[10, 20]
38+
""";
39+
40+
JsonSchema.IsValid(schema, json);
41+
var exception = Assert.ThrowsException<JsonSchemaException>(
42+
() => JsonAssert.IsValid(schema, json));
43+
Assert.AreEqual(DTYP06, exception.Code);
44+
Console.WriteLine(exception);
45+
}
46+
47+
[TestMethod]
48+
public void When_NestedTypeWithNonCompositeJson_ExceptionThrown()
49+
{
50+
var schema =
51+
"""
52+
#string*
53+
""";
54+
var json =
55+
"""
56+
10
57+
""";
58+
59+
JsonSchema.IsValid(schema, json);
60+
var exception = Assert.ThrowsException<JsonSchemaException>(
61+
() => JsonAssert.IsValid(schema, json));
62+
Assert.AreEqual(DTYP03, exception.Code);
63+
Console.WriteLine(exception);
64+
}
65+
66+
[TestMethod]
67+
public void When_UndefinedDataTypeArgument_ExceptionThrown()
68+
{
69+
var schema =
70+
"""
71+
#array($undefined)
72+
""";
73+
var json =
74+
"""
75+
[10, 20]
76+
""";
77+
78+
JsonSchema.IsValid(schema, json);
79+
var exception = Assert.ThrowsException<DefinitionNotFoundException>(
80+
() => JsonAssert.IsValid(schema, json));
81+
Assert.AreEqual(DEFI03, exception.Code);
82+
Console.WriteLine(exception);
83+
}
84+
85+
[TestMethod]
86+
public void When_UndefinedNestedDataTypeArgument_ExceptionThrown()
87+
{
88+
var schema =
89+
"""
90+
#integer*($undefined) #array
91+
""";
92+
var json =
93+
"""
94+
[10, 20]
95+
""";
96+
97+
JsonSchema.IsValid(schema, json);
98+
var exception = Assert.ThrowsException<DefinitionNotFoundException>(
99+
() => JsonAssert.IsValid(schema, json));
100+
Assert.AreEqual(DEFI04, exception.Code);
101+
Console.WriteLine(exception);
102+
}
103+
104+
[TestMethod]
105+
public void When_DataTypeArgumentWithValidationFailed_ExceptionThrown()
106+
{
107+
var schema =
108+
"""
109+
%define $test: {"k1": #string}
110+
%schema: #object($test)
111+
""";
112+
var json =
113+
"""
114+
{"k1": 10}
115+
""";
116+
117+
JsonSchema.IsValid(schema, json);
118+
var exception = Assert.ThrowsException<JsonSchemaException>(
119+
() => JsonAssert.IsValid(schema, json));
120+
Assert.AreEqual(DTYP04, exception.Code);
121+
Console.WriteLine(exception);
122+
}
123+
124+
[TestMethod]
125+
public void When_NestedDataTypeArgumentWithValidationFailed_ExceptionThrown()
126+
{
127+
var schema =
128+
"""
129+
%define $test: {"k1": #string}
130+
%schema: #object*($test) #array
131+
""";
132+
var json =
133+
"""
134+
[{"k1": 10}]
135+
""";
136+
137+
JsonSchema.IsValid(schema, json);
138+
var exception = Assert.ThrowsException<JsonSchemaException>(
139+
() => JsonAssert.IsValid(schema, json));
140+
Assert.AreEqual(DTYP04, exception.Code);
141+
Console.WriteLine(exception);
142+
}
143+
}

0 commit comments

Comments
 (0)