-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjson.ts
More file actions
195 lines (179 loc) · 8.43 KB
/
json.ts
File metadata and controls
195 lines (179 loc) · 8.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import type { JSONSchema7 } from 'json-schema';
import { noEmpty, isObject } from './utils';
import type { FormanSchemaField, FormanSchemaFieldType, FormanSchemaValue } from './types';
import { EMPTY_OPTION_DESCRIPTION } from './forman';
import { udttypeCollapse } from './composites/udttype';
import { udtspecCollapse } from './composites/udtspec';
/**
* Maps JSON Schema primitive types to Forman Schema types.
*/
const JSON_PRIMITIVE_TYPE_MAP: Readonly<Record<string, FormanSchemaFieldType>> = {
string: 'text',
number: 'number',
integer: 'number',
boolean: 'boolean',
} as const;
type XSearchDirective = {
url: string;
label?: string;
inputSchema: JSONSchema7;
};
/**
* Converts a JSON Schema field to its Forman Schema equivalent.
* @param field The JSON Schema field to convert
* @returns The equivalent Forman Schema field
*/
export function toFormanSchema(field: JSONSchema7): FormanSchemaField {
const compositeType = Object.getOwnPropertyDescriptor(field, 'x-composite')?.value;
if (compositeType === 'udttype') return udttypeCollapse(field);
if (compositeType === 'udtspec') return udtspecCollapse(field);
switch (field.type) {
case 'object':
// When there are no defined properties in the spec, assume the collection is dynamic.
if (!field.properties || !Object.entries(field.properties).length) {
return {
type: 'dynamicCollection',
label: noEmpty(field.title),
help: noEmpty(field.description),
};
}
// For objects, create a collection type with spec from properties
const spec: FormanSchemaField[] = Object.entries(field.properties)
.filter(([name, property]) => !!property)
.map(([name, property]) => {
// Convert each property to a Forman Schema field
const subField = toFormanSchema(property as JSONSchema7);
subField.name = name;
subField.required = field.required?.includes(name) || false;
return subField;
});
return {
type: 'collection',
label: noEmpty(field.title),
help: noEmpty(field.description),
spec,
};
case 'array':
// If the array is flagged as filter field root, then short-circuit to it
const filterLogic = Object.getOwnPropertyDescriptor(field, 'x-filter');
if (filterLogic) {
return {
type: 'filter',
label: noEmpty(field.title),
help: noEmpty(field.description),
logic: filterLogic.value === 'default' ? undefined : filterLogic.value,
};
}
// For arrays, create an array type with spec from items
const items = field.items && isObject<JSONSchema7>(field.items) ? field.items : undefined;
const formanSchema: FormanSchemaField = {
type: 'array',
label: noEmpty(field.title),
help: noEmpty(field.description),
spec: items
? items.type === 'object' && items.properties
? Object.entries(items.properties).map(([name, property]) => {
// If items is an object, convert its properties to Forman Schema fields
const subField = toFormanSchema(property as JSONSchema7);
subField.name = name;
subField.required = items.required?.includes(name) || false;
return subField;
})
: toFormanSchema(items)
: undefined,
};
// Add validation if present
if (field.minItems !== undefined || field.maxItems !== undefined) {
formanSchema.validate = formanSchema.validate || {};
if (field.minItems !== undefined) formanSchema.validate.minItems = field.minItems;
if (field.maxItems !== undefined) formanSchema.validate.maxItems = field.maxItems;
}
return formanSchema;
case 'string':
// If the field is flagged as path selector field root, then short-circuit to it
const pathInfo = Object.getOwnPropertyDescriptor(field, 'x-path');
if (pathInfo) {
const help = field.description === EMPTY_OPTION_DESCRIPTION ? undefined : noEmpty(field.description);
return {
type: pathInfo.value.type,
label: noEmpty(field.title),
help,
options: {
store: Object.getOwnPropertyDescriptor(field, 'x-fetch')?.value,
showRoot: pathInfo.value.showRoot,
singleLevel: pathInfo.value.singleLevel,
},
};
}
if (field.enum) {
// For strings with enum, create a select type
const help = field.description === EMPTY_OPTION_DESCRIPTION ? undefined : noEmpty(field.description);
return {
type: 'select',
label: noEmpty(field.title),
help,
options: field.enum
.filter(value => value !== '')
.map(value => ({ value: value as FormanSchemaValue })),
};
} else if (field.oneOf) {
// For strings with oneOf, create a select type
const help = field.description === EMPTY_OPTION_DESCRIPTION ? undefined : noEmpty(field.description);
return {
type: 'select',
label: noEmpty(field.title),
help,
options: field.oneOf
.filter(value => value && (value as JSONSchema7).const !== '')
.map(value => ({ value: (value as JSONSchema7).const as FormanSchemaValue })),
};
}
// For regular strings, create a text type
let textField: FormanSchemaField = {
type: 'text',
label: noEmpty(field.title),
help: noEmpty(field.description),
default: field.default as FormanSchemaValue,
};
// Add validation if present
if (field.pattern || field.enum) {
textField.validate = textField.validate || {};
if (field.pattern) textField.validate.pattern = field.pattern;
if (field.enum) textField.validate.enum = field.enum;
}
if ('x-search' in field) {
textField = handleSearchDirective(textField, field['x-search'] as XSearchDirective);
}
return textField;
default:
// For primitive types, use the type mapping
let primitiveField: FormanSchemaField = {
type: (JSON_PRIMITIVE_TYPE_MAP[field.type as keyof typeof JSON_PRIMITIVE_TYPE_MAP] ||
'any') as FormanSchemaFieldType,
label: noEmpty(field.title),
help: noEmpty(field.description),
default: field.default as FormanSchemaValue,
};
// Add validation if present
if (field.minimum !== undefined || field.maximum !== undefined) {
primitiveField.validate = primitiveField.validate || {};
if (field.minimum !== undefined) primitiveField.validate.min = field.minimum;
if (field.maximum !== undefined) primitiveField.validate.max = field.maximum;
}
if ('x-search' in field) {
primitiveField = handleSearchDirective(primitiveField, field['x-search'] as XSearchDirective);
}
return primitiveField;
}
}
function handleSearchDirective(formanField: FormanSchemaField, directive: XSearchDirective): FormanSchemaField {
formanField.rpc = {
url: directive.url,
label: directive.label,
parameters:
typeof directive.inputSchema.$ref === 'string'
? directive.inputSchema.$ref
: (toFormanSchema(directive.inputSchema).spec as FormanSchemaField[]),
};
return formanField;
}