You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -140,20 +142,21 @@ The next example represents an expanded version of the previous one, which bring
140
142
if(role == "user" && target > 5) return fail(
141
143
"ERRACCESS01", "Data access incompatible with 'user' role",
142
144
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"));
144
146
}
145
147
}
146
148
```
147
149
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.
148
150
```json
149
151
{
150
152
"user": {
151
-
"id": 1234,
153
+
"id": 1111,
152
154
"username": "johndoe",
153
155
"role": "admin",
154
156
"isActive": true,
155
157
"registeredAt": "06-09-2023 15:10:30",
156
158
"dataAccess": 10,
159
+
"ipAddress": "127.0.0.1",
157
160
"profile": {
158
161
"firstName": "John",
159
162
"lastName": "Doe",
@@ -183,7 +186,7 @@ The subsequent JSON sample is an illustrative example that successfully validate
183
186
"title": "Working with JSON in Java",
184
187
"content": "Java provides great support for working with JSON...",
Copy file name to clipboardExpand all lines: doc/content/articles/components.md
+9-8
Original file line number
Diff line number
Diff line change
@@ -7,24 +7,25 @@ weight = 10
7
7
# Schema Components
8
8
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.
9
9
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.
11
11
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:
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.
18
18
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.
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:
28
29
```js
29
30
%define $article: {
30
31
"id": @range(1, 100) #integer,
@@ -36,7 +37,7 @@ In the above table, all three rows have identical validation constraints for the
36
37
%schema: @length(1, 10) #object*($article) #array
37
38
```
38
39
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.
40
41
41
42
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:
0 commit comments