Skip to content

Commit 773f1a7

Browse files
authoredAug 31, 2024··
Merge pull request #13 from relogiclabs/develop
Update Exception Handling
2 parents 0b6159f + 8dd016d commit 773f1a7

File tree

511 files changed

+17147
-9261
lines changed

Some content is hidden

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

511 files changed

+17147
-9261
lines changed
 

‎README.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ The next example represents an expanded version of the previous one, which bring
6767
```cpp
6868
%title: "Extended User Profile Dashboard API Response"
6969
%version: "2.0.0-extended"
70+
%import: com.relogiclabs.jschema.test.external.ExternalFunctions
7071

7172
%pragma DateDataTypeFormat: "DD-MM-YYYY"
7273
%pragma TimeDataTypeFormat: "DD-MM-YYYY hh:mm:ss"
@@ -105,6 +106,7 @@ The next example represents an expanded version of the previous one, which bring
105106
"isActive": #boolean, //user account current status
106107
"registeredAt": @after("01-01-2010 00:00:00") #time,
107108
"dataAccess": @checkAccess(&role) #integer,
109+
"ipAddress": @checkIPAddress #string,
108110
"profile": {
109111
"firstName": @regex("[A-Za-z]{3,50}") #string,
110112
"lastName": @regex("[A-Za-z]{3,50}") #string,
@@ -140,20 +142,21 @@ The next example represents an expanded version of the previous one, which bring
140142
if(role == "user" && target > 5) return fail(
141143
"ERRACCESS01", "Data access incompatible with 'user' role",
142144
expected("an access at most 5 for 'user' role"),
143-
actual("found access " + target + " which is greater than 5"));
145+
actual("found access " + target + " that is greater than 5"));
144146
}
145147
}
146148
```
147149
The subsequent JSON sample is an illustrative example that successfully validates against the expanded schema mentioned earlier. Within this example, recurring JSON structures appear that can be validated by defining components or nested functions and data types. Besides, reusing simple component definitions, you can achieve a clear and concise schema when validating large JSON with repetitive structures. This improves the overall readability and maintainability of the schema.
148150
```json
149151
{
150152
"user": {
151-
"id": 1234,
153+
"id": 1111,
152154
"username": "johndoe",
153155
"role": "admin",
154156
"isActive": true,
155157
"registeredAt": "06-09-2023 15:10:30",
156158
"dataAccess": 10,
159+
"ipAddress": "127.0.0.1",
157160
"profile": {
158161
"firstName": "John",
159162
"lastName": "Doe",
@@ -183,7 +186,7 @@ The subsequent JSON sample is an illustrative example that successfully validate
183186
"title": "Working with JSON in Java",
184187
"content": "Java provides great support for working with JSON...",
185188
"tags": [
186-
"CSharp",
189+
"Java",
187190
"JSON",
188191
"tutorial"
189192
]

‎doc/content/articles/components.md

+9-8
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,25 @@ weight = 10
77
# Schema Components
88
A schema component, also known as a reusable schema fragment or sub-schema, plays a vital role in improving readability, reducing redundancy, and organizing the structure of a Schema document. In JSON validation, a schema component or fragment defines a validation rule that can be recursively composed of multiple nested validation rules, collectively specifying the expected and valid format of a JSON construct.
99

10-
These schema components are used as an extension of data type validation, as core data types have limited features to validate the internal structure of a composite JSON value or construct. Therefore, a data type is parameterized with a schema component to validate the internal structure of such composite JSON constructs.
10+
These schema components are used as an extension of data type validation, as core data types have limited features to validate the internal structure of a composite JSON value or construct. Therefore, a data type is parameterized with a schema component to validate the internal structure of such composite JSON constructs.
1111

12-
The name or alias of a schema component always starts with `$` which also refers to the fact that they are named schema components or fragments defined elsewhere in the schema. Schema components can be referenced from any other part of the schema document, effectively reducing redundancy and enhancing reusability and readability. The following example defines a simple schema component named `$component` where the validation rule describes an object structure with two key-value pairs:
12+
The name or alias of a schema component always starts with `$` which unambiguously indicates that they are named schema components or fragments defined elsewhere in the schema. Schema components can be referenced from any parts of the schema document, effectively reducing redundancy and enhancing reusability and readability. The following example defines a simple schema component named `$component` where the validation rule describes an object structure with two key-value pairs:
1313
```js
1414
%define $component: { "key1": #integer, "key2": #string }
1515
```
1616

1717
A composite JSON construct is created by combining multiple values as defined by the JSON specification. These nested values can range from simple, like numbers or strings, to more complex, such as arrays or objects. While simple nested values of a composite construct can be validated using only nested data types and functions, handling hierarchical composite constructs with multiple layers of nested structures requires defining schema components.
1818

19-
The second and third rows of the following table illustrate how the component validates the value associated with the data type for which it is used as a parameter. If the associated data type is direct, the component validates the target value itself. Conversely, if the associated data type is nested, the component validates each of the nested values comprising the composite target construct.
19+
The second and third rows of the following table illustrate how the component validates the value associated with the data type for which it is used as a parameter. If the associated data type is direct, the component validates the target value itself. Conversely, if the associated data type is nested, the component validates each of the nested values.
2020

2121
| SN | Component Example | Valid Json |
2222
|----|----------------------------------------------------------------------------------|--------------------|
23-
| 1 | `@range*(1, 10) @length(5) #integer* #array` | `[1, 3, 5, 8, 10]` |
24-
| 2 | `%define $cmp: @range*(1, 10) #integer*` <br> `%schema: @length(5) #array($cmp)` | `[1, 3, 5, 8, 10]` |
25-
| 3 | `%define $cmp: @range(1, 10)` <br> `%schema: @length(5) #integer*($cmp) #array` | `[1, 3, 5, 8, 10]` |
23+
| 1 | `%schema: @range*(1, 10) @length(5) #integer* #array` | `[1, 3, 5, 8, 10]` |
24+
| 2 | `%define $cmp: @range(1, 10)` <br> `%schema: @length(5) #integer*($cmp) #array` | `[1, 3, 5, 8, 10]` |
25+
| 3 | `%define $cmp: @range*(1, 10) #integer*` <br> `%schema: @length(5) #array($cmp)` | `[1, 3, 5, 8, 10]` |
26+
| 4 | `%define $cmp: @range*(1, 10) @length(5) #integer* #array` <br> `%schema: $cmp` | `[1, 3, 5, 8, 10]` |
2627

27-
In the above table, all three rows have identical validation constraints for the input JSON array. This demonstrates that when dealing with simple and primitive nested values in a composite JSON construct, preferring the nested data types and functions is more convenient due to their simplicity and conciseness. However, in cases where the nested values are complex and composite, the schema component syntax becomes more suitable. The following example illustrates how the component syntax can be used to validate elements of a JSON array that are not as straightforward as the previous examples:
28+
In the above table, all four rows have identical validation constraints for the input JSON array. This demonstrates that when dealing with simple and primitive nested values in a composite JSON construct, preferring the nested data types and functions is more convenient due to their simplicity and conciseness. However, in cases where the nested values are complex and composite, the schema component syntax becomes more suitable. The following example illustrates how the component syntax can be used to validate elements of a JSON array that are not as straightforward as the previous examples:
2829
```js
2930
%define $article: {
3031
"id": @range(1, 100) #integer,
@@ -36,7 +37,7 @@ In the above table, all three rows have identical validation constraints for the
3637
%schema: @length(1, 10) #object*($article) #array
3738
```
3839

39-
In practical scenarios, JSON arrays often hold multiple composite JSON constructs as elements, typically sharing a recurring pattern and structure similar to the example above. To facilitate the validation of such elements, using schema components is highly effective.
40+
In practical scenarios, JSON arrays often hold multiple composite JSON constructs as elements, typically sharing a recurring pattern and structure similar to the example above. To facilitate the validation of such elements, using schema components is highly effective.
4041

4142
By defining a reusable schema component with a clear and descriptive name, one can improve the overall clarity and readability of the Schema document with recurring structures. This clarity not only makes it easier to understand the structure and intent of the schema but also contributes to keeping your complex schema well-organized, concise, and more manageable. For instance, consider the following example of a JSON document which is valid against the schema example provided above, demonstrating the usage of a schema component:
4243
```js

0 commit comments

Comments
 (0)
Please sign in to comment.