Skip to content

Commit b8c0d89

Browse files
Merge pull request #1 from relogiclabs/develop
Implement New Json Schema
2 parents a4b3274 + 07fcfc0 commit b8c0d89

File tree

155 files changed

+15688
-11
lines changed

Some content is hidden

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

155 files changed

+15688
-11
lines changed

.gitignore

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
# Compiled class file
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
!**/src/main/**/target/
4+
!**/src/test/**/target/
5+
6+
### Compiled class file ###
27
*.class
38

4-
# Log file
9+
### Log file ###
510
*.log
611

7-
# BlueJ files
8-
*.ctxt
9-
10-
# Mobile Tools for Java (J2ME)
11-
.mtj.tmp/
12-
13-
# Package Files #
12+
### Package Files ###
1413
*.jar
1514
*.war
1615
*.nar
@@ -19,6 +18,28 @@
1918
*.tar.gz
2019
*.rar
2120

22-
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
21+
### VM crash logs ###
2322
hs_err_pid*
2423
replay_pid*
24+
25+
### IDE Files ###
26+
.idea/
27+
*.iws
28+
*.iml
29+
*.ipr
30+
.apt_generated
31+
.classpath
32+
.factorypath
33+
.project
34+
.settings
35+
.springBeans
36+
.sts4-cache
37+
/nbproject/
38+
/nbbuild/
39+
/dist/
40+
/nbdist/
41+
/.nb-gradle/
42+
build/
43+
.vscode/
44+
.DS_Store
45+

LICENSE

