Skip to content

Commit 4a4ae63

Browse files
committed
add json schema input
1 parent 8d0ed78 commit 4a4ae63

35 files changed

+1317
-46
lines changed

docs/generators/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ All available generators, across languages and inputs:
2222
|---|---|---|---|---|---|---|---|---|
2323
| AsyncAPI | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
2424
| OpenAPI | ✔️ | ✔️ | ✔️ | ✔️ ||| ✔️ | ✔️ |
25+
| JSON Schema ||||||| ✔️ | ✔️ |
2526

2627
| **Languages** | [`payloads`](./payloads.md) | [`parameters`](./parameters.md) | [`headers`](./headers.md) | [`types`](./types.md) | [`channels`](./channels.md) | [`client`](./client.md) | [`models`](./models.md) | [`custom`](./custom.md) |
2728
|---|---|---|---|---|---|---|---|---|

docs/generators/models.md

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ export default {
1919
};
2020
```
2121

22-
The `models` preset provides native integration with [AsyncAPI Modelina](https://modelina.org) for generating TypeScript models directly from AsyncAPI and OpenAPI documents. This generator exposes Modelina's full capabilities, giving you complete control over model generation.
22+
The `models` preset provides native integration with [AsyncAPI Modelina](https://modelina.org) for generating TypeScript models directly from AsyncAPI, OpenAPI, and JSON Schema documents. This generator exposes Modelina's full capabilities, giving you complete control over model generation.
2323

24-
This is supported through the following inputs: `asyncapi`, `openapi`
24+
This is supported through the following inputs: `asyncapi`, `openapi`, `jsonschema`
2525

2626
It supports the following languages; [`typescript`](#typescript)
2727

@@ -161,6 +161,35 @@ export default {
161161
};
162162
```
163163

