Skip to content

Commit 082624b

Browse files
Merge pull request #10 from relogiclabs/develop
Add Deferred Validation and Receiver Features
2 parents 0cf55af + c281f8f commit 082624b

File tree

95 files changed

+5370
-977
lines changed

Some content is hidden

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

95 files changed

+5370
-977
lines changed

JsonSchema.Tests/JsonSchema.Tests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
<Description>This project contains test cases for JsonSchema project.</Description>
66
<Authors>Relogic Labs</Authors>
77
<Company>Relogic Labs</Company>
8-
<Version>1.8.0</Version>
9-
<AssemblyVersion>1.8.0</AssemblyVersion>
8+
<Version>1.9.0</Version>
9+
<AssemblyVersion>1.9.0</AssemblyVersion>
1010
<Copyright>Copyright © Relogic Labs. All rights reserved.</Copyright>
1111
<NeutralLanguage>en</NeutralLanguage>
1212
<TargetFrameworks>net5.0;net6.0;net7.0</TargetFrameworks>

JsonSchema.Tests/RelogicLabs/JsonSchema/Tests/Negative/AggregatedTests.cs

Lines changed: 166 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
using RelogicLabs.JsonSchema.Exceptions;
22
using static RelogicLabs.JsonSchema.Message.ErrorCode;
3+
using static RelogicLabs.JsonSchema.Tests.Positive.ExternalFunctions;
34

45
namespace RelogicLabs.JsonSchema.Tests.Negative;
56

67
[TestClass]
78
public class AggregatedTests
89
{
9-
1010
[TestMethod]
1111
public void When_JsonSchemaAggregatedTestWithWrongData_ExceptionThrown()
1212
{
@@ -67,4 +67,169 @@ public void When_JsonSchemaAggregatedTestWithWrongData_ExceptionThrown()
6767
Assert.AreEqual(DTYP04, exception.Code);
6868
Console.WriteLine(exception);
6969
}
70+
71+
[TestMethod]
72+
public void When_ExtendedAggregatedTestWithInvalidAccess_ExceptionThrown()
73+
{
74+
var schema = """
75+
%title: "Extended User Profile Dashboard API Response"
76+
%version: 2.0.0
77+
%include: RelogicLabs.JsonSchema.Tests.Positive.ExternalFunctions,
78+
RelogicLabs.JsonSchema.Tests
79+
80+
%pragma IgnoreUndefinedProperties: true
81+
82+
%define $post: {
83+
"id": @range(1, 1000) #integer,
84+
"title": @length(10, 100) #string,
85+
"content": @length(30, 1000) #string,
86+
"tags": $tags
87+
} #object
88+
%define $product: {
89+
"id": @length(2, 10) @regex("[a-z][a-z0-9]+") #string,
90+
"name": @length(5, 30) #string,
91+
"brand": @length(5, 30) #string,
92+
"price": @range(0.1, 1000000),
93+
"inStock": #boolean,
94+
"specs": {
95+
"cpu": @length(5, 30) #string,
96+
"ram": @regex("[0-9]{1,2}GB") #string,
97+
"storage": @regex("[0-9]{1,4}GB (SSD|HDD)") #string
98+
} #object #null
99+
}
100+
%define $tags: @length(1, 10) #string*($tag) #array
101+
%define $tag: @length(3, 20) @regex("[A-Za-z_]+") #string
102+
%schema:
103+
{
104+
"user": {
105+
"id": @range(1, 10000) #integer,
106+
/*username does not allow special characters*/
107+
"username": @regex("[a-z_]{3,30}") #string,
108+
"role": @enum("user", "admin") #string &role,
109+
"isActive": #boolean, //user account current status
110+
"registeredAt": @time("DD-MM-YYYY hh:mm:ss") #string,
111+
"dataAccess": @checkAccess(&role) #integer,
112+
"profile": {
113+
"firstName": @regex("[A-Za-z]{3,50}") #string,
114+
"lastName": @regex("[A-Za-z]{3,50}") #string,
115+
"dateOfBirth": @date("DD-MM-YYYY") #string,
116+
"age": @range(18, 128) #integer,
117+
"email": @email #string,
118+
"pictureURL": @url #string,
119+
"address": {
120+
"street": @length(10, 200) #string,
121+
"city": @length(3, 50) #string,
122+
"country": @regex("[A-Za-z ]{3,50}") #string
123+
} #object #null,
124+
"hobbies": !?
125+
},
126+
"posts": @length(0, 1000) #object*($post) #array,
127+
"preferences": {
128+
"theme": @enum("light", "dark") #string,
129+
"fontSize": @range(9, 24) #integer,
130+
"autoSave": #boolean
131+
}
132+
},
133+
"products": #object*($product) #array,
134+
"weather": {
135+
"temperature": @range(-50, 60) #integer #float,
136+
"isCloudy": #boolean
137+
}
138+
}
139+
""";
140+
var json = """
141+
{
142+
"user": {
143+
"id": 1234,
144+
"username": "johndoe",
145+
"role": "user",
146+
"isActive": true,
147+
"registeredAt": "06-09-2023 15:10:30",
148+
"dataAccess": 6,
149+
"profile": {
150+
"firstName": "John",
151+
"lastName": "Doe",
152+
"dateOfBirth": "17-06-1993",
153+
"age": 30,
154+
"email": "[email protected]",
155+
"pictureURL": "https://example.com/picture.jpg",
156+
"address": {
157+
"street": "123 Some St",
158+
"city": "Some town",
159+
"country": "Some Country"
160+
}
161+
},
162+
"posts": [
163+
{
164+
"id": 1,
165+
"title": "Introduction to JSON",
166+
"content": "JSON (JavaScript Object Notation) is a lightweight data interchange format...",
167+
"tags": [
168+
"JSON",
169+
"tutorial",
170+
"data"
171+
]
172+
},
173+
{
174+
"id": 2,
175+
"title": "Working with JSON in C#",
176+
"content": "C# provides built-in support for working with JSON...",
177+
"tags": [
178+
"CSharp",
179+
"JSON",
180+
"tutorial"
181+
]
182+
},
183+
{
184+
"id": 3,
185+
"title": "Introduction to JSON Schema",
186+
"content": "A JSON schema defines the structure and data types of JSON objects...",
187+
"tags": [
188+
"Schema",
189+
"JSON",
190+
"tutorial"
191+
]
192+
}
193+
],
194+
"preferences": {
195+
"theme": "dark",
196+
"fontSize": 14,
197+
"autoSave": true
198+
}
199+
},
200+
"products": [
201+
{
202+
"id": "p1",
203+
"name": "Smartphone",
204+
"brand": "TechGiant",
205+
"price": 599.99,
206+
"inStock": true,
207+
"specs": null
208+
},
209+
{
210+
"id": "p2",
211+
"name": "Laptop",
212+
"brand": "SuperTech",
213+
"price": 1299.99,
214+
"inStock": false,
215+
"specs": {
216+
"cpu": "Intel i11",
217+
"ram": "11GB",
218+
"storage": "11GB SSD"
219+
}
220+
}
221+
],
222+
"weather": {
223+
"temperature": 25.5,
224+
"isCloudy": false,
225+
"conditions": null
226+
}
227+
}
228+
""";
229+
JsonSchema.IsValid(schema, json);
230+
var exception = Assert.ThrowsException<JsonSchemaException>(
231+
() => JsonAssert.IsValid(schema, json));
232+
Assert.AreEqual(ERRACCESS01, exception.Code);
233+
Console.WriteLine(exception);
234+
}
70235
}

