ajsc 1.0.1 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +145 -0
- package/package.json +5 -12
package/README.md
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
# ajsc - Another JSON Schema Parser
|
|
2
|
+
|
|
3
|
+
**ajsc** is an npm package that transforms JSON Schema definitions into an intermediate representation (IR) called `IRNode`. This intermediate form is designed to be consumed by language-specific converters—such as the built-in [TypeScript converter](#typescriptconverter)—or by any custom converter you create for languages like Kotlin or Swift. This library streamlines the process of converting API JSON Schema definitions into strong-typed code representations, ensuring consistency between your API contracts and client implementations.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- **Comprehensive Schema Parsing:**
|
|
10
|
+
Convert JSON Schema types including strings, numbers, booleans, nulls, literals, enums, unions, intersections, arrays, and objects into a unified IR.
|
|
11
|
+
|
|
12
|
+
- **Intermediate Representation (`IRNode`):**
|
|
13
|
+
The `IRNode` provides a standard structure that captures type, constraints, and path information. This representation is ideal for further transformation into various target languages.
|
|
14
|
+
|
|
15
|
+
- **Plugin-Based Architecture:**
|
|
16
|
+
Use the provided language plugin (currently, a [TypeScript converter](#typescriptconverter)) or write your own converter to support additional languages such as Kotlin or Swift.
|
|
17
|
+
|
|
18
|
+
- **$defs and References Handling:**
|
|
19
|
+
Resolve JSON Schema definitions (`$defs`) and references (`$ref`) seamlessly, ensuring reusable schema components are properly processed.
|
|
20
|
+
|
|
21
|
+
- **Unique Signature Tracking:**
|
|
22
|
+
For objects and arrays, the library tracks unique signatures to avoid duplicate type definitions when converting to code.
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
## Installation
|
|
27
|
+
|
|
28
|
+
Install **ajsc** via npm:
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
npm install ajsc
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Usage
|
|
35
|
+
### Basic Conversion to IRNode
|
|
36
|
+
The primary function of ajsc is to convert a JSON Schema into an IRNode. Here’s an example of converting a simple JSON Schema that defines a string type:
|
|
37
|
+
|
|
38
|
+
```javascript
|
|
39
|
+
import { JSONSchemaConverter } from "ajsc";
|
|
40
|
+
|
|
41
|
+
const schema = { type: "string" };
|
|
42
|
+
const converter = new JSONSchemaConverter(schema);
|
|
43
|
+
|
|
44
|
+
console.log(converter.irNode);
|
|
45
|
+
// Output:
|
|
46
|
+
// {
|
|
47
|
+
// type: "string",
|
|
48
|
+
// constraints: {},
|
|
49
|
+
// path: "",
|
|
50
|
+
// }
|
|
51
|
+
Converting Complex Schemas
|
|
52
|
+
You can also convert more complex schemas including objects, arrays, unions, intersections, and even schemas with $defs:
|
|
53
|
+
|
|
54
|
+
javascript
|
|
55
|
+
Copy
|
|
56
|
+
import { JSONSchemaConverter } from "ajsc";
|
|
57
|
+
|
|
58
|
+
const complexSchema = {
|
|
59
|
+
$defs: {
|
|
60
|
+
Person: {
|
|
61
|
+
type: "object",
|
|
62
|
+
properties: {
|
|
63
|
+
name: { type: "string" },
|
|
64
|
+
age: { type: "number" },
|
|
65
|
+
},
|
|
66
|
+
required: ["name"],
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
type: "object",
|
|
70
|
+
properties: {
|
|
71
|
+
person: { $ref: "#/$defs/Person" },
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const converter = new JSONSchemaConverter(complexSchema);
|
|
76
|
+
console.log(converter.irNode);
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## API Reference
|
|
80
|
+
### JSONSchemaConverter
|
|
81
|
+
- Constructor:
|
|
82
|
+
`new JSONSchemaConverter(schema: object)`
|
|
83
|
+
- Properties:
|
|
84
|
+
- irNode: The resulting intermediate representation of the JSON Schema.
|
|
85
|
+
- Supported Schema Types:
|
|
86
|
+
- Primitive Types: string, number, boolean, null
|
|
87
|
+
- Literals: Using the const keyword.
|
|
88
|
+
- Enums: Using the enum property.
|
|
89
|
+
- Unions: When type is an array (e.g., ["string", "number"]).
|
|
90
|
+
- Intersections: Using allOf to combine multiple schemas.
|
|
91
|
+
- Arrays: With type: "array" and an items schema.
|
|
92
|
+
- Objects: With type: "object" and a properties definition.
|
|
93
|
+
- $defs and $ref: For defining and referencing reusable schema parts.
|
|
94
|
+
|
|
95
|
+
### TypescriptConverter
|
|
96
|
+
|
|
97
|
+
The package includes a TypeScript language plugin that converts the IRNode into TypeScript type definitions.
|
|
98
|
+
|
|
99
|
+
- Constructor:
|
|
100
|
+
`new TypescriptConverter(schema: object, options?: { inlineTypes?: boolean })`
|
|
101
|
+
- Properties:
|
|
102
|
+
- code: A string containing the generated TypeScript code.
|
|
103
|
+
|
|
104
|
+
Usage Example:
|
|
105
|
+
|
|
106
|
+
```javascript
|
|
107
|
+
import { TypescriptConverter } from "ajsc";
|
|
108
|
+
|
|
109
|
+
const schema = {
|
|
110
|
+
type: "object",
|
|
111
|
+
properties: {
|
|
112
|
+
name: { type: "string" },
|
|
113
|
+
age: { type: "number" },
|
|
114
|
+
contacts: {
|
|
115
|
+
type: "array",
|
|
116
|
+
items: {
|
|
117
|
+
type: "object",
|
|
118
|
+
properties: {
|
|
119
|
+
email: { type: "string" },
|
|
120
|
+
},
|
|
121
|
+
required: ["email"],
|
|
122
|
+
},
|
|
123
|
+
},
|
|
124
|
+
profile: {
|
|
125
|
+
type: "object",
|
|
126
|
+
properties: {
|
|
127
|
+
email: { type: "string" },
|
|
128
|
+
},
|
|
129
|
+
required: ["email"],
|
|
130
|
+
},
|
|
131
|
+
},
|
|
132
|
+
required: ["name", "age"],
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
const tsConverter = new TypescriptConverter(schema, { inlineTypes: true });
|
|
136
|
+
console.log(tsConverter.code);
|
|
137
|
+
|
|
138
|
+
// Expected output (formatted):
|
|
139
|
+
// {
|
|
140
|
+
// name: string;
|
|
141
|
+
// age: number;
|
|
142
|
+
// contacts?: Array<{ email: string; }>;
|
|
143
|
+
// profile?: { email: string; };
|
|
144
|
+
// }
|
|
145
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ajsc",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "Another Json-Schema Converter",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -11,17 +11,10 @@
|
|
|
11
11
|
"test": "vitest --run"
|
|
12
12
|
},
|
|
13
13
|
"exports": {
|
|
14
|
-
"
|
|
15
|
-
"types": "./dist/
|
|
16
|
-
"
|
|
17
|
-
|
|
18
|
-
"./TypescriptConverter": {
|
|
19
|
-
"types": "./dist/TypescriptConverter.d.ts",
|
|
20
|
-
"default": "./dist/TypescriptConverter.js"
|
|
21
|
-
},
|
|
22
|
-
"./TypescriptProcedureConverter": {
|
|
23
|
-
"types": "./dist/TypescriptProcedureConverter.d.ts",
|
|
24
|
-
"default": "./dist/TypescriptProcedureConverter.js"
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"import": "./dist/index.js",
|
|
17
|
+
"require": "./dist/index.js"
|
|
25
18
|
}
|
|
26
19
|
},
|
|
27
20
|
"keywords": [
|