@react-typed-forms/schemas 14.2.0 → 14.3.0
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/lib/controlDefinition.d.ts +11 -0
- package/lib/index.cjs +3392 -1
- package/lib/index.cjs.map +1 -1
- package/lib/index.js +2869 -1
- package/lib/index.js.map +1 -1
- package/lib/renderers.d.ts +2 -2
- package/lib/util.d.ts +7 -2
- package/package.json +3 -2
- package/src/controlBuilder.ts +268 -0
- package/src/controlDefinition.ts +792 -0
- package/src/controlRender.tsx +1252 -0
- package/src/createFormRenderer.tsx +218 -0
- package/src/defaultSchemaInterface.ts +191 -0
- package/src/dynamicHooks.ts +98 -0
- package/src/entityExpression.ts +38 -0
- package/src/hooks.tsx +459 -0
- package/src/index.ts +14 -0
- package/src/renderers.tsx +205 -0
- package/src/schemaBuilder.ts +318 -0
- package/src/schemaField.ts +552 -0
- package/src/schemaValidator.ts +32 -0
- package/src/util.ts +1039 -0
- package/src/validators.ts +217 -0
|
@@ -0,0 +1,552 @@
|
|
|
1
|
+
import { SchemaValidator } from "./schemaValidator";
|
|
2
|
+
import { Control, ControlSetup, newControl } from "@react-typed-forms/core";
|
|
3
|
+
import { EntityExpression } from "./entityExpression";
|
|
4
|
+
|
|
5
|
+
export type EqualityFunc = (a: any, b: any) => boolean;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Represents a schema field with various properties.
|
|
9
|
+
*/
|
|
10
|
+
export interface SchemaField {
|
|
11
|
+
/** The type of the field. */
|
|
12
|
+
type: string;
|
|
13
|
+
/** The name of the field. */
|
|
14
|
+
field: string;
|
|
15
|
+
/** The display name of the field, optional. */
|
|
16
|
+
displayName?: string | null;
|
|
17
|
+
/** Tags associated with the field, optional. */
|
|
18
|
+
tags?: string[] | null;
|
|
19
|
+
/** Indicates if the field is a system field, optional. */
|
|
20
|
+
system?: boolean | null;
|
|
21
|
+
/** Indicates if the field is a collection, optional. */
|
|
22
|
+
collection?: boolean | null;
|
|
23
|
+
/** Specifies the types for which the field is applicable, optional. */
|
|
24
|
+
onlyForTypes?: string[] | null;
|
|
25
|
+
/** Indicates if the field is required, optional. */
|
|
26
|
+
required?: boolean | null;
|
|
27
|
+
/** Indicates if the field is not nullable, optional. */
|
|
28
|
+
notNullable?: boolean | null;
|
|
29
|
+
/** The default value of the field, optional. */
|
|
30
|
+
defaultValue?: any;
|
|
31
|
+
/** Indicates if the field is a type field, optional. */
|
|
32
|
+
isTypeField?: boolean | null;
|
|
33
|
+
/** Indicates if the field is searchable, optional. */
|
|
34
|
+
searchable?: boolean | null;
|
|
35
|
+
/** Options for the field, optional. */
|
|
36
|
+
options?: FieldOption[] | null;
|
|
37
|
+
/** Validators for the field, optional. */
|
|
38
|
+
validators?: SchemaValidator[] | null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Represents a map of schema fields.
|
|
43
|
+
* The key is a string representing the schema name.
|
|
44
|
+
* The value is an array of SchemaField objects.
|
|
45
|
+
*/
|
|
46
|
+
export type SchemaMap = Record<string, SchemaField[]>;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Enum representing the various field types.
|
|
50
|
+
*/
|
|
51
|
+
export enum FieldType {
|
|
52
|
+
String = "String",
|
|
53
|
+
Bool = "Bool",
|
|
54
|
+
Int = "Int",
|
|
55
|
+
Date = "Date",
|
|
56
|
+
DateTime = "DateTime",
|
|
57
|
+
Time = "Time",
|
|
58
|
+
Double = "Double",
|
|
59
|
+
EntityRef = "EntityRef",
|
|
60
|
+
Compound = "Compound",
|
|
61
|
+
AutoId = "AutoId",
|
|
62
|
+
Image = "Image",
|
|
63
|
+
Any = "Any",
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Represents a field that references an entity.
|
|
68
|
+
*/
|
|
69
|
+
export interface EntityRefField extends SchemaField {
|
|
70
|
+
/** The type of the field, which is EntityRef. */
|
|
71
|
+
type: FieldType.EntityRef;
|
|
72
|
+
/** The type of the referenced entity. */
|
|
73
|
+
entityRefType: string;
|
|
74
|
+
/** The parent field of the entity reference. */
|
|
75
|
+
parentField: string;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Represents an option for a field.
|
|
80
|
+
*/
|
|
81
|
+
export interface FieldOption {
|
|
82
|
+
/** The name of the option. */
|
|
83
|
+
name: string;
|
|
84
|
+
/** The value of the option. */
|
|
85
|
+
value: any;
|
|
86
|
+
/** The description of the option, optional. */
|
|
87
|
+
description?: string | null;
|
|
88
|
+
/** The group of the option, optional. */
|
|
89
|
+
group?: string | null;
|
|
90
|
+
/** Indicates if the option is disabled, optional. */
|
|
91
|
+
disabled?: boolean | null;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Represents a compound field that contains child fields.
|
|
96
|
+
*/
|
|
97
|
+
export interface CompoundField extends SchemaField {
|
|
98
|
+
/** The type of the field, which is Compound. */
|
|
99
|
+
type: FieldType.Compound;
|
|
100
|
+
/** The child fields of the compound field. */
|
|
101
|
+
children: SchemaField[];
|
|
102
|
+
/** Indicates if the children are tree-structured, optional. */
|
|
103
|
+
treeChildren?: boolean;
|
|
104
|
+
/** The schema reference for the compound field, optional. */
|
|
105
|
+
schemaRef?: string;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Enum representing the various validation message types.
|
|
110
|
+
*/
|
|
111
|
+
export enum ValidationMessageType {
|
|
112
|
+
NotEmpty = "NotEmpty",
|
|
113
|
+
MinLength = "MinLength",
|
|
114
|
+
MaxLength = "MaxLength",
|
|
115
|
+
NotAfterDate = "NotAfterDate",
|
|
116
|
+
NotBeforeDate = "NotBeforeDate",
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Interface for schema-related operations.
|
|
121
|
+
*/
|
|
122
|
+
export interface SchemaInterface {
|
|
123
|
+
/**
|
|
124
|
+
* Checks if the value of a field is empty.
|
|
125
|
+
* @param field The schema field.
|
|
126
|
+
* @param value The value to check.
|
|
127
|
+
* @returns True if the value is empty, false otherwise.
|
|
128
|
+
*/
|
|
129
|
+
isEmptyValue(field: SchemaField, value: any): boolean;
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Gets the text representation of a field's value.
|
|
133
|
+
* @param field The schema field.
|
|
134
|
+
* @param value The value to convert.
|
|
135
|
+
* @param element Indicates if the value is an element, optional.
|
|
136
|
+
* @returns The text representation of the value.
|
|
137
|
+
*/
|
|
138
|
+
textValue(
|
|
139
|
+
field: SchemaField,
|
|
140
|
+
value: any,
|
|
141
|
+
element?: boolean,
|
|
142
|
+
): string | undefined;
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Gets the length of a control's value.
|
|
146
|
+
* @param field The schema field.
|
|
147
|
+
* @param control The control to check.
|
|
148
|
+
* @returns The length of the control's value.
|
|
149
|
+
*/
|
|
150
|
+
controlLength(field: SchemaField, control: Control<any>): number;
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Gets the length of a field's value.
|
|
154
|
+
* @param field The schema field.
|
|
155
|
+
* @param value The value to check.
|
|
156
|
+
* @returns The length of the value.
|
|
157
|
+
*/
|
|
158
|
+
valueLength(field: SchemaField, value: any): number;
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Gets the data options for a schema data node.
|
|
162
|
+
* @param node The schema data node.
|
|
163
|
+
* @returns The data options.
|
|
164
|
+
*/
|
|
165
|
+
getDataOptions(node: SchemaDataNode): FieldOption[] | null | undefined;
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Gets the node options for a schema node.
|
|
169
|
+
* @param node The schema node.
|
|
170
|
+
* @returns The node options.
|
|
171
|
+
*/
|
|
172
|
+
getNodeOptions(node: SchemaNode): FieldOption[] | null | undefined;
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Gets the options for a schema field.
|
|
176
|
+
* @param field The schema field.
|
|
177
|
+
* @returns The field options.
|
|
178
|
+
*/
|
|
179
|
+
getOptions(field: SchemaField): FieldOption[] | undefined | null;
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Gets the filter options for a schema data node and field.
|
|
183
|
+
* @param array The schema data node.
|
|
184
|
+
* @param field The schema node.
|
|
185
|
+
* @returns The filter options.
|
|
186
|
+
*/
|
|
187
|
+
getFilterOptions(
|
|
188
|
+
array: SchemaDataNode,
|
|
189
|
+
field: SchemaNode,
|
|
190
|
+
): FieldOption[] | undefined | null;
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Parses a string value to milliseconds.
|
|
194
|
+
* @param field The schema field.
|
|
195
|
+
* @param v The string value to parse.
|
|
196
|
+
* @returns The parsed value in milliseconds.
|
|
197
|
+
*/
|
|
198
|
+
parseToMillis(field: SchemaField, v: string): number;
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Gets the validation message text for a field.
|
|
202
|
+
* @param field The schema field.
|
|
203
|
+
* @param messageType The type of validation message.
|
|
204
|
+
* @param actual The actual value.
|
|
205
|
+
* @param expected The expected value.
|
|
206
|
+
* @returns The validation message text.
|
|
207
|
+
*/
|
|
208
|
+
validationMessageText(
|
|
209
|
+
field: SchemaField,
|
|
210
|
+
messageType: ValidationMessageType,
|
|
211
|
+
actual: any,
|
|
212
|
+
expected: any,
|
|
213
|
+
): string;
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Compares two values of a field.
|
|
217
|
+
* @param field The schema field.
|
|
218
|
+
* @param v1 The first value.
|
|
219
|
+
* @param v2 The second value.
|
|
220
|
+
* @returns The comparison result.
|
|
221
|
+
*/
|
|
222
|
+
compareValue(field: SchemaField, v1: unknown, v2: unknown): number;
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Gets the search text for a field's value.
|
|
226
|
+
* @param field The schema field.
|
|
227
|
+
* @param value The value to search.
|
|
228
|
+
* @returns The search text.
|
|
229
|
+
*/
|
|
230
|
+
searchText(field: SchemaField, value: any): string;
|
|
231
|
+
|
|
232
|
+
makeEqualityFunc(field: SchemaNode, element?: boolean): EqualityFunc;
|
|
233
|
+
|
|
234
|
+
makeControlSetup(field: SchemaNode, element?: boolean): ControlSetup<any>;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
export interface SchemaTreeLookup {
|
|
238
|
+
getSchema(schemaId: string): SchemaNode | undefined;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export interface SchemaNode extends SchemaTreeLookup {
|
|
242
|
+
id: string;
|
|
243
|
+
field: SchemaField;
|
|
244
|
+
getChildNode(field: string): SchemaNode | undefined;
|
|
245
|
+
getChildNodes(noRecurse?: boolean, withParent?: SchemaNode): SchemaNode[];
|
|
246
|
+
parent?: SchemaNode;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
export interface SchemaDataNode {
|
|
250
|
+
id: string;
|
|
251
|
+
schema: SchemaNode;
|
|
252
|
+
elementIndex?: number;
|
|
253
|
+
control: Control<any>;
|
|
254
|
+
parent?: SchemaDataNode;
|
|
255
|
+
getChild(schemaNode: SchemaNode): SchemaDataNode;
|
|
256
|
+
getChildElement(index: number): SchemaDataNode;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
export function findField(
|
|
260
|
+
fields: SchemaField[],
|
|
261
|
+
field: string,
|
|
262
|
+
): SchemaField | undefined {
|
|
263
|
+
return fields.find((x) => x.field === field);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
export function isScalarField(sf: SchemaField): sf is SchemaField {
|
|
267
|
+
return !isCompoundField(sf);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
export function isCompoundField(sf: SchemaField): sf is CompoundField {
|
|
271
|
+
return sf.type === FieldType.Compound;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function missingField(field: string): SchemaField {
|
|
275
|
+
return { field: "__missing", type: FieldType.Any, displayName: field };
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function nodeForSchema(
|
|
279
|
+
field: SchemaField,
|
|
280
|
+
lookup: SchemaTreeLookup,
|
|
281
|
+
parent: SchemaNode | undefined,
|
|
282
|
+
): SchemaNode {
|
|
283
|
+
const node = {
|
|
284
|
+
id: parent ? parent.id + "/" + field.field : field.field,
|
|
285
|
+
field,
|
|
286
|
+
getSchema: lookup.getSchema,
|
|
287
|
+
parent,
|
|
288
|
+
getChildNode,
|
|
289
|
+
getChildNodes,
|
|
290
|
+
};
|
|
291
|
+
return node;
|
|
292
|
+
|
|
293
|
+
function getChildNode(fieldName: string) {
|
|
294
|
+
if (isCompoundField(field) && !field.schemaRef && !field.treeChildren) {
|
|
295
|
+
const childField = field.children.find((x) => x.field === fieldName);
|
|
296
|
+
return childField ? nodeForSchema(childField, lookup, node) : undefined;
|
|
297
|
+
}
|
|
298
|
+
return getChildNodes(false, node).find((x) => x.field.field === fieldName);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
function getChildNodes(
|
|
302
|
+
noRecurse?: boolean,
|
|
303
|
+
withParent?: SchemaNode,
|
|
304
|
+
): SchemaNode[] {
|
|
305
|
+
if (isCompoundField(field)) {
|
|
306
|
+
if (field.treeChildren) {
|
|
307
|
+
return noRecurse
|
|
308
|
+
? []
|
|
309
|
+
: parent!.getChildNodes(false, withParent ?? node);
|
|
310
|
+
}
|
|
311
|
+
const otherRef = field.schemaRef && lookup.getSchema(field.schemaRef);
|
|
312
|
+
if (otherRef) return otherRef.getChildNodes(false, withParent ?? node);
|
|
313
|
+
return field.children.map((x) =>
|
|
314
|
+
nodeForSchema(x, lookup, withParent ?? node),
|
|
315
|
+
);
|
|
316
|
+
}
|
|
317
|
+
return [];
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
export function createSchemaLookup<A extends Record<string, SchemaField[]>>(
|
|
322
|
+
schemaMap: A,
|
|
323
|
+
): {
|
|
324
|
+
getSchema(schemaId: keyof A): SchemaNode;
|
|
325
|
+
} {
|
|
326
|
+
const lookup = {
|
|
327
|
+
getSchema,
|
|
328
|
+
};
|
|
329
|
+
return lookup;
|
|
330
|
+
|
|
331
|
+
function getSchema(schemaId: keyof A): SchemaNode {
|
|
332
|
+
const fields = schemaMap[schemaId];
|
|
333
|
+
if (fields) {
|
|
334
|
+
return rootSchemaNode(fields, lookup);
|
|
335
|
+
}
|
|
336
|
+
return undefined!;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
export function makeSchemaDataNode(
|
|
341
|
+
schema: SchemaNode,
|
|
342
|
+
control: Control<unknown>,
|
|
343
|
+
parent?: SchemaDataNode,
|
|
344
|
+
elementIndex?: number,
|
|
345
|
+
): SchemaDataNode {
|
|
346
|
+
const indexId = typeof elementIndex === "number" ? "/" + elementIndex : "";
|
|
347
|
+
const dataNode = {
|
|
348
|
+
id:
|
|
349
|
+
(parent ? parent.id + "/" + schema.field.field : schema.field.field) +
|
|
350
|
+
indexId,
|
|
351
|
+
schema,
|
|
352
|
+
control,
|
|
353
|
+
parent,
|
|
354
|
+
elementIndex,
|
|
355
|
+
getChild,
|
|
356
|
+
getChildElement,
|
|
357
|
+
};
|
|
358
|
+
return dataNode;
|
|
359
|
+
|
|
360
|
+
function getChild(childNode: SchemaNode): SchemaDataNode {
|
|
361
|
+
const objControl = control as Control<Record<string, unknown>>;
|
|
362
|
+
if (objControl && objControl.current.isNull) {
|
|
363
|
+
objControl.value = {};
|
|
364
|
+
}
|
|
365
|
+
return makeSchemaDataNode(
|
|
366
|
+
childNode,
|
|
367
|
+
objControl?.fields[childNode.field.field],
|
|
368
|
+
dataNode,
|
|
369
|
+
);
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
function getChildElement(elementIndex: number): SchemaDataNode {
|
|
373
|
+
return makeSchemaDataNode(
|
|
374
|
+
schema,
|
|
375
|
+
(control as Control<unknown[]>)?.elements?.[elementIndex],
|
|
376
|
+
dataNode,
|
|
377
|
+
elementIndex,
|
|
378
|
+
);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
export function schemaDataForFieldRef(
|
|
383
|
+
fieldRef: string | undefined,
|
|
384
|
+
schema: SchemaDataNode,
|
|
385
|
+
): SchemaDataNode {
|
|
386
|
+
return schemaDataForFieldPath(fieldRef?.split("/") ?? [], schema);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
export function schemaForFieldRef(
|
|
390
|
+
fieldRef: string | undefined,
|
|
391
|
+
schema: SchemaNode,
|
|
392
|
+
): SchemaNode {
|
|
393
|
+
return schemaForFieldPath(fieldRef?.split("/") ?? [], schema);
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
export function traverseSchemaPath<A>(
|
|
397
|
+
fieldPath: string[],
|
|
398
|
+
schema: SchemaNode,
|
|
399
|
+
acc: A,
|
|
400
|
+
next: (acc: A, node: SchemaNode) => A,
|
|
401
|
+
): A {
|
|
402
|
+
let i = 0;
|
|
403
|
+
while (i < fieldPath.length) {
|
|
404
|
+
const nextField = fieldPath[i];
|
|
405
|
+
let childNode =
|
|
406
|
+
nextField === ".." ? schema.parent : schema.getChildNode(nextField);
|
|
407
|
+
if (!childNode) {
|
|
408
|
+
childNode = nodeForSchema(missingField(nextField), schema, schema);
|
|
409
|
+
}
|
|
410
|
+
acc = next(acc, childNode);
|
|
411
|
+
schema = childNode;
|
|
412
|
+
i++;
|
|
413
|
+
}
|
|
414
|
+
return acc;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
export function traverseData(
|
|
418
|
+
fieldPath: string[],
|
|
419
|
+
root: SchemaNode,
|
|
420
|
+
data: { [k: string]: any },
|
|
421
|
+
): unknown {
|
|
422
|
+
return traverseSchemaPath(
|
|
423
|
+
fieldPath,
|
|
424
|
+
root,
|
|
425
|
+
data,
|
|
426
|
+
(acc, n) => acc?.[n.field.field] as any,
|
|
427
|
+
);
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
export function schemaDataForFieldPath(
|
|
431
|
+
fieldPath: string[],
|
|
432
|
+
dataNode: SchemaDataNode,
|
|
433
|
+
): SchemaDataNode {
|
|
434
|
+
let i = 0;
|
|
435
|
+
while (i < fieldPath.length) {
|
|
436
|
+
const nextField = fieldPath[i];
|
|
437
|
+
let nextNode =
|
|
438
|
+
nextField === ".." ? dataNode.parent : lookupField(nextField);
|
|
439
|
+
nextNode ??= makeSchemaDataNode(
|
|
440
|
+
nodeForSchema(missingField(nextField), dataNode.schema, dataNode.schema),
|
|
441
|
+
newControl(undefined),
|
|
442
|
+
);
|
|
443
|
+
dataNode = nextNode;
|
|
444
|
+
i++;
|
|
445
|
+
}
|
|
446
|
+
return dataNode;
|
|
447
|
+
|
|
448
|
+
function lookupField(field: string): SchemaDataNode | undefined {
|
|
449
|
+
const childNode = dataNode.schema.getChildNode(field);
|
|
450
|
+
if (childNode) {
|
|
451
|
+
return dataNode.getChild(childNode);
|
|
452
|
+
}
|
|
453
|
+
return undefined;
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
export function schemaForFieldPath(
|
|
458
|
+
fieldPath: string[],
|
|
459
|
+
schema: SchemaNode,
|
|
460
|
+
): SchemaNode {
|
|
461
|
+
let i = 0;
|
|
462
|
+
while (i < fieldPath.length) {
|
|
463
|
+
const nextField = fieldPath[i];
|
|
464
|
+
let childNode =
|
|
465
|
+
nextField === ".." ? schema.parent : schema.getChildNode(nextField);
|
|
466
|
+
if (!childNode) {
|
|
467
|
+
childNode = nodeForSchema(missingField(nextField), schema, schema);
|
|
468
|
+
}
|
|
469
|
+
schema = childNode;
|
|
470
|
+
i++;
|
|
471
|
+
}
|
|
472
|
+
return schema;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
export function rootSchemaNode(
|
|
476
|
+
fields: SchemaField[],
|
|
477
|
+
lookup: SchemaTreeLookup = {
|
|
478
|
+
getSchema(schemaId: string): SchemaNode | undefined {
|
|
479
|
+
return undefined;
|
|
480
|
+
},
|
|
481
|
+
},
|
|
482
|
+
): SchemaNode {
|
|
483
|
+
return nodeForSchema(
|
|
484
|
+
{
|
|
485
|
+
type: FieldType.Compound,
|
|
486
|
+
field: "",
|
|
487
|
+
children: fields,
|
|
488
|
+
} as CompoundField,
|
|
489
|
+
lookup,
|
|
490
|
+
undefined,
|
|
491
|
+
);
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
export function getSchemaNodePath(node: SchemaNode) {
|
|
495
|
+
const paths: string[] = [];
|
|
496
|
+
let curNode: SchemaNode | undefined = node;
|
|
497
|
+
while (curNode) {
|
|
498
|
+
paths.push(curNode.field.field);
|
|
499
|
+
curNode = curNode.parent;
|
|
500
|
+
}
|
|
501
|
+
return paths.reverse();
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
export function isCompoundNode(node: SchemaNode) {
|
|
505
|
+
return isCompoundField(node.field);
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
/**
|
|
509
|
+
* Returns the relative path from a parent node to a child node.
|
|
510
|
+
* @param parent
|
|
511
|
+
* @param child
|
|
512
|
+
*/
|
|
513
|
+
export function relativePath(parent: SchemaNode, child: SchemaNode): string {
|
|
514
|
+
// return the path from child to parent
|
|
515
|
+
if (parent.id === child.id) return "";
|
|
516
|
+
|
|
517
|
+
const parentPath = getSchemaNodePath(parent);
|
|
518
|
+
const childPath = getSchemaNodePath(child);
|
|
519
|
+
|
|
520
|
+
let i = 0;
|
|
521
|
+
while (
|
|
522
|
+
i < parentPath.length &&
|
|
523
|
+
i < childPath.length &&
|
|
524
|
+
parentPath[i] === childPath[i]
|
|
525
|
+
) {
|
|
526
|
+
i++;
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
const upLevels = parentPath.length - i;
|
|
530
|
+
const downPath = childPath.slice(i).join("/");
|
|
531
|
+
|
|
532
|
+
return "../".repeat(upLevels) + downPath;
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
export enum SchemaTags {
|
|
536
|
+
NoControl = "_NoControl",
|
|
537
|
+
HtmlEditor = "_HtmlEditor",
|
|
538
|
+
ControlGroup = "_ControlGroup:",
|
|
539
|
+
ControlRef = "_ControlRef:",
|
|
540
|
+
IdField = "_IdField:",
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
export function getTagParam(
|
|
544
|
+
field: SchemaField,
|
|
545
|
+
tag: string,
|
|
546
|
+
): string | undefined {
|
|
547
|
+
return field.tags?.find((x) => x.startsWith(tag))?.substring(tag.length);
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
export function makeParamTag(tag: string, value: string): string {
|
|
551
|
+
return `${tag}${value}`;
|
|
552
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export enum ValidatorType {
|
|
2
|
+
Jsonata = "Jsonata",
|
|
3
|
+
Date = "Date",
|
|
4
|
+
Length = "Length",
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export interface SchemaValidator {
|
|
8
|
+
type: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface JsonataValidator extends SchemaValidator {
|
|
12
|
+
type: ValidatorType.Jsonata;
|
|
13
|
+
expression: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface LengthValidator extends SchemaValidator {
|
|
17
|
+
type: ValidatorType.Length;
|
|
18
|
+
min?: number | null;
|
|
19
|
+
max?: number | null;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export enum DateComparison {
|
|
23
|
+
NotBefore = "NotBefore",
|
|
24
|
+
NotAfter = "NotAfter",
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface DateValidator extends SchemaValidator {
|
|
28
|
+
type: ValidatorType.Date;
|
|
29
|
+
comparison: DateComparison;
|
|
30
|
+
fixedDate?: string | null;
|
|
31
|
+
daysFromCurrent?: number | null;
|
|
32
|
+
}
|