JsonSchema.Tests/RelogicLabs/JsonSchema/Tests/Negative/ExternalFunctions.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public bool Odd(JNumber target)
1717
public class ExternalFunctions2 : FunctionBase
1818
{
1919
public ExternalFunctions2(RuntimeContext runtime) : base(runtime) { }
20-
20+
2121
public void Odd(JNumber target)
2222
{
2323
bool result = target % 2 != 0;
@@ -28,13 +28,19 @@ public void Odd(JNumber target)
2828
public class ExternalFunctions3 : FunctionBase
2929
{
3030
public ExternalFunctions3(RuntimeContext runtime) : base(runtime) { }
31-
31+
3232
public bool Odd() => throw new InvalidOperationException();
3333
}
3434

3535
public class ExternalFunctions4 : FunctionBase
3636
{
3737
public ExternalFunctions4(RuntimeContext runtime) : base(runtime) { }
38+
39+
public bool CanTest(JNumber target)
40+
{
41+
// If you just want to throw any exception without details
42+
return FailWith(new Exception("something went wrong"));
43+
}
3844
}
3945

4046
public class ExternalFunctions5 : FunctionBase

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public void When_ExternalFunctionNotExists_ExceptionThrown()
143143
"""
144144
%include: RelogicLabs.JsonSchema.Tests.Negative.ExternalFunctions4,
145145
RelogicLabs.JsonSchema.Tests
146-
%schema: @odd #integer
146+
%schema: @notExist #integer
147147
""";
148148
var json = "10";
149149

@@ -153,4 +153,21 @@ public void When_ExternalFunctionNotExists_ExceptionThrown()
153153
Assert.AreEqual(FUNC05, exception.Code);
154154
Console.WriteLine(exception);
155155
}
156+
157+
[TestMethod]
158+
public void When_FunctionThrowArbitraryException_ExceptionThrown()
159+
{
160+
var schema =
161+
"""
162+
%include: RelogicLabs.JsonSchema.Tests.Negative.ExternalFunctions4,
163+
RelogicLabs.JsonSchema.Tests
164+
%schema: @canTest #integer
165+
""";
166+
var json = "10";
167+
168+
JsonSchema.IsValid(schema, json);
169+
var exception = Assert.ThrowsException<Exception>(
170+
() => JsonAssert.IsValid(schema, json));
171+
Console.WriteLine(exception);
172+
}
156173
}

0 commit comments

Comments
 (0)