https://github.com/linux-china/json-schema-dsl
JSON Schema DSL
https://github.com/linux-china/json-schema-dsl
Last synced: about 1 year ago
JSON representation
JSON Schema DSL
- Host: GitHub
- URL: https://github.com/linux-china/json-schema-dsl
- Owner: linux-china
- License: apache-2.0
- Created: 2024-08-12T15:07:17.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-12-14T07:29:39.000Z (over 1 year ago)
- Last Synced: 2025-04-02T02:51:10.584Z (over 1 year ago)
- Language: Rust
- Size: 377 KB
- Stars: 7
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
JSON Schema DSL
==================
A simple DSL to generate JSON Schema with one-liner style.
# Why JSON Schema DSL?
1. Make JSON Schema concise:

2. AI friendly: Function calling, Structured Output with simple DSL:

3. Schema friendly for CSV, Excel, Text2SQL:

# Get Started
CLI: `cargo install json-schema-dsl`
```shell
$ json-schema-dsl "User{ id: int, name: string, email: Email}"
```
Output as following:
```json
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "User",
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
},
"email": {
"type": "string",
"format": "email"
}
},
"required": [
"id",
"name",
"email"
]
}
```
Rust library: `cargo add json-schema-dsl serde_json`
```rust
fn main() {
let struct_text = "User {id: int, name: string, email: Email}";
let json_schema = json_schema_dsl::to_json_schema(struct_text).unwrap();
println!("{}", serde_json::to_string_pretty(&json_schema).unwrap());
}
```
# Syntax

`User { id: int, name: string, birth_date: Date, email?: Email, tags: List}`
- Object Name: starts with capital character, such as `ObjectName { field: type }`.
- Field name: starts with lower-case character.
- Optional field: `field?: type`
### Basic Types
JSON Schema basic types:
- `string`: aliases: `varchar`, `Text`, `String`, `bytes` or `bytea`(base64)
- `integer`: aliases: `int`, `bigint`, `long`, `serial`, `bigserial`,`int32`, `int64`, `int96`, `int128`
- `number`: aliases: `float`, `double`, `real`, `decimal`
- `boolean`: aliases: `bool`
### array Types
array type is alike `List`, and T is a basic type or format name.
- `List`: aliases: `list`
- `Array`: aliases: `array`
- `Set`(uniqueItems): aliases: `set`
### object Type
Declare object type: `field: ObjectName {field: type}`.
**Attention**: `ObjectName` should start with Capital Character.
### Formats
JSON Schema formats, and name should start with a capital letter:
- `Date`
- `Time`
- `Datetime`
- `Timestamp`
- `Interval`
- `Duration`
- `Email`
- `Hostname`
- `Ipv4`
- `Ipv6`
- `Uri`
- `Hostname`
- `Uuid` or `UUID`
- `Json` or `JSON`: JSON text
- `Xml` or `XML`: XML text
### Misc
- range: `age: int(18,)`, `age: int(,150)` or `age: int(1,18)`
- string length range: `nick: string(6,32)`, `varchar(32)`
- array items length range: `list(2)`, `list(1536)`
- tuple: `income: [int, string]`
- enum: `enum('a', 'b', 'c')` or `enum(1, 2, 3)`
- regex: `regex('^[a-z]+$')`
- anyOf: `field: type1|type2`, no space between types
- additionalProperties: `{field: type, ...}`, ellipsis before `}`.
# References
* JSON Schema: https://json-schema.org/
* JSON Schema formats: https://json-schema.org/understanding-json-schema/reference/string#built-in-formats