164+
### JSON Schema Input
165+
166+
```js
167+
export default {
168+
inputType: 'jsonschema',
169+
inputPath: 'user-schema.json',
170+
language: 'typescript',
171+
generators: [
172+
{
173+
preset: 'models',
174+
options: {
175+
modelType: 'class',
176+
enumType: 'enum'
177+
},
178+
renderers: [
179+
{
180+
class: {
181+
additionalContent: ({ content, model }) => {
182+
return `${content}\n\n // Custom validation method\n public validate(): boolean {\n return true;\n }`;
183+
}
184+
}
185+
}
186+
],
187+
outputPath: './src/models'
188+
}
189+
]
190+
};
191+
```
192+
164193
## Languages
165194

166195
### TypeScript

docs/inputs/jsonschema.md

Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
---
2+
sidebar_position: 3
3+
---
4+
5+
# JSON Schema
6+
7+
JSON Schema input support enables you to generate TypeScript models directly from JSON Schema documents. This is particularly useful when you have standalone JSON Schema files that define your data structures.
8+
9+
## Supported Generators
10+
11+
The JSON Schema input type supports the following generators:
12+
13+
| Generator | Support |
14+
|-----------|---------|
15+
| [`models`](../generators/models.md) | ✅ Full Support |
16+
| [`custom`](../generators/custom.md) | ✅ Full Support |
17+
18+
## Features
19+
20+
- **Direct JSON Schema Support**: Use JSON Schema files directly as input without wrapping them in AsyncAPI or OpenAPI documents
21+
- **Full Modelina Integration**: Leverages AsyncAPI Modelina for comprehensive TypeScript model generation
22+
- **Multiple Formats**: Supports both JSON (`.json`) and YAML (`.yaml`, `.yml`) file formats
23+
- **Schema Validation**: Built-in validation for JSON Schema documents
24+
- **Rich Model Generation**: Generate classes, interfaces, types, and enums from your schemas
25+
26+
## Configuration
27+
28+
### Basic Configuration
29+
30+
```js
31+
export default {
32+
inputType: 'jsonschema',
33+
inputPath: './user-schema.json',
34+
language: 'typescript',
35+
generators: [
36+
{
37+
preset: 'models',
38+
outputPath: './src/models'
39+
}
40+
]
41+
};
42+
```
43+
44+
### Advanced Configuration with Modelina Options
45+
46+
```js
47+
export default {
48+
inputType: 'jsonschema',
49+
inputPath: './complex-schema.json',
50+
language: 'typescript',
51+
generators: [
52+
{
53+
preset: 'models',
54+
outputPath: './src/models',
55+
options: {
56+
modelType: 'class',
57+
enumType: 'enum',
58+
mapType: 'record',
59+
rawPropertyNames: false,
60+
useJavascriptReservedKeywords: false
61+
},
62+
renderers: [
63+
{
64+
class: {
65+
property: ({ content, property }) => {
66+
return `/** ${property.property.description || 'Auto-generated property'} */\n${content}`;
67+
}
68+
}
69+
}
70+
]
71+
}
72+
]
73+
};
74+
```
75+
76+
## Examples
77+
78+
### Simple User Schema
79+
80+
**Input: `user-schema.json`**
81+
```json
82+
{
83+
"$schema": "http://json-schema.org/draft-07/schema#",
84+
"title": "User",
85+
"type": "object",
86+
"properties": {
87+
"id": {
88+
"type": "string",
89+
"format": "uuid"
90+
},
91+
"name": {
92+
"type": "string"
93+
},
94+
"email": {
95+
"type": "string",
96+
"format": "email"
97+
},
98+
"age": {
99+
"type": "integer",
100+
"minimum": 0
101+
}
102+
},
103+
"required": ["id", "name", "email"]
104+
}
105+
```
106+
107+
**Configuration: `codegen.mjs`**
108+
```js
109+
export default {
110+
inputType: 'jsonschema',
111+
inputPath: './user-schema.json',
112+
language: 'typescript',
113+
generators: [
114+
{
115+
preset: 'models',
116+
outputPath: './src/models'
117+
}
118+
]
119+
};
120+
```
121+
122+
**Generated Output: `src/models/User.ts`**
123+
```typescript
124+
export class User {
125+
private _id: string;
126+
private _name: string;
127+
private _email: string;
128+
private _age?: number;
129+
130+
constructor(input: {
131+
id: string,
132+
name: string,
133+
email: string,
134+
age?: number,
135+
}) {
136+
this._id = input.id;
137+
this._name = input.name;
138+
this._email = input.email;
139+
this._age = input.age;
140+
}
141+
142+
get id(): string { return this._id; }
143+
get name(): string { return this._name; }
144+
get email(): string { return this._email; }
145+
get age(): number | undefined { return this._age; }
146+
147+
public marshal(): string {
148+
return JSON.stringify({
149+
id: this.id,
150+
name: this.name,
151+
email: this.email,
152+
age: this.age,
153+
});
154+
}
155+
156+
public static unmarshal(json: string): User {
157+
const obj = JSON.parse(json);
158+
return new User(obj);
159+
}
160+
}
161+
```
162+
163+
### Complex Schema with Definitions
164+
165+
**Input: `complex-schema.json`**
166+
```json
167+
{
168+
"$schema": "http://json-schema.org/draft-07/schema#",
169+
"definitions": {
170+
"Address": {
171+
"type": "object",
172+
"properties": {
173+
"street": { "type": "string" },
174+
"city": { "type": "string" },
175+
"zipCode": { "type": "string" }
176+
},
177+
"required": ["street", "city", "zipCode"]
178+
}
179+
},
180+
"type": "object",
181+
"properties": {
182+
"person": {
183+
"type": "object",
184+
"properties": {
185+
"name": { "type": "string" },
186+
"address": { "$ref": "#/definitions/Address" }
187+
},
188+
"required": ["name"]
189+
}
190+
}
191+
}
192+
```
193+
194+
This will generate both `Person` and `Address` classes with proper type relationships.
195+
196+
## File Format Support
197+
198+
### JSON Format
199+
```json
200+
{
201+
"$schema": "http://json-schema.org/draft-07/schema#",
202+
"type": "object",
203+
"properties": {
204+
"example": { "type": "string" }
205+
}
206+
}
207+
```
208+
209+
### YAML Format
210+
```yaml
211+
$schema: "http://json-schema.org/draft-07/schema#"
212+
type: object
213+
properties:
214+
example:
215+
type: string
216+
```
217+
218+
## Best Practices
219+
220+
### Schema Structure
221+
- **Include `$schema`**: Always specify the JSON Schema draft version
222+
- **Use Descriptions**: Add descriptions to properties for better generated documentation
223+
- **Define Required Fields**: Clearly specify required properties
224+
- **Use Appropriate Formats**: Leverage format keywords like `email`, `uuid`, `date-time`
225+
226+
### Model Generation
227+
- **Choose Model Type**: Select between `class`, `interface`, or `type` based on your needs
228+
- **Enum Strategy**: Use `enum` for named constants, `union` for type unions
229+
- **Property Names**: Consider `rawPropertyNames` option for exact property name preservation
230+
231+
### File Organization
232+
- **Separate Concerns**: Use separate schema files for different domains
233+
- **Reference Definitions**: Use `$ref` to reference common definitions
234+
- **Modular Schemas**: Break complex schemas into smaller, reusable components
235+
236+
## Limitations
237+
238+
- **Single File Input**: Each configuration supports one JSON Schema file
239+
- **No Operation Definitions**: JSON Schema doesn't include API operations (use OpenAPI for that)
240+
- **No Channel Definitions**: JSON Schema doesn't include messaging patterns (use AsyncAPI for that)
241+
242+
For API operations, consider using [OpenAPI input](./openapi.md). For messaging patterns, consider using [AsyncAPI input](./asyncapi.md).
243+
244+
## Schema Validation
245+
246+
The JSON Schema input processor includes built-in validation:
247+
248+
- **Document Structure**: Validates basic JSON Schema structure
249+
- **Format Support**: Ensures the file format (JSON/YAML) is supported
250+
- **Content Validation**: Warns about empty or incomplete schemas
251+
- **Version Detection**: Detects and validates JSON Schema draft versions
252+
253+
## Integration with Other Tools
254+
255+
JSON Schema input works seamlessly with:
256+
- **JSON Schema Validators**: Generated models can be validated against the original schema
257+
- **API Documentation**: Use with tools like JSON Schema documentation generators
258+
- **Database Integration**: Generate models that match database schema definitions
259+
- **Form Generation**: Create form validation that matches your data models

0 commit comments

Comments
 (0)