@formspec/build 0.1.0-alpha.10 → 0.1.0-alpha.11
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/dist/browser.cjs +549 -0
- package/dist/browser.cjs.map +1 -0
- package/dist/browser.d.ts +4 -2
- package/dist/browser.d.ts.map +1 -1
- package/dist/browser.js +498 -45
- package/dist/browser.js.map +1 -1
- package/dist/build.d.ts +343 -33
- package/dist/cli.cjs +2267 -0
- package/dist/cli.cjs.map +1 -0
- package/dist/cli.js +2204 -101
- package/dist/cli.js.map +1 -1
- package/dist/generators/class-schema.d.ts +5 -9
- package/dist/generators/class-schema.d.ts.map +1 -1
- package/dist/index.cjs +2093 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +4 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2024 -114
- package/dist/index.js.map +1 -1
- package/dist/internals.cjs +1345 -0
- package/dist/internals.cjs.map +1 -0
- package/dist/internals.d.ts +1 -0
- package/dist/internals.d.ts.map +1 -1
- package/dist/internals.js +1297 -21
- package/dist/internals.js.map +1 -1
- package/dist/json-schema/generator.d.ts.map +1 -1
- package/dist/json-schema/schema.d.ts +16 -0
- package/dist/json-schema/schema.d.ts.map +1 -0
- package/dist/ui-schema/generator.d.ts +15 -0
- package/dist/ui-schema/generator.d.ts.map +1 -1
- package/dist/ui-schema/schema.d.ts +357 -0
- package/dist/ui-schema/schema.d.ts.map +1 -0
- package/dist/ui-schema/types.d.ts +8 -73
- package/dist/ui-schema/types.d.ts.map +1 -1
- package/package.json +14 -9
- package/dist/__tests__/analyzer-edge-cases.test.js +0 -376
- package/dist/__tests__/analyzer-edge-cases.test.js.map +0 -1
- package/dist/__tests__/analyzer.test.js +0 -190
- package/dist/__tests__/analyzer.test.js.map +0 -1
- package/dist/__tests__/cli.test.js +0 -178
- package/dist/__tests__/cli.test.js.map +0 -1
- package/dist/__tests__/codegen.test.js +0 -506
- package/dist/__tests__/codegen.test.js.map +0 -1
- package/dist/__tests__/decorator-pipeline.test.js +0 -460
- package/dist/__tests__/decorator-pipeline.test.js.map +0 -1
- package/dist/__tests__/edge-cases.test.js +0 -215
- package/dist/__tests__/edge-cases.test.js.map +0 -1
- package/dist/__tests__/fixtures/edge-cases.js +0 -137
- package/dist/__tests__/fixtures/edge-cases.js.map +0 -1
- package/dist/__tests__/fixtures/example-a-builtins.js +0 -100
- package/dist/__tests__/fixtures/example-a-builtins.js.map +0 -1
- package/dist/__tests__/fixtures/example-b-decorators.js +0 -5
- package/dist/__tests__/fixtures/example-b-decorators.js.map +0 -1
- package/dist/__tests__/fixtures/example-b-extended.js +0 -60
- package/dist/__tests__/fixtures/example-b-extended.js.map +0 -1
- package/dist/__tests__/fixtures/example-c-custom.js +0 -61
- package/dist/__tests__/fixtures/example-c-custom.js.map +0 -1
- package/dist/__tests__/fixtures/example-c-decorators.js +0 -4
- package/dist/__tests__/fixtures/example-c-decorators.js.map +0 -1
- package/dist/__tests__/fixtures/example-d-mixed-decorators.js +0 -75
- package/dist/__tests__/fixtures/example-d-mixed-decorators.js.map +0 -1
- package/dist/__tests__/fixtures/example-e-decorators.js +0 -10
- package/dist/__tests__/fixtures/example-e-decorators.js.map +0 -1
- package/dist/__tests__/fixtures/example-e-no-namespace.js +0 -61
- package/dist/__tests__/fixtures/example-e-no-namespace.js.map +0 -1
- package/dist/__tests__/fixtures/example-interface-types.js +0 -8
- package/dist/__tests__/fixtures/example-interface-types.js.map +0 -1
- package/dist/__tests__/fixtures/example-jsdoc-constraints.js +0 -98
- package/dist/__tests__/fixtures/example-jsdoc-constraints.js.map +0 -1
- package/dist/__tests__/fixtures/example-nested-class.js +0 -248
- package/dist/__tests__/fixtures/example-nested-class.js.map +0 -1
- package/dist/__tests__/fixtures/sample-forms.js +0 -78
- package/dist/__tests__/fixtures/sample-forms.js.map +0 -1
- package/dist/__tests__/generator.test.js +0 -234
- package/dist/__tests__/generator.test.js.map +0 -1
- package/dist/__tests__/integration.test.js +0 -161
- package/dist/__tests__/integration.test.js.map +0 -1
- package/dist/__tests__/interface-types.test.js +0 -404
- package/dist/__tests__/interface-types.test.js.map +0 -1
- package/dist/__tests__/jsdoc-constraints.test.js +0 -465
- package/dist/__tests__/jsdoc-constraints.test.js.map +0 -1
- package/dist/__tests__/write-schemas.test.js +0 -198
- package/dist/__tests__/write-schemas.test.js.map +0 -1
- package/dist/analyzer/class-analyzer.js +0 -377
- package/dist/analyzer/class-analyzer.js.map +0 -1
- package/dist/analyzer/decorator-extractor.js +0 -336
- package/dist/analyzer/decorator-extractor.js.map +0 -1
- package/dist/analyzer/jsdoc-constraints.js +0 -153
- package/dist/analyzer/jsdoc-constraints.js.map +0 -1
- package/dist/analyzer/program.js +0 -114
- package/dist/analyzer/program.js.map +0 -1
- package/dist/analyzer/type-converter.js +0 -474
- package/dist/analyzer/type-converter.js.map +0 -1
- package/dist/codegen/index.js +0 -597
- package/dist/codegen/index.js.map +0 -1
- package/dist/generators/class-schema.js +0 -140
- package/dist/generators/class-schema.js.map +0 -1
- package/dist/generators/method-schema.js +0 -108
- package/dist/generators/method-schema.js.map +0 -1
- package/dist/json-schema/generator.js +0 -166
- package/dist/json-schema/generator.js.map +0 -1
- package/dist/json-schema/types.js +0 -33
- package/dist/json-schema/types.js.map +0 -1
- package/dist/ui-schema/generator.js +0 -148
- package/dist/ui-schema/generator.js.map +0 -1
- package/dist/ui-schema/types.js +0 -8
- package/dist/ui-schema/types.js.map +0 -1
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Class schema generator.
|
|
3
|
-
*
|
|
4
|
-
* Generates JSON Schema and FormSpec/UI Schema from statically analyzed
|
|
5
|
-
* class fields and decorators.
|
|
6
|
-
*/
|
|
7
|
-
import { convertType, applyDecoratorsToSchema, createFormSpecField, } from "../analyzer/type-converter.js";
|
|
8
|
-
import { createProgramContext, findClassByName, findInterfaceByName, findTypeAliasByName, } from "../analyzer/program.js";
|
|
9
|
-
import { analyzeClass, analyzeInterface, analyzeTypeAlias } from "../analyzer/class-analyzer.js";
|
|
10
|
-
/**
|
|
11
|
-
* Generates JSON Schema and FormSpec from a class analysis.
|
|
12
|
-
*
|
|
13
|
-
* Uses static type information and decorator metadata to build
|
|
14
|
-
* complete schema definitions for a class's fields.
|
|
15
|
-
*
|
|
16
|
-
* @param analysis - The class analysis result
|
|
17
|
-
* @param checker - TypeScript type checker
|
|
18
|
-
* @returns Generated JSON Schema and FormSpec
|
|
19
|
-
*/
|
|
20
|
-
export function generateClassSchemas(analysis, checker) {
|
|
21
|
-
const properties = {};
|
|
22
|
-
const required = [];
|
|
23
|
-
const uiElements = [];
|
|
24
|
-
for (const field of analysis.fields) {
|
|
25
|
-
// Generate JSON Schema for field
|
|
26
|
-
const { jsonSchema: baseSchema } = convertType(field.type, checker);
|
|
27
|
-
const fieldSchema = applyDecoratorsToSchema(baseSchema, field.decorators, field);
|
|
28
|
-
properties[field.name] = fieldSchema;
|
|
29
|
-
// Track required fields
|
|
30
|
-
if (!field.optional) {
|
|
31
|
-
required.push(field.name);
|
|
32
|
-
}
|
|
33
|
-
// Generate FormSpec field
|
|
34
|
-
const formSpecField = createFormSpecField(field.name, field.type, field.decorators, field.optional, checker);
|
|
35
|
-
uiElements.push(formSpecField);
|
|
36
|
-
}
|
|
37
|
-
// Build complete JSON Schema
|
|
38
|
-
const jsonSchema = {
|
|
39
|
-
type: "object",
|
|
40
|
-
properties,
|
|
41
|
-
...(required.length > 0 ? { required } : {}),
|
|
42
|
-
};
|
|
43
|
-
// Build FormSpec/UI Schema
|
|
44
|
-
const uiSchema = {
|
|
45
|
-
elements: uiElements,
|
|
46
|
-
};
|
|
47
|
-
return { jsonSchema, uiSchema };
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Generates JSON Schema for a single field.
|
|
51
|
-
*
|
|
52
|
-
* Useful for generating schemas for method return types
|
|
53
|
-
* or individual field extraction.
|
|
54
|
-
*
|
|
55
|
-
* @param field - The field information
|
|
56
|
-
* @param checker - TypeScript type checker
|
|
57
|
-
* @returns JSON Schema for the field's type
|
|
58
|
-
*/
|
|
59
|
-
export function generateFieldSchema(field, checker) {
|
|
60
|
-
const { jsonSchema: baseSchema } = convertType(field.type, checker);
|
|
61
|
-
return applyDecoratorsToSchema(baseSchema, field.decorators, field);
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Generates JSON Schema and FormSpec/UI Schema from a decorated TypeScript class.
|
|
65
|
-
*
|
|
66
|
-
* This is a high-level entry point that handles the entire pipeline:
|
|
67
|
-
* creating a TypeScript program, finding the class, analyzing it,
|
|
68
|
-
* and generating schemas — all in one call.
|
|
69
|
-
*
|
|
70
|
-
* @example
|
|
71
|
-
* ```typescript
|
|
72
|
-
* const result = generateSchemasFromClass({
|
|
73
|
-
* filePath: "./src/forms.ts",
|
|
74
|
-
* className: "UserForm",
|
|
75
|
-
* });
|
|
76
|
-
* console.log(result.jsonSchema);
|
|
77
|
-
* ```
|
|
78
|
-
*
|
|
79
|
-
* @param options - File path, class name, and optional compiler options
|
|
80
|
-
* @returns Generated JSON Schema and FormSpec/UI Schema
|
|
81
|
-
*/
|
|
82
|
-
export function generateSchemasFromClass(options) {
|
|
83
|
-
const ctx = createProgramContext(options.filePath);
|
|
84
|
-
const classDecl = findClassByName(ctx.sourceFile, options.className);
|
|
85
|
-
if (!classDecl) {
|
|
86
|
-
throw new Error(`Class "${options.className}" not found in ${options.filePath}`);
|
|
87
|
-
}
|
|
88
|
-
const analysis = analyzeClass(classDecl, ctx.checker);
|
|
89
|
-
return generateClassSchemas(analysis, ctx.checker);
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* Generates JSON Schema and FormSpec/UI Schema from a named TypeScript
|
|
93
|
-
* type — a decorated class, an interface with TSDoc tags, or a type alias.
|
|
94
|
-
*
|
|
95
|
-
* This is the recommended entry point. It automatically detects whether
|
|
96
|
-
* the name resolves to a class, interface, or type alias and uses the
|
|
97
|
-
* appropriate analysis pipeline:
|
|
98
|
-
*
|
|
99
|
-
* - **Classes**: extracts decorators and JSDoc constraints
|
|
100
|
-
* - **Interfaces**: extracts TSDoc tags (`@Field_displayName`, `@Minimum`, etc.)
|
|
101
|
-
* - **Type aliases**: object literal bodies analyzed like interfaces
|
|
102
|
-
*
|
|
103
|
-
* @example
|
|
104
|
-
* ```typescript
|
|
105
|
-
* // Works with both classes and interfaces — caller doesn't need to know
|
|
106
|
-
* const result = generateSchemas({
|
|
107
|
-
* filePath: "./src/config.ts",
|
|
108
|
-
* typeName: "DiscountConfig",
|
|
109
|
-
* });
|
|
110
|
-
* ```
|
|
111
|
-
*
|
|
112
|
-
* @param options - File path and type name
|
|
113
|
-
* @returns Generated JSON Schema and FormSpec/UI Schema
|
|
114
|
-
*/
|
|
115
|
-
export function generateSchemas(options) {
|
|
116
|
-
const ctx = createProgramContext(options.filePath);
|
|
117
|
-
// Try class first
|
|
118
|
-
const classDecl = findClassByName(ctx.sourceFile, options.typeName);
|
|
119
|
-
if (classDecl) {
|
|
120
|
-
const analysis = analyzeClass(classDecl, ctx.checker);
|
|
121
|
-
return generateClassSchemas(analysis, ctx.checker);
|
|
122
|
-
}
|
|
123
|
-
// Try interface
|
|
124
|
-
const interfaceDecl = findInterfaceByName(ctx.sourceFile, options.typeName);
|
|
125
|
-
if (interfaceDecl) {
|
|
126
|
-
const analysis = analyzeInterface(interfaceDecl, ctx.checker);
|
|
127
|
-
return generateClassSchemas(analysis, ctx.checker);
|
|
128
|
-
}
|
|
129
|
-
// Try type alias
|
|
130
|
-
const typeAlias = findTypeAliasByName(ctx.sourceFile, options.typeName);
|
|
131
|
-
if (typeAlias) {
|
|
132
|
-
const result = analyzeTypeAlias(typeAlias, ctx.checker);
|
|
133
|
-
if (result.ok) {
|
|
134
|
-
return generateClassSchemas(result.analysis, ctx.checker);
|
|
135
|
-
}
|
|
136
|
-
throw new Error(result.error);
|
|
137
|
-
}
|
|
138
|
-
throw new Error(`Type "${options.typeName}" not found as a class, interface, or type alias in ${options.filePath}`);
|
|
139
|
-
}
|
|
140
|
-
//# sourceMappingURL=class-schema.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"class-schema.js","sourceRoot":"","sources":["../../src/generators/class-schema.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH,OAAO,EACL,WAAW,EACX,uBAAuB,EACvB,mBAAmB,GAEpB,MAAM,+BAA+B,CAAC;AAEvC,OAAO,EACL,oBAAoB,EACpB,eAAe,EACf,mBAAmB,EACnB,mBAAmB,GACpB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,+BAA+B,CAAC;AAcjG;;;;;;;;;GASG;AACH,MAAM,UAAU,oBAAoB,CAClC,QAAuB,EACvB,OAAuB;IAEvB,MAAM,UAAU,GAAwC,EAAE,CAAC;IAC3D,MAAM,QAAQ,GAAa,EAAE,CAAC;IAC9B,MAAM,UAAU,GAAoB,EAAE,CAAC;IAEvC,KAAK,MAAM,KAAK,IAAI,QAAQ,CAAC,MAAM,EAAE,CAAC;QACpC,iCAAiC;QACjC,MAAM,EAAE,UAAU,EAAE,UAAU,EAAE,GAAG,WAAW,CAAC,KAAK,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;QACpE,MAAM,WAAW,GAAG,uBAAuB,CAAC,UAAU,EAAE,KAAK,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;QACjF,UAAU,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,WAAW,CAAC;QAErC,wBAAwB;QACxB,IAAI,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC;YACpB,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QAC5B,CAAC;QAED,0BAA0B;QAC1B,MAAM,aAAa,GAAG,mBAAmB,CACvC,KAAK,CAAC,IAAI,EACV,KAAK,CAAC,IAAI,EACV,KAAK,CAAC,UAAU,EAChB,KAAK,CAAC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uDAAuD,OAAO,CAAC,QAAQ,EAAE,CACnG,CAAC;AACJ,CAAC"}
|
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Method schema generator.
|
|
3
|
-
*
|
|
4
|
-
* Generates schemas for method parameters and return types:
|
|
5
|
-
* - Parameters using FormSpec (InferSchema<typeof X>) -> runtime generation
|
|
6
|
-
* - Parameters with regular types -> static type conversion
|
|
7
|
-
* - Return types -> static type conversion
|
|
8
|
-
*/
|
|
9
|
-
import { convertType } from "../analyzer/type-converter.js";
|
|
10
|
-
/**
|
|
11
|
-
* Generates schemas for a method's parameters and return type.
|
|
12
|
-
*
|
|
13
|
-
* If a parameter references a FormSpec via `InferSchema<typeof X>`,
|
|
14
|
-
* the schemas are taken from the loaded FormSpec at runtime.
|
|
15
|
-
* Otherwise, schemas are generated from static type analysis.
|
|
16
|
-
*
|
|
17
|
-
* @param method - The method information from static analysis
|
|
18
|
-
* @param checker - TypeScript type checker
|
|
19
|
-
* @param loadedFormSpecs - Map of FormSpec export names to their schemas
|
|
20
|
-
* @returns Generated method schemas
|
|
21
|
-
*/
|
|
22
|
-
export function generateMethodSchemas(method, checker, loadedFormSpecs) {
|
|
23
|
-
// Generate return type schema from static analysis
|
|
24
|
-
const returnType = convertType(method.returnType, checker).jsonSchema;
|
|
25
|
-
// Handle parameters
|
|
26
|
-
const params = generateParamsSchemas(method.parameters, checker, loadedFormSpecs);
|
|
27
|
-
return {
|
|
28
|
-
name: method.name,
|
|
29
|
-
params,
|
|
30
|
-
returnType,
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* Generates schemas for method parameters.
|
|
35
|
-
*/
|
|
36
|
-
function generateParamsSchemas(parameters, checker, loadedFormSpecs) {
|
|
37
|
-
if (parameters.length === 0) {
|
|
38
|
-
return null;
|
|
39
|
-
}
|
|
40
|
-
// Check if any parameter uses InferSchema<typeof X>
|
|
41
|
-
for (const param of parameters) {
|
|
42
|
-
if (param.formSpecExportName) {
|
|
43
|
-
const formSpec = loadedFormSpecs.get(param.formSpecExportName);
|
|
44
|
-
if (formSpec) {
|
|
45
|
-
// Use runtime-generated schemas from FormSpec
|
|
46
|
-
// Cast to our ExtendedJSONSchema7 type (structurally compatible)
|
|
47
|
-
return {
|
|
48
|
-
jsonSchema: formSpec.jsonSchema,
|
|
49
|
-
uiSchema: formSpec.uiSchema,
|
|
50
|
-
formSpecExport: param.formSpecExportName,
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
// FormSpec not found - fall back to static analysis
|
|
54
|
-
console.warn(`Warning: FormSpec export "${param.formSpecExportName}" not found, using static analysis`);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
// Generate from static type analysis
|
|
58
|
-
if (parameters.length === 1 && parameters[0]) {
|
|
59
|
-
// Single parameter - use its type directly
|
|
60
|
-
const param = parameters[0];
|
|
61
|
-
const jsonSchema = convertType(param.type, checker).jsonSchema;
|
|
62
|
-
return {
|
|
63
|
-
jsonSchema,
|
|
64
|
-
uiSchema: null,
|
|
65
|
-
formSpecExport: null,
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
// Multiple parameters - create object schema
|
|
69
|
-
const properties = {};
|
|
70
|
-
const required = [];
|
|
71
|
-
for (const param of parameters) {
|
|
72
|
-
const paramSchema = convertType(param.type, checker).jsonSchema;
|
|
73
|
-
properties[param.name] = paramSchema;
|
|
74
|
-
// Only non-optional parameters should be marked as required
|
|
75
|
-
if (!param.optional) {
|
|
76
|
-
required.push(param.name);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
return {
|
|
80
|
-
jsonSchema: {
|
|
81
|
-
type: "object",
|
|
82
|
-
properties,
|
|
83
|
-
...(required.length > 0 ? { required } : {}),
|
|
84
|
-
},
|
|
85
|
-
uiSchema: null,
|
|
86
|
-
formSpecExport: null,
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
|
-
/**
|
|
90
|
-
* Collects all FormSpec export names referenced by methods.
|
|
91
|
-
*
|
|
92
|
-
* Use this to determine which exports to load at runtime.
|
|
93
|
-
*
|
|
94
|
-
* @param methods - Array of method infos
|
|
95
|
-
* @returns Set of FormSpec export names
|
|
96
|
-
*/
|
|
97
|
-
export function collectFormSpecReferences(methods) {
|
|
98
|
-
const references = new Set();
|
|
99
|
-
for (const method of methods) {
|
|
100
|
-
for (const param of method.parameters) {
|
|
101
|
-
if (param.formSpecExportName) {
|
|
102
|
-
references.add(param.formSpecExportName);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
return references;
|
|
107
|
-
}
|
|
108
|
-
//# sourceMappingURL=method-schema.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"method-schema.js","sourceRoot":"","sources":["../../src/generators/method-schema.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAIH,OAAO,EAAE,WAAW,EAAE,MAAM,+BAA+B,CAAC;AAuC5D;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,qBAAqB,CACnC,MAAkB,EAClB,OAAuB,EACvB,eAAmD;IAEnD,mDAAmD;IACnD,MAAM,UAAU,GAAG,WAAW,CAAC,MAAM,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC,UAAU,CAAC;IAEtE,oBAAoB;IACpB,MAAM,MAAM,GAAG,qBAAqB,CAAC,MAAM,CAAC,UAAU,EAAE,OAAO,EAAE,eAAe,CAAC,CAAC;IAElF,OAAO;QACL,IAAI,EAAE,MAAM,CAAC,IAAI;QACjB,MAAM;QACN,UAAU;KACX,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,SAAS,qBAAqB,CAC5B,UAA2B,EAC3B,OAAuB,EACvB,eAAmD;IAEnD,IAAI,UAAU,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC5B,OAAO,IAAI,CAAC;IACd,CAAC;IAED,oDAAoD;IACpD,KAAK,MAAM,KAAK,IAAI,UAAU,EAAE,CAAC;QAC/B,IAAI,KAAK,C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IACH,CAAC;IAED,OAAO;QACL,UAAU,EAAE;YACV,IAAI,EAAE,QAAQ;YACd,UAAU;YACV,GAAG,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;SAC7C;QACD,QAAQ,EAAE,IAAI;QACd,cAAc,EAAE,IAAI;KACrB,CAAC;AACJ,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,yBAAyB,CAAC,OAAqB;IAC7D,MAAM,UAAU,GAAG,IAAI,GAAG,EAAU,CAAC;IAErC,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;QAC7B,KAAK,MAAM,KAAK,IAAI,MAAM,CAAC,UAAU,EAAE,CAAC;YACtC,IAAI,KAAK,CAAC,kBAAkB,EAAE,CAAC;gBAC7B,UAAU,CAAC,GAAG,CAAC,KAAK,CAAC,kBAAkB,CAAC,CAAC;YAC3C,CAAC;QACH,CAAC;IACH,CAAC;IAED,OAAO,UAAU,CAAC;AACpB,CAAC"}
|
|
@@ -1,166 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* JSON Schema generator for FormSpec forms.
|
|
3
|
-
*/
|
|
4
|
-
/**
|
|
5
|
-
* Generates JSON Schema for nested elements (used for array items and object properties).
|
|
6
|
-
*/
|
|
7
|
-
function generateNestedSchema(elements) {
|
|
8
|
-
const properties = {};
|
|
9
|
-
const required = [];
|
|
10
|
-
collectFields(elements, properties, required);
|
|
11
|
-
// Deduplicate required array (can have duplicates when the same field is defined
|
|
12
|
-
// in multiple branches/containers, e.g., repeated in different conditional branches)
|
|
13
|
-
const uniqueRequired = [...new Set(required)];
|
|
14
|
-
return {
|
|
15
|
-
type: "object",
|
|
16
|
-
properties,
|
|
17
|
-
...(uniqueRequired.length > 0 && { required: uniqueRequired }),
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* Converts a single field to its JSON Schema representation.
|
|
22
|
-
*/
|
|
23
|
-
function fieldToJsonSchema(field) {
|
|
24
|
-
const base = {};
|
|
25
|
-
if (field.label !== undefined) {
|
|
26
|
-
base.title = field.label;
|
|
27
|
-
}
|
|
28
|
-
switch (field._field) {
|
|
29
|
-
case "text":
|
|
30
|
-
return { ...base, type: "string" };
|
|
31
|
-
case "number":
|
|
32
|
-
return {
|
|
33
|
-
...base,
|
|
34
|
-
type: "number",
|
|
35
|
-
...(field.min !== undefined && { minimum: field.min }),
|
|
36
|
-
...(field.max !== undefined && { maximum: field.max }),
|
|
37
|
-
};
|
|
38
|
-
case "boolean":
|
|
39
|
-
return { ...base, type: "boolean" };
|
|
40
|
-
case "enum": {
|
|
41
|
-
const opts = field.options;
|
|
42
|
-
const isObjectOptions = opts.length > 0 &&
|
|
43
|
-
opts.every((opt) => typeof opt === "object" && "id" in opt && "label" in opt);
|
|
44
|
-
if (isObjectOptions) {
|
|
45
|
-
// Object options with id/label: use oneOf with const/title
|
|
46
|
-
return {
|
|
47
|
-
...base,
|
|
48
|
-
type: "string",
|
|
49
|
-
oneOf: opts.map((o) => ({
|
|
50
|
-
const: o.id,
|
|
51
|
-
title: o.label,
|
|
52
|
-
})),
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
return { ...base, type: "string", enum: opts };
|
|
56
|
-
}
|
|
57
|
-
case "dynamic_enum":
|
|
58
|
-
// Dynamic enums are strings at the schema level
|
|
59
|
-
// The actual options are resolved at runtime
|
|
60
|
-
// x-formspec-source indicates the data source key
|
|
61
|
-
// x-formspec-params indicates dependent field names for fetching options
|
|
62
|
-
return {
|
|
63
|
-
...base,
|
|
64
|
-
type: "string",
|
|
65
|
-
"x-formspec-source": field.source,
|
|
66
|
-
...(field.params !== undefined &&
|
|
67
|
-
field.params.length > 0 && { "x-formspec-params": field.params }),
|
|
68
|
-
};
|
|
69
|
-
case "dynamic_schema":
|
|
70
|
-
// Dynamic schemas are objects with unknown properties
|
|
71
|
-
// x-formspec-schemaSource indicates where to load the schema from
|
|
72
|
-
return {
|
|
73
|
-
...base,
|
|
74
|
-
type: "object",
|
|
75
|
-
additionalProperties: true,
|
|
76
|
-
"x-formspec-schemaSource": field.schemaSource,
|
|
77
|
-
};
|
|
78
|
-
case "array": {
|
|
79
|
-
const arrayField = field;
|
|
80
|
-
return {
|
|
81
|
-
...base,
|
|
82
|
-
type: "array",
|
|
83
|
-
items: generateNestedSchema(arrayField.items),
|
|
84
|
-
...(arrayField.minItems !== undefined && { minItems: arrayField.minItems }),
|
|
85
|
-
...(arrayField.maxItems !== undefined && { maxItems: arrayField.maxItems }),
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
case "object": {
|
|
89
|
-
const objectField = field;
|
|
90
|
-
const nestedSchema = generateNestedSchema(objectField.properties);
|
|
91
|
-
return {
|
|
92
|
-
...base,
|
|
93
|
-
...nestedSchema,
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
default: {
|
|
97
|
-
// Exhaustiveness check
|
|
98
|
-
const _exhaustive = field;
|
|
99
|
-
return _exhaustive;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* Visits all elements in a form tree, collecting fields and required fields.
|
|
105
|
-
*/
|
|
106
|
-
function collectFields(elements, properties, required) {
|
|
107
|
-
for (const element of elements) {
|
|
108
|
-
switch (element._type) {
|
|
109
|
-
case "field":
|
|
110
|
-
properties[element.name] = fieldToJsonSchema(element);
|
|
111
|
-
if (element.required === true) {
|
|
112
|
-
required.push(element.name);
|
|
113
|
-
}
|
|
114
|
-
break;
|
|
115
|
-
case "group":
|
|
116
|
-
// Groups don't affect schema structure, just collect their children
|
|
117
|
-
collectFields(element.elements, properties, required);
|
|
118
|
-
break;
|
|
119
|
-
case "conditional":
|
|
120
|
-
// Conditional fields are still part of the schema
|
|
121
|
-
// They're just hidden/shown in the UI
|
|
122
|
-
collectFields(element.elements, properties, required);
|
|
123
|
-
break;
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
/**
|
|
128
|
-
* Generates a JSON Schema from a FormSpec.
|
|
129
|
-
*
|
|
130
|
-
* @example
|
|
131
|
-
* ```typescript
|
|
132
|
-
* const form = formspec(
|
|
133
|
-
* field.text("name", { label: "Name", required: true }),
|
|
134
|
-
* field.number("age", { min: 0 }),
|
|
135
|
-
* );
|
|
136
|
-
*
|
|
137
|
-
* const schema = generateJsonSchema(form);
|
|
138
|
-
* // {
|
|
139
|
-
* // $schema: "https://json-schema.org/draft-07/schema#",
|
|
140
|
-
* // type: "object",
|
|
141
|
-
* // properties: {
|
|
142
|
-
* // name: { type: "string", title: "Name" },
|
|
143
|
-
* // age: { type: "number", minimum: 0 }
|
|
144
|
-
* // },
|
|
145
|
-
* // required: ["name"]
|
|
146
|
-
* // }
|
|
147
|
-
* ```
|
|
148
|
-
*
|
|
149
|
-
* @param form - The FormSpec to convert
|
|
150
|
-
* @returns A JSON Schema object
|
|
151
|
-
*/
|
|
152
|
-
export function generateJsonSchema(form) {
|
|
153
|
-
const properties = {};
|
|
154
|
-
const required = [];
|
|
155
|
-
collectFields(form.elements, properties, required);
|
|
156
|
-
// Deduplicate required array (can have duplicates when the same field is defined
|
|
157
|
-
// in multiple branches/containers, e.g., repeated in different conditional branches)
|
|
158
|
-
const uniqueRequired = [...new Set(required)];
|
|
159
|
-
return {
|
|
160
|
-
$schema: "https://json-schema.org/draft-07/schema#",
|
|
161
|
-
type: "object",
|
|
162
|
-
properties,
|
|
163
|
-
...(uniqueRequired.length > 0 && { required: uniqueRequired }),
|
|
164
|
-
};
|
|
165
|
-
}
|
|
166
|
-
//# sourceMappingURL=generator.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"generator.js","sourceRoot":"","sources":["../../src/json-schema/generator.ts"],"names":[],"mappings":"AAAA;;GAEG;AAaH;;GAEG;AACH,SAAS,oBAAoB,CAAC,QAAgC;IAC5D,MAAM,UAAU,GAAgC,EAAE,CA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E,UAAU,EACV,QAAQ,CACT,CAAC;gBACF,MAAM;QACV,CAAC;IACH,CAAC;AACH,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,UAAU,kBAAkB,CAChC,IAAiB;IAEjB,MAAM,UAAU,GAAgC,EAAE,CAAC;IACnD,MAAM,QAAQ,GAAa,EAAE,CAAC;IAE9B,aAAa,CAAC,IAAI,CAAC,QAAQ,EAAE,UAAU,EAAE,QAAQ,CAAC,CAAC;IAEnD,iFAAiF;IACjF,qFAAqF;IACrF,MAAM,cAAc,GAAG,CAAC,GAAG,IAAI,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC;IAE9C,OAAO;QACL,OAAO,EAAE,0CAA0C;QACnD,IAAI,EAAE,QAAQ;QACd,UAAU;QACV,GAAG,CAAC,cAAc,CAAC,MAAM,GAAG,CAAC,IAAI,EAAE,QAAQ,EAAE,cAAc,EAAE,CAAC;KAC/D,CAAC;AACJ,CAAC"}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* JSON Schema Draft-07 type definitions.
|
|
3
|
-
*
|
|
4
|
-
* These types are a subset of JSON Schema sufficient for form generation.
|
|
5
|
-
*/
|
|
6
|
-
/**
|
|
7
|
-
* Sets a FormSpec extension property on a JSON Schema node.
|
|
8
|
-
*
|
|
9
|
-
* Use this to safely add `x-formspec-*` properties to any schema,
|
|
10
|
-
* including nested schemas typed as `JSONSchema7` (which don't carry
|
|
11
|
-
* the extension index signature).
|
|
12
|
-
*
|
|
13
|
-
* @param schema - Any JSON Schema node
|
|
14
|
-
* @param key - Extension key (must start with `x-formspec-`)
|
|
15
|
-
* @param value - Extension value
|
|
16
|
-
*/
|
|
17
|
-
export function setSchemaExtension(schema, key, value) {
|
|
18
|
-
schema[key] = value;
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* Reads a FormSpec extension property from a JSON Schema node.
|
|
22
|
-
*
|
|
23
|
-
* Use this to safely read `x-formspec-*` properties from any schema,
|
|
24
|
-
* including nested schemas typed as `JSONSchema7`.
|
|
25
|
-
*
|
|
26
|
-
* @param schema - Any JSON Schema node
|
|
27
|
-
* @param key - Extension key (must start with `x-formspec-`)
|
|
28
|
-
* @returns The extension value, or `undefined` if not present
|
|
29
|
-
*/
|
|
30
|
-
export function getSchemaExtension(schema, key) {
|
|
31
|
-
return schema[key];
|
|
32
|
-
}
|
|
33
|
-
//# sourceMappingURL=types.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/json-schema/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAqGH;;;;;;;;;;GAUG;AACH,MAAM,UAAU,kBAAkB,CAChC,MAAmB,EACnB,GAA2B,EAC3B,KAAc;IAEb,MAA8B,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;AAC/C,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,kBAAkB,CAAC,MAAmB,EAAE,GAA2B;IACjF,OAAQ,MAA8B,CAAC,GAAG,CAAC,CAAC;AAC9C,CAAC"}
|
|
@@ -1,148 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* JSON Forms UI Schema generator for FormSpec forms.
|
|
3
|
-
*/
|
|
4
|
-
/**
|
|
5
|
-
* Converts a field name to a JSON Pointer scope.
|
|
6
|
-
*/
|
|
7
|
-
function fieldToScope(fieldName) {
|
|
8
|
-
return `#/properties/${fieldName}`;
|
|
9
|
-
}
|
|
10
|
-
/**
|
|
11
|
-
* Creates a rule for conditional visibility.
|
|
12
|
-
*/
|
|
13
|
-
function createShowRule(fieldName, value) {
|
|
14
|
-
return {
|
|
15
|
-
effect: "SHOW",
|
|
16
|
-
condition: {
|
|
17
|
-
scope: fieldToScope(fieldName),
|
|
18
|
-
schema: { const: value },
|
|
19
|
-
},
|
|
20
|
-
};
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Combines two rules into one using allOf.
|
|
24
|
-
*
|
|
25
|
-
* When elements are nested inside multiple conditionals, all conditions
|
|
26
|
-
* must be met for the element to be visible.
|
|
27
|
-
*/
|
|
28
|
-
function combineRules(parentRule, childRule) {
|
|
29
|
-
// Both rules should have the same effect (SHOW)
|
|
30
|
-
// Combine conditions using allOf
|
|
31
|
-
const parentCondition = parentRule.condition;
|
|
32
|
-
const childCondition = childRule.condition;
|
|
33
|
-
return {
|
|
34
|
-
effect: "SHOW",
|
|
35
|
-
condition: {
|
|
36
|
-
scope: "#",
|
|
37
|
-
schema: {
|
|
38
|
-
allOf: [
|
|
39
|
-
{
|
|
40
|
-
properties: {
|
|
41
|
-
[parentCondition.scope.replace("#/properties/", "")]: parentCondition.schema,
|
|
42
|
-
},
|
|
43
|
-
},
|
|
44
|
-
{
|
|
45
|
-
properties: {
|
|
46
|
-
[childCondition.scope.replace("#/properties/", "")]: childCondition.schema,
|
|
47
|
-
},
|
|
48
|
-
},
|
|
49
|
-
],
|
|
50
|
-
},
|
|
51
|
-
},
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* Converts form elements to UI Schema elements.
|
|
56
|
-
*
|
|
57
|
-
* @param elements - The form elements to convert
|
|
58
|
-
* @param parentRule - Optional rule inherited from parent conditional
|
|
59
|
-
* @returns Array of UI Schema elements
|
|
60
|
-
*/
|
|
61
|
-
function elementsToUiSchema(elements, parentRule) {
|
|
62
|
-
const result = [];
|
|
63
|
-
for (const element of elements) {
|
|
64
|
-
switch (element._type) {
|
|
65
|
-
case "field": {
|
|
66
|
-
const control = {
|
|
67
|
-
type: "Control",
|
|
68
|
-
scope: fieldToScope(element.name),
|
|
69
|
-
...(element.label !== undefined && { label: element.label }),
|
|
70
|
-
...(parentRule !== undefined && { rule: parentRule }),
|
|
71
|
-
};
|
|
72
|
-
result.push(control);
|
|
73
|
-
break;
|
|
74
|
-
}
|
|
75
|
-
case "group": {
|
|
76
|
-
const groupElement = element;
|
|
77
|
-
const group = {
|
|
78
|
-
type: "Group",
|
|
79
|
-
label: groupElement.label,
|
|
80
|
-
elements: elementsToUiSchema(groupElement.elements, parentRule),
|
|
81
|
-
...(parentRule !== undefined && { rule: parentRule }),
|
|
82
|
-
};
|
|
83
|
-
result.push(group);
|
|
84
|
-
break;
|
|
85
|
-
}
|
|
86
|
-
case "conditional": {
|
|
87
|
-
const conditionalElement = element;
|
|
88
|
-
// Create a rule for this conditional
|
|
89
|
-
const newRule = createShowRule(conditionalElement.field, conditionalElement.value);
|
|
90
|
-
// Combine with parent rule if present (for nested conditionals)
|
|
91
|
-
const combinedRule = parentRule !== undefined ? combineRules(parentRule, newRule) : newRule;
|
|
92
|
-
// Apply the combined rule to all children
|
|
93
|
-
const childElements = elementsToUiSchema(conditionalElement.elements, combinedRule);
|
|
94
|
-
result.push(...childElements);
|
|
95
|
-
break;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
return result;
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* Generates a JSON Forms UI Schema from a FormSpec.
|
|
103
|
-
*
|
|
104
|
-
* @example
|
|
105
|
-
* ```typescript
|
|
106
|
-
* const form = formspec(
|
|
107
|
-
* group("Customer",
|
|
108
|
-
* field.text("name", { label: "Name" }),
|
|
109
|
-
* ),
|
|
110
|
-
* when("status", "draft",
|
|
111
|
-
* field.text("notes", { label: "Notes" }),
|
|
112
|
-
* ),
|
|
113
|
-
* );
|
|
114
|
-
*
|
|
115
|
-
* const uiSchema = generateUiSchema(form);
|
|
116
|
-
* // {
|
|
117
|
-
* // type: "VerticalLayout",
|
|
118
|
-
* // elements: [
|
|
119
|
-
* // {
|
|
120
|
-
* // type: "Group",
|
|
121
|
-
* // label: "Customer",
|
|
122
|
-
* // elements: [
|
|
123
|
-
* // { type: "Control", scope: "#/properties/name", label: "Name" }
|
|
124
|
-
* // ]
|
|
125
|
-
* // },
|
|
126
|
-
* // {
|
|
127
|
-
* // type: "Control",
|
|
128
|
-
* // scope: "#/properties/notes",
|
|
129
|
-
* // label: "Notes",
|
|
130
|
-
* // rule: {
|
|
131
|
-
* // effect: "SHOW",
|
|
132
|
-
* // condition: { scope: "#/properties/status", schema: { const: "draft" } }
|
|
133
|
-
* // }
|
|
134
|
-
* // }
|
|
135
|
-
* // ]
|
|
136
|
-
* // }
|
|
137
|
-
* ```
|
|
138
|
-
*
|
|
139
|
-
* @param form - The FormSpec to convert
|
|
140
|
-
* @returns A JSON Forms UI Schema
|
|
141
|
-
*/
|
|
142
|
-
export function generateUiSchema(form) {
|
|
143
|
-
return {
|
|
144
|
-
type: "VerticalLayout",
|
|
145
|
-
elements: elementsToUiSchema(form.elements),
|
|
146
|
-
};
|
|
147
|
-
}
|
|
148
|
-
//# sourceMappingURL=generator.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"generator.js","sourceRoot":"","sources":["../../src/ui-schema/generator.ts"],"names":[],"mappings":"AAAA;;GAEG;AAKH;;GAEG;AACH,SAAS,YAAY,CAAC,SAAiB;IACrC,OAAO,gBAAgB,SAAS,EAAE,CAAC;AACrC,CAAC;AAED;;GAEG;AACH,SAAS,cAAc,CAAC,SAAiB,EAAE,KAAc;IACvD,OAAO;QACL,MAAM,EAAE,MAAM;QACd,SAAS,EAAE;YACT,KAAK,EAAE,YAAY,CAAC,SAAS,CAAC;YAC9B,MAAM,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE;SACzB;KACF,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,SAAS,YAAY,CAAC,UAAgB,EAAE,SAAe;IACrD,gDAAgD;IAChD,iCAAiC;IACjC,MAAM,eAAe,GAAG,UAAU,CAAC,SAAS,CAAC;IAC7C,MAAM,cAAc,GAAG,SAAS,CAAC,SAAS,CAAC;IAE3C,OAAO;QACL,MAAM,EAAE,MAAM;QACd,SAAS,EAAE;YACT,KAAK,EAAE,GAAG;YACV,MAAM,EAAE;gBACN,KAAK,EAAE;oBACL;wBACE,UAAU,EAAE;4BACV,C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