11using RelogicLabs . JsonSchema . Exceptions ;
22using static RelogicLabs . JsonSchema . Message . ErrorCode ;
3+ using static RelogicLabs . JsonSchema . Tests . Positive . ExternalFunctions ;
34
45namespace RelogicLabs . JsonSchema . Tests . Negative ;
56
67[ TestClass ]
78public 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+ 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}
0 commit comments