Skip to content

Commit

Permalink
Merge pull request #11 from relogiclabs/develop
Browse files Browse the repository at this point in the history
Update project documentations
  • Loading branch information
zhossain-info committed Apr 25, 2024
2 parents 1330860 + 22c775d commit df506bf
Show file tree
Hide file tree
Showing 350 changed files with 25,315 additions and 14,351 deletions.
22 changes: 16 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# A New JSON Schema
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. If you are interested in an overview of the roles and usage of JSON Schema in modern web applications, check out our in-depth post [here](https://www.relogiclabs.com/2023/01/the-roles-of-json-schema.html).
# JSchema - The New JSON Schema
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. If you are interested in a comprehensive overview of the roles and uses of JSON Schema in modern web applications, check out our in-depth post [here](https://www.relogiclabs.com/2023/01/the-roles-of-json-schema.html).

## Design Goals
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.
Expand All @@ -10,7 +10,7 @@ This new schema is simple, lucid, easy to grasp, and doesn't require much prior
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.
```cpp
%title: "User Profile Response"
%version: 1.0.0
%version: "1.0.0-basic"
%schema:
{
"user": {
Expand Down Expand Up @@ -63,11 +63,10 @@ In the above example, two types of constraints are used: constraint functions (a
}
```
## Extended Example
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/JSchema-Java/articles/introduction).
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, including custom validation functions, it's beneficial to have a fundamental understanding of this schema syntax. 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/JSchema-Java/articles/introduction).
```cpp
%title: "Extended User Profile Dashboard API Response"
%version: 2.0.0
%include: com.relogiclabs.json.schema.external.ExternalFunctions
%version: "2.0.0-extended"

%pragma DateDataTypeFormat: "DD-MM-YYYY"
%pragma TimeDataTypeFormat: "DD-MM-YYYY hh:mm:ss"
Expand Down Expand Up @@ -133,6 +132,17 @@ The next example represents an expanded version of the previous one, which bring
"isCloudy": #boolean
}
}

%script: {
constraint function checkAccess(role) {
// Receiver &role received only one value
// 'target' keyword refers to the target JSON value
if(role[0] == "user" && target > 5) return fail(
"ERRACCESS01", "Data access incompatible with 'user' role",
expected("an access at most 5 for 'user' role"),
actual("found access " + target + " which is greater than 5"));
}
}
```
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 instead of duplicating or referring to various structures across the schema. This improves the overall readability and maintainability of the schema.
```json
Expand Down
7 changes: 4 additions & 3 deletions doc/content/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ date = 2023-10-08T09:38:53+06:00
+++

# Introduction
The New JSON Schema prioritizes simplicity, conciseness, and readability, making it intuitive and accessible without the need for extensive prior knowledge. It offers efficient read-write facilities, precise JSON document definition through various data types and functions, and extensibility to meet modern web service diverse requirements.
JSchema, a new JSON Schema, prioritizes simplicity, conciseness, and readability, making it intuitive and accessible without the need for extensive prior knowledge. It offers efficient read-write facilities, precise JSON document definition through various data types and functions, and extensibility to meet modern web service diverse requirements.

JSON, short for JavaScript Object Notation, is one of the most widely used data exchange formats that are both user-friendly and machine-readable. New JSON Schema is a practical tool for defining and validating the structure of JSON documents. The following guides offer comprehensive details about the New JSON Schema. This document provides a list of links, each corresponding to a specific detail document available in this project.
JSON, short for JavaScript Object Notation, is one of the most widely used data exchange formats that are both user-friendly and machine-readable. JSchema is a practical tool for defining and validating the structure of JSON documents. The following guides provide comprehensive details about the JSchema.
<br/>

* <font size="4">[Getting Started & Installation](/JSchema-Java/articles/quickstart)</font>
Expand All @@ -15,9 +15,10 @@ JSON, short for JavaScript Object Notation, is one of the most widely used data
* <font size="4">[Schema Specification & Grammar](/JSchema-Java/articles/specification)</font>
* <font size="4">[Data Types & Validation](/JSchema-Java/articles/datatypes)</font>
* <font size="4">[Functions & Validation](/JSchema-Java/articles/functions)</font>
* <font size="4">[JSchema CScript & Validation](/JSchema-Java/articles/cscript)</font>
* <font size="4">[Date & Time Patterns](/JSchema-Java/articles/datetime)</font>
* <font size="4">[Defining Schema Components](/JSchema-Java/articles/components)</font>
* <font size="4">[Build from Source Code](/JSchema-Java/articles/sourcebuild)</font>
* <font size="4">[API Reference Documentation](/JSchema-Java/api/index.html)</font>
* <font size="4">[GitHub Source Code Repository](https://github.com/relogiclabs/JSchema-Java)</font>
<br/>
<br/>
2 changes: 1 addition & 1 deletion doc/content/articles/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ title = 'Articles'
date = 2023-10-08T09:38:53+06:00
+++

The New JSON Schema prioritizes simplicity, conciseness, and readability, making it user-friendly and accessible without the need for extensive prior knowledge. It offers efficient read-write facilities, precise JSON document definition through various data types and functions, and extensibility to meet modern web service diverse requirements.
JSchema, a new JSON Schema, prioritizes simplicity, conciseness, and readability, ensuring user-friendliness and accessibility without requiring extensive prior knowledge. It offers efficient read-write capabilities, precise JSON document definition through various data types and functions, and extensibility to address the diverse requirements of modern web services.
8 changes: 4 additions & 4 deletions doc/content/articles/components.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
+++
title = 'Components'
date = 2023-12-15T09:38:53+06:00
weight = 9
weight = 10
+++

# Schema Components
Expand Down Expand Up @@ -45,19 +45,19 @@ By defining a reusable schema component with a clear and descriptive name, one c
"id": 1,
"title": "Getting Started",
"preview": "This guide will show you through the essential steps to quickly...",
"tags": ["JSON", "Json Schema", "Quick Start"]
"tags": ["JSON", "JSchema", "Quick Start"]
},
{
"id": 2,
"title": "Validation Syntax",
"preview": "A JSON document is a structured data format used for the exchange...",
"tags": ["JSON", "Json Schema", "Validation Syntax"]
"tags": ["JSON", "JSchema", "Validation Syntax"]
},
{
"id": 3,
"title": "Constraint Functions",
"preview": "This document serves as a brief overview, providing key insights into...",
"tags": ["JSON", "Json Schema", "Constraint Functions"]
"tags": ["JSON", "JSchema", "Constraint Functions"]
}
]
```
97 changes: 97 additions & 0 deletions doc/content/articles/cscript.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
+++
title = 'CScript DSL'
date = 2024-01-25T09:38:53+06:00
weight = 8
+++

# CScript Domain-Specific Language
CScript, a domain-specific scripting language, designed specifically for JSchema document. This lightweight, interpreted, and dynamically typed C-style language seamlessly integrates with the foundational principles of the existing JSchema architecture. With the use of dynamic typing and programming constructs similar to both JSON and JavaScript, streamlines the process of writing concise, user-friendly, and flexible logic for constraint or validation functions.

Although CScript is similar to JavaScript in many aspects, the CScript interpreter is designed to quickly halt (fail-fast) and report errors instead of trying to continue with potentially corrupted or unexpected data. This ensures a seamless alignment with the validation requirements of modern web services, significantly enhancing readability and productivity in both documentation and implementation, as well as reducing the potential for bugs.

## Keywords & Reserved Words
Here is a list of keywords (reserved words) in the CScript language. You cannot use any of the following words as identifiers in your scripts. Some keywords are reserved for future extensions and are not currently in use.

| Description | Current Keyword (Reserved Word) |
|---------------------------|--------------------------------------------------|
| Variable Declaration | `var`; (`const`) |
| Conditional Flow Control | `if`; `else`; (`switch`; `case`; `default`) |
| Iterative Flow Control | `for`; `foreach`; `while`; (`do`) |
| Jump Control | `break`; (`continue`) |
| Membership Management | `in` |
| Function Declaration | `function`; `constraint`; `future`; `subroutine` |
| Function Data & Control | `return`; `target`; `caller` |
| Literals Value | `true`; `false`; `null`; `undefined` |
| Exception Management | `tryof`; `throw` |
| Class & Object Management | (`class`; `new`; `super`; `this`) |
| Script Modularization | (`import`) |

## Data Types
Data types play a pivotal role in specifying the fundamental structure of values that serve as the foundational components for data manipulation within CScript. In the dynamically typed CScript language, data types are classified into two main categories: primitive immutable value types and composite reference types.

Composite types include `#array` and `#object`, while the remaining types fall under the category of primitive types. Both primitive values and composite references of schema accessible from CScript are readonly and unmodifiable nodes. The following table lists CScript data types:

| SN | Type Name | Example |
|----|--------------|--------------------------------------------|
| 1 | `#array` | `[1, 2, 3]`; `["item1", [2.5, 5.8]]`; `[]` |
| 2 | `#boolean` | `true`; `false` |
| 3 | `#double` | `10.5`; `20E-5`; `5E+5` |
| 4 | `#integer` | `10`; `20`; `100`; `500` |
| 5 | `#null` | `null` |
| 6 | `#object` | `{ k1: "text", k2: { "k11" : 10 } }`; `{}` |
| 7 | `#range` | `1..10`; `-10..-5`; `-10..`; `..100` |
| 8 | `#string` | `"any text"`; `""` |
| 9 | `#undefined` | `undefined` |
| 10 | `#void` | Used only for internal purposes |

The `#void` type is reserved for internal operations, including initializing unassigned l-values (variables, properties, and array elements) to the default state, among other cases. To explicitly represent the absence of a value in your script, use the `undefined` or `null` literal.

## Operators & Precedences
CScript operators are symbols that are used to perform operations on variables and values. The direct operation of any operator that requires a modifiable l-value including `++`, `--` or `=` will raise an exception for the readonly schema nodes. The following table lists the operators according to their precedences from the highest to the lowest:

| SN | Category | Operator |
|----|-------------------------------|----------------------|
| 1 | Property Access & Parentheses | `.`; `[]`; `()` |
| 2 | Unary Minus & Logical Not | `-`; `!` |
| 3 | Postfix Increment/Decrement | `i++`; `i--` |
| 4 | Prefix Increment/Decrement | `++i`; `--i` |
| 5 | Arithmetic Multiplicative | `*`; `/` |
| 6 | Arithmetic Additive | `+`; `-` |
| 7 | Sequence Range | `..` |
| 8 | Relational Comparison | `>`; `<`; `>=`; `<=` |
| 9 | Equality Comparison | `==`; `!=` |
| 10 | Logical And (Short-Circuit) | `&&` |
| 11 | Logical Or (Short-Circuit) | `\|\|` |
| 12 | Assignment | `=` |

## Function Types
Function types are essential for specifying the executable units that serve as the building-blocks of validation process within CScript. All function types can also accept variable number of arguments, specified by an ellipsis `...` after the last parameter name which is then bound to an array containing the remaining arguments.

Additionally, all types of functions can be overloaded with varying numbers of fixed parameters, along with one that includes a variable argument parameter. A function name can be overloaded with only one variable argument definition, regardless of the number of required arguments preceding it. Fixed argument functions always take precedence or priority over variable argument functions when arguments match both definitions. Below are the various kinds of functions, each with distinct purposes, used in the CScript language:

### Constraint Function
The constraint function defines conditions for the target JSON value. They assess whether the target JSON value satisfies the conditions specified by the functions. The function should return true if the conditions are met; otherwise, it should return false. Even if the function exits without returning any value, it is assumed that all conditions are met since any early return from the function usually implies that the conditions are not satisfied.

Within the scope including nested scopes of a constraint function, the `target` keyword refers to the JSON value or node to which the constraint function is applied, and the `caller` keyword refers to the schema node that invokes this constraint function. The subsequent example illustrates various alternative forms of the definition of a constraint function named `example` with the main keyword `constraint`:
```js
constraint example(param1, param2, param3) { }
constraint function example(param1, param2, params...) { }
```

### Future Constraint Function
The future constraint function extends the utility of the constraint function by also considering receiver values. They ensure that the validations are deferred until the receivers of the schema have received their anticipated values, thus evaluating the specified conditions imposed by the functions at a delayed phase. The subsequent example illustrates various alternative forms of the definition of a future constraint function named `example` with the main keyword `future`:
```js
future example(param1, param2, param3) { }
future constraint example(param1, param2, param3) { }
future function example(param1, param2, param3) { }
future constraint function example(param1, param2, params...) { }
```

### Subroutine Function
The subroutine function supports the constraint function by promoting code reusability, readability, and modularization. These functions serve as auxiliary units, enhancing the organization and maintainability of validation in CScript, and are not available in the schema context for invocation. Conversely, the constraint function, as well as the future function, are special functions available in the schema context, and are not invocable from the script context without the `target` and `caller` information, thereby preventing any potential overloading conflicts between subroutine and constraint functions. The subsequent example illustrates various alternative forms of the definition of a subroutine function named `example` with the main keyword `subroutine`:
```js
subroutine example(param1, param2, param3) { }
subroutine function example(param1, param2, params...) { }
```

The CScript language provides a wide range of functions, data types, and programming constructs that can be used to implement diverse validation logic. This allows for the handling of complex validation requirements, both at the level of individual JSON values and groups of values received across different parts of the JSON document, ultimately ensuring the structural integrity of the entire JSON document.
2 changes: 1 addition & 1 deletion doc/content/articles/datatypes.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ The boolean data type is a binary or switch-based data type that only accepts tw
```

### The Null Data Type
The null data type serves as a special constraint within JSON schemas, facilitating the controlled use of `null` in place of other JSON elements or values. Typically, it is combined with other data types to permit the use of `null` for specific JSON elements or values. This can set constraints for scenarios in which an array without any elements and an object without any properties can either have `null` or only be allowed to be empty.
The null data type serves as a special constraint within JSchema documents, facilitating the controlled use of `null` in place of other JSON elements or values. Typically, it is combined with other data types to permit the use of `null` for specific JSON elements or values. This can set constraints for scenarios in which an array without any elements and an object without any properties can either have `null` or only be allowed to be empty.

Additionally, the `@nonempty` constraint functions can be employed to further control the use of empty values within a JSON document. To specify the null type in the schema, use the following syntax:
```html
Expand Down
Loading

0 comments on commit df506bf

Please sign in to comment.