Lines changed: 661 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 223 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,223 @@
1-
# Json Schema
1+
# A New JSON Schema
2+
A JSON Schema is crucial for making communication, interoperability, validation, testing, documentation, and specification seamless. All of this combined contributes to better maintenance and evolution of data-driven applications and systems. For a comprehensive overview of the roles and uses of JSON Schema in modern web applications, we invite you to explore our dedicated post available [here](https://www.relogiclabs.com/2023/01/the-roles-of-json-schema.html).
3+
4+
## Design Goals
5+
The traditional standard JSON Schema rigorously follows the conventional JSON structure, which unfortunately comes at the expense of simplicity, conciseness, and readability. Our goal is to develop a new JSON Schema that promotes these essential aspects that were previously missing.
6+
7+
This new schema is simple, lucid, easy to grasp, and doesn't require much prior knowledge to understand it. It also offers a shallow learning curve for both reading and writing. Additionally, its simplicity and conciseness allow us and machines to read-write more efficiently. Moreover, a large set of constraint data types and functions within the core schema facilitates the precise definition of JSON documents, significantly reducing the potential for communication gaps among collaborators. Furthermore, its inherent extensibility simplifies the process of integrating new constraints and functionalities to meet the diverse requirements of modern web services.
8+
9+
## Basic Example
10+
Let's explore an example of our schema for a typical JSON API response containing information about a user profile or account. The schema is very self-explanatory and thus almost no prior knowledge is required to understand the schema and the JSON responses specified by this schema.
11+
```cpp
12+
%title: "User Profile Response"
13+
%version: 1.0.0
14+
%schema:
15+
{
16+
"user": {
17+
"id": @range(1, 10000) #integer,
18+
/*username does not allow special characters*/
19+
"username": @regex("[a-z_]{3,30}") #string,
20+
/*currently only one role is allowed by system*/
21+
"role": "user" #string,
22+
"isActive": #boolean, //user account current status
23+
"registeredAt": #time,
24+
"profile": {
25+
"firstName": @regex("[A-Za-z ]{3,50}") #string,
26+
"lastName": @regex("[A-Za-z ]{3,50}") #string,
27+
"dateOfBirth": #date,
28+
"age": @range(18, 130) #integer,
29+
"email": @email #string,
30+
"pictureURL": @url #string,
31+
"address": {
32+
"street": @length(10, 200) #string,
33+
"city": @length(3, 50) #string,
34+
"country": @regex("[A-Za-z ]{3,50}") #string
35+
} #object #null
36+
}
37+
}
38+
}
39+
```
40+
In the above example, two types of constraint or rule descriptors are used: constraint functions (also known as validation functions, such as `@range(1, 10000)`) and constraint data types (also known as validation data types, such as `#integer`). All constraint functions begin with the `@` symbol, while all constraint data types start with `#`. C-style comments are also permitted in the schema. Please note that `address` can be `null` (eg. an optional input for users) and if it is `null` then no constraints of `address` are applicable. The following JSON is one of the examples which can successfully validate against the above schema. To start your journey with the JSON validation library, please consult the documentation available [here](https://relogiclabs.github.io/JsonSchema-DotNet/articles/intro.html).
41+
```json
42+
{
43+
"user": {
44+
"id": 1234,
45+
"username": "johndoe",
46+
"role": "user",
47+
"isActive": true,
48+
"registeredAt": "2023-09-06T15:10:30.639Z",
49+
"profile": {
50+
"firstName": "John",
51+
"lastName": "Doe",
52+
"dateOfBirth": "1993-06-17",
53+
"age": 30,
54+
"email": "[email protected]",
55+
"pictureURL": "https://example.com/picture.jpg",
56+
"address": {
57+
"street": "123 Some St",
58+
"city": "Some town",
59+
"country": "Some Country"
60+
}
61+
}
62+
}
63+
}
64+
```
65+
## Extended Example
66+
The next example represents an expanded version of the previous one, which brings more complexity. To effectively construct such schemas with multiple layers of nested structures, it's beneficial to have a fundamental understanding of this schema format. While the syntax may seem difficult at first, it becomes straightforward once you have a basic understanding of it. For more detailed information, reference documentation is available [here](https://relogiclabs.github.io/JsonSchema-DotNet/articles/intro.html).
67+
```cpp
68+
%title: "Extended User Profile Dashboard API Response"
69+
%version: 2.0.0
70+
%include: com.relogiclabs.json.schema.positive.ExternalFunctions
71+
%pragma IgnoreUndefinedProperties: true
72+
73+
%define $post: {
74+
"id": @range(1, 1000) #integer,
75+
"title": @length(10, 100) #string,
76+
"content": @length(30, 1000) #string,
77+
"tags": $tags
78+
} #object
79+
80+
%define $product: {
81+
"id": @length(2, 10) @regex("[a-z][a-z0-9]+") #string,
82+
"name": @length(5, 30) #string,
83+
"brand": @length(5, 30) #string,
84+
"price": @range(0.1, 1000000),
85+
"inStock": #boolean,
86+
"specs": {
87+
"cpu": @length(5, 30) #string,
88+
"ram": @regex("[0-9]{1,2}GB") #string,
89+
"storage": @regex("[0-9]{1,4}GB (SSD|HDD)") #string
90+
} #object #null
91+
}
92+
93+
%define $tags: @length(1, 10) #array($tag)
94+
%define $tag: @length(3, 20) @regex("[A-Za-z_]+") #string
95+
96+
%schema:
97+
{
98+
"user": {
99+
"id": @range(1, 10000) #integer,
100+
/*username does not allow special characters*/
101+
"username": @regex("[a-z_]{3,30}") #string,
102+
/*currently only one role is allowed by system*/
103+
"role": "user" #string,
104+
"isActive": #boolean, //user account current status
105+
"registeredAt": #time,
106+
"profile": {
107+
"firstName": @regex("[A-Za-z]{3,50}") #string,
108+
"lastName": @regex("[A-Za-z]{3,50}") #string,
109+
"dateOfBirth": @date("DD-MM-YYYY") #string,
110+
"age": @range(18, 128) #integer,
111+
"email": @email #string,
112+
"pictureURL": @url #string,
113+
"address": {
114+
"street": @length(10, 200) #string,
115+
"city": @length(3, 50) #string,
116+
"country": @regex("[A-Za-z ]{3,50}") #string
117+
} #object #null,
118+
"hobbies": !?
119+
},
120+
"posts": @length(0, 1000) #object*($post) #array,
121+
"preferences": {
122+
"theme": @enum("light", "dark") #string,
123+
"fontSize": @range(9, 24) #integer,
124+
"autoSave": #boolean
125+
}
126+
},
127+
"products": #object*($product) #array,
128+
"weather": {
129+
"temperature": @range(-50.0, 60.0) #float,
130+
"isCloudy": #boolean
131+
}
132+
}
133+
```
134+
The subsequent JSON sample is an illustrative example that successfully validates against the expanded schema mentioned earlier. Within this example, recurring JSON structure appear that can be validated through defining components. By reusing defined components, you can achieve a clear and concise schema when validating large JSON with repetitive structures instead of duplicating large and complex validation constraints across the schema. This improves the overall readability and maintainability of the schema.
135+
```json
136+
{
137+
"user": {
138+
"id": 1234,
139+
"username": "johndoe",
140+
"role": "user",
141+
"isActive": true,
142+
"registeredAt": "2023-09-06T15:10:30.639Z",
143+
"profile": {
144+
"firstName": "John",
145+
"lastName": "Doe",
146+
"dateOfBirth": "17-06-1993",
147+
"age": 30,
148+
"email": "[email protected]",
149+
"pictureURL": "https://example.com/picture.jpg",
150+
"address": {
151+
"street": "123 Some St",
152+
"city": "Some town",
153+
"country": "Some Country"
154+
}
155+
},
156+
"posts": [
157+
{
158+
"id": 1,
159+
"title": "Introduction to JSON",
160+
"content": "JSON (JavaScript Object Notation) is a lightweight data interchange format...",
161+
"tags": [
162+
"JSON",
163+
"tutorial",
164+
"data"
165+
]
166+
},
167+
{
168+
"id": 2,
169+
"title": "Working with JSON in Java",
170+
"content": "Java provides built-in support for working with JSON...",
171+
"tags": [
172+
"CSharp",
173+
"JSON",
174+
"tutorial"
175+
]
176+
},
177+
{
178+
"id": 3,
179+
"title": "Introduction to JSON Schema",
180+
"content": "A JSON schema defines the structure and data types of JSON objects...",
181+
"tags": [
182+
"Schema",
183+
"JSON",
184+
"tutorial"
185+
]
186+
}
187+
],
188+
"preferences": {
189+
"theme": "dark",
190+
"fontSize": 14,
191+
"autoSave": true
192+
}
193+
},
194+
"products": [
195+
{
196+
"id": "p1",
197+
"name": "Smartphone",
198+
"brand": "TechGiant",
199+
"price": 1.99,
200+
"inStock": true,
201+
"specs": null
202+
},
203+
{
204+
"id": "p2",
205+
"name": "Laptop",
206+
"brand": "SuperTech",
207+
"price": 159999.99,
208+
"inStock": false,
209+
"specs": {
210+
"cpu": "Ryzen 01",
211+
"ram": "8GB",
212+
"storage": "256GB SSD"
213+
}
214+
}
215+
],
216+
"weather": {
217+
"temperature": 25.5,
218+
"isCloudy": true,
219+
"conditions": null
220+
}
221+
}
222+
```
223+
For more information about the schema syntax format and library functionalities, please refer to the reference documentation [here](https://relogiclabs.github.io/JsonSchema-DotNet/api/index.html).

pom.xml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.relogiclabs.json</groupId>
8+
<artifactId>relogiclabs-json-schema</artifactId>
9+
<version>1.3.0</version>
10+
<name>New Json Schema</name>
11+
<description>A simplified, concise, intuitive, and extensible Json Schema</description>
12+
<url>https://www.relogiclabs.com/p/json-schema.html</url>
13+
<organization>
14+
<name>Relogic Labs</name>
15+
<url>https://www.relogiclabs.com</url>
16+
</organization>
17+
<licenses>
18+
<license>
19+
<name>GNU Affero General Public License</name>
20+
<url>https://www.gnu.org/licenses/agpl-3.0</url>
21+
<distribution>repo</distribution>
22+
</license>
23+
</licenses>
24+
<scm>
25+
<connection>scm:git:[email protected]:relogiclabs/JsonSchema-Java.git</connection>
26+
<developerConnection>scm:git:[email protected]:relogiclabs/JsonSchema-Java.git</developerConnection>
27+
<url>https://github.com/relogiclabs/JsonSchema-Java.git</url>
28+
</scm>
29+
30+
<properties>
31+
<maven.compiler.source>17</maven.compiler.source>
32+
<maven.compiler.target>17</maven.compiler.target>
33+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
34+
</properties>
35+
36+
<dependencies>
37+
<dependency>
38+
<groupId>org.antlr</groupId>
39+
<artifactId>antlr4-runtime</artifactId>
40+
<version>4.13.0</version>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.projectlombok</groupId>
44+
<artifactId>lombok</artifactId>
45+
<version>1.18.28</version>
46+
<scope>provided</scope>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.apache.commons</groupId>
50+
<artifactId>commons-lang3</artifactId>
51+
<version>3.13.0</version>
52+
</dependency>
53+
<dependency>
54+
<groupId>org.junit.jupiter</groupId>
55+
<artifactId>junit-jupiter-engine</artifactId>
56+
<version>5.10.0</version>
57+
<scope>test</scope>
58+
</dependency>
59+
</dependencies>
60+
</project>

0 commit comments

Comments
 (0)