Skip to content

Commit cecb26a

Browse files
committed
Merge pull request #5 from relogiclabs/develop
Update Validation Features
2 parents 3cda351 + bb6e26c commit cecb26a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+1750
-398
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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 AggregatedTests
8+
{
9+
10+
[TestMethod]
11+
public void When_JsonSchemaAggregatedTestWithWrongData_ExceptionThrown()
12+
{
13+
var schema =
14+
"""
15+
%title: "User Profile Response"
16+
%version: 1.0.0
17+
%schema:
18+
{
19+
"user": {
20+
"id": @range(1, 10000) #integer,
21+
/*username does not allow special characters*/
22+
"username": @regex("[a-z_]{3,30}") #string,
23+
/*currently only one role is allowed by system*/
24+
"role": "user" #string,
25+
"isActive": #boolean, //user account current status
26+
"profile": {
27+
"firstName": @regex("[A-Za-z ]{3,50}") #string,
28+
"lastName": @regex("[A-Za-z ]{3,50}") #string,
29+
"age": @range(18, 130) #integer,
30+
"email": @email #string,
31+
"pictureURL": @url #string,
32+
"address": {
33+
"street": @length(10, 200) #string,
34+
"city": @length(3, 50) #string,
35+
"country": @regex("[A-Za-z ]{3,50}") #string
36+
} #object #null
37+
}
38+
}
39+
}
40+
""";
41+
var json =
42+
"""
43+
{
44+
"user": {
45+
"id": "not number",
46+
"username": "john doe",
47+
"role": "user",
48+
"isActive": true,
49+
"profile": {
50+
"firstName": "John",
51+
"lastName": "Doe",
52+
"age": 30,
53+
"email": "[email protected]",
54+
"pictureURL": "https://example.com/picture.jpg",
55+
"address": {
56+
"street": "123 Some St",
57+
"city": "Some town",
58+
"country": "Some Country"
59+
}
60+
}
61+
}
62+
}
63+
""";
64+
JsonSchema.IsValid(schema, json);
65+
var exception = Assert.ThrowsException<JsonSchemaException>(
66+
() => JsonAssert.IsValid(schema, json));
67+
Assert.AreEqual(DTYP04, exception.Code);
68+
Console.WriteLine(exception);
69+
}
70+
}

