4 releases
| 0.2.2 | Dec 14, 2024 |
|---|---|
| 0.2.1 | Aug 14, 2024 |
| 0.2.0 | Aug 13, 2024 |
| 0.1.0 | Aug 12, 2024 |
#137 in #dsl
286 downloads per month
340KB
606 lines
JSON Schema DSL
A simple DSL to generate JSON Schema with one-liner style.
Why JSON Schema DSL?
-
Make JSON Schema concise:

-
AI friendly: Function calling, Structured Output with simple DSL:

-
Schema friendly for CSV, Excel, Text2SQL:

Get Started
CLI: cargo install json-schema-dsl
$ json-schema-dsl "User{ id: int, name: string, email: Email}"
Output as following:
{
"$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
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<string>}
- 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,bytesorbytea(base64)integer: aliases:int,bigint,long,serial,bigserial,int32,int64,int96,int128number: aliases:float,double,real,decimalboolean: aliases:bool
Extra Types
Extra types are for semantic meaning, and they are all string type.
Path:/path/to/fileS3Path:s3://bucket/keyUlidorULIDColor:#F7F8FAIsbnorISBN:978-3-16-148410-0SemVer:1.2.3PhoneNumber:+1-202-555-0192CreditCard:4111 1111 1111 1111Currency:USD,CNYLanguage:en,zh-CNLocale:en-US,zh-CNMimeType:application/jsonBase64: base64 encoded string
array Types
array type is alike List<T>, and T is a basic type or format name.
List: aliases:listArray: aliases:arraySet(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:
DateTimeDatetimeTimestampIntervalDurationEmailHostnameDomainnameIpv4Ipv6UriUuidorUUIDJsonorJSON: JSON textXmlorXML: XML text
Misc
- range:
age: int(18,),age: int(,150)orage: int(1,18) - string length range:
nick: string(6,32),varchar(32) - array items length range:
list<string>(2),list<float>(1536) - tuple:
income: [int, string] - enum:
enum('a', 'b', 'c')orenum(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
- M-Schema: a semi-structure representation of database schema - https://github.com/XGenerationLab/M-Schema
Dependencies
~1.2–2.3MB
~45K SLoC