JsonSchema.Tests/RelogicLabs/JsonSchema/Tests/Negative/FunctionTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,25 @@ namespace RelogicLabs.JsonSchema.Tests.Negative;
77
public class FunctionTests
88
{
99

10+
[TestMethod]
11+
public void When_FunctionAppliedOnWrongType_ExceptionThrown()
12+
{
13+
var schema =
14+
"""
15+
%schema: @range(10, 20)
16+
""";
17+
var json =
18+
"""
19+
"test"
20+
""";
21+
22+
//JsonSchema.IsValid(schema, json);
23+
var exception = Assert.ThrowsException<FunctionMismatchException>(
24+
() => JsonAssert.IsValid(schema, json));
25+
Assert.AreEqual(FUNC03, exception.Code);
26+
Console.WriteLine(exception);
27+
}
28+
1029
[TestMethod]
1130
public void When_ExternalIncludeNotInheritBaseClass_ExceptionThrown()
1231
{
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
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 NumberTests
8+
{
9+
[TestMethod]
10+
public void When_JsonLessThanMinimumFloat_ExceptionThrown()
11+
{
12+
var schema =
13+
"""
14+
@minimum(10) #float
15+
""";
16+
var json =
17+
"""
18+
9.999999
19+
""";
20+
JsonSchema.IsValid(schema, json);
21+
var exception = Assert.ThrowsException<JsonSchemaException>(
22+
() => JsonAssert.IsValid(schema, json));
23+
Assert.AreEqual(MINI01, exception.Code);
24+
Console.WriteLine(exception);
25+
}
26+
27+
[TestMethod]
28+
public void When_JsonGreaterThanMaximumInteger_ExceptionThrown()
29+
{
30+
var schema =
31+
"""
32+
@maximum(10) #integer
33+
""";
34+
var json =
35+
"""
36+
1000
37+
""";
38+
JsonSchema.IsValid(schema, json);
39+
var exception = Assert.ThrowsException<JsonSchemaException>(
40+
() => JsonAssert.IsValid(schema, json));
41+
Assert.AreEqual(MAXI01, exception.Code);
42+
Console.WriteLine(exception);
43+
}
44+
45+
[TestMethod]
46+
public void When_WrongJsonWithNestedMinimumIntegerInArray_ExceptionThrown()
47+
{
48+
var schema =
49+
"""
50+
@minimum*(10.5) #integer*
51+
""";
52+
var json =
53+
"""
54+
[
55+
1111,
56+
100,
57+
10
58+
]
59+
""";
60+
JsonSchema.IsValid(schema, json);
61+
var exception = Assert.ThrowsException<JsonSchemaException>(
62+
() => JsonAssert.IsValid(schema, json));
63+
Assert.AreEqual(MINI01, exception.Code);
64+
Console.WriteLine(exception);
65+
}
66+
67+
[TestMethod]
68+
public void When_WrongJsonWithNestedMinimumFloatInObject_ExceptionThrown()
69+
{
70+
var schema =
71+
"""
72+
@minimum*(100) #number*
73+
""";
74+
var json =
75+
"""
76+
{
77+
"key1": 100.000,
78+
"key2": 99.99,
79+
"key3": 200.884
80+
}
81+
""";
82+
JsonSchema.IsValid(schema, json);
83+
var exception = Assert.ThrowsException<JsonSchemaException>(
84+
() => JsonAssert.IsValid(schema, json));
85+
Assert.AreEqual(MINI01, exception.Code);
86+
Console.WriteLine(exception);
87+
}
88+
89+
[TestMethod]
90+
public void When_WrongJsonWithNestedMaximumNumberInArray_ExceptionThrown()
91+
{
92+
var schema =
93+
"""
94+
@maximum*(1000.05) #number*
95+
""";
96+
var json =
97+
"""
98+
[
99+
-1000.05,
100+
1000.05,
101+
1001
102+
]
103+
""";
104+
JsonSchema.IsValid(schema, json);
105+
var exception = Assert.ThrowsException<JsonSchemaException>(
106+
() => JsonAssert.IsValid(schema, json));
107+
Assert.AreEqual(MAXI01, exception.Code);
108+
Console.WriteLine(exception);
109+
}
110+
111+
[TestMethod]
112+
public void When_NestedMaximumFloatInObject_ValidTrue()
113+
{
114+
var schema =
115+
"""
116+
@maximum*(100) #float*
117+
""";
118+
var json =
119+
"""
120+
{
121+
"key1": 100.000,
122+
"key2": -150.407,
123+
"key3": 100.00001
124+
}
125+
""";
126+
JsonSchema.IsValid(schema, json);
127+
var exception = Assert.ThrowsException<JsonSchemaException>(
128+
() => JsonAssert.IsValid(schema, json));
129+
Assert.AreEqual(MAXI01, exception.Code);
130+
Console.WriteLine(exception);
131+
}
132+
133+
[TestMethod]
134+
public void When_NestedMinimumExclusiveFloatInObject_ValidTrue()
135+
{
136+
var schema =
137+
"""
138+
@minimum*(100, true) #float*
139+
""";
140+
var json =
141+
"""
142+
{
143+
"key1": 500.999,
144+
"key2": 100.001,
145+
"key3": 100.000
146+
}
147+
""";
148+
JsonSchema.IsValid(schema, json);
149+
var exception = Assert.ThrowsException<JsonSchemaException>(
150+
() => JsonAssert.IsValid(schema, json));
151+
Assert.AreEqual(MINI03, exception.Code);
152+
Console.WriteLine(exception);
153+
}
154+
155+
[TestMethod]
156+
public void When_NestedMaximumExclusiveFloatInObject_ValidTrue()
157+
{
158+
var schema =
159+
"""
160+
@maximum*(100, true) #float*
161+
""";
162+
var json =
163+
"""
164+
{
165+
"key1": 99.999,
166+
"key2": 10.407,
167+
"key3": 100.000
168+
}
169+
""";
170+
JsonSchema.IsValid(schema, json);
171+
var exception = Assert.ThrowsException<JsonSchemaException>(
172+
() => JsonAssert.IsValid(schema, json));
173+
Assert.AreEqual(MAXI03, exception.Code);
174+
Console.WriteLine(exception);
175+
}
176+
}

JsonSchema.Tests/RelogicLabs/JsonSchema/Tests/Positive/AggregatedTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public void When_SchemaAggregatedTest_ValidTrue()
88
{
99
var schema = """
1010
%title: "Example Schema For Some Json HTTP Request or Response"
11-
%version: 1.0.1
11+
%version: 2023.09.11
1212
%include: RelogicLabs.JsonSchema.Tests.Positive.ExternalFunctions,
1313
RelogicLabs.JsonSchema.Tests
1414
@@ -56,7 +56,7 @@ public void When_SchemaAggregatedTest_ValidTrue()
5656
""";
5757
JsonAssert.IsValid(schema, json);
5858
}
59-
59+
6060
[TestMethod]
6161
public void When_JsonAggregatedTest_ValidTrue()
6262
{
@@ -153,10 +153,10 @@ public void When_SimpleJsonSchemaAggregatedTest_ValidTrue()
153153
}
154154
}
155155
""";
156-
156+
157157
JsonAssert.IsValid(schema, json);
158158
}
159-
159+
160160
[TestMethod]
161161
public void When_ExtendedJsonSchemaAggregatedTest_ValidTrue()
162162
{
@@ -314,7 +314,7 @@ public void When_ExtendedJsonSchemaAggregatedTest_ValidTrue()
314314
}
315315
}
316316
""";
317-
317+
318318
JsonAssert.IsValid(schema, json);
319319
}
320320
}

0 commit comments

Comments
 (0)