@react-typed-forms/schemas 15.1.3 → 16.0.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.
Files changed (46) hide show
  1. package/lib/RenderForm.d.ts +39 -0
  2. package/lib/controlBuilder.d.ts +3 -7
  3. package/lib/controlRender.d.ts +35 -64
  4. package/lib/index.cjs +438 -2189
  5. package/lib/index.cjs.map +1 -1
  6. package/lib/index.d.ts +3 -13
  7. package/lib/index.js +303 -1681
  8. package/lib/index.js.map +1 -1
  9. package/lib/renderers.d.ts +3 -2
  10. package/lib/types.d.ts +31 -0
  11. package/lib/util.d.ts +4 -48
  12. package/package.json +5 -4
  13. package/src/RenderForm.tsx +301 -0
  14. package/src/controlBuilder.ts +16 -19
  15. package/src/controlRender.tsx +109 -449
  16. package/src/createFormRenderer.tsx +3 -3
  17. package/src/index.ts +3 -13
  18. package/src/renderers.tsx +2 -2
  19. package/src/types.ts +52 -0
  20. package/src/util.ts +52 -113
  21. package/lib/controlDefinition.d.ts +0 -392
  22. package/lib/defaultSchemaInterface.d.ts +0 -28
  23. package/lib/dynamicHooks.d.ts +0 -54
  24. package/lib/entityExpression.d.ts +0 -32
  25. package/lib/formNode.d.ts +0 -46
  26. package/lib/hooks.d.ts +0 -29
  27. package/lib/schemaBuilder.d.ts +0 -67
  28. package/lib/schemaDataNode.d.ts +0 -31
  29. package/lib/schemaField.d.ts +0 -120
  30. package/lib/schemaInterface.d.ts +0 -102
  31. package/lib/schemaNode.d.ts +0 -54
  32. package/lib/schemaValidator.d.ts +0 -27
  33. package/lib/validators.d.ts +0 -19
  34. package/src/controlDefinition.ts +0 -621
  35. package/src/defaultSchemaInterface.ts +0 -201
  36. package/src/dynamicHooks.ts +0 -98
  37. package/src/entityExpression.ts +0 -38
  38. package/src/formNode.ts +0 -253
  39. package/src/hooks.tsx +0 -469
  40. package/src/schemaBuilder.ts +0 -318
  41. package/src/schemaDataNode.ts +0 -129
  42. package/src/schemaField.ts +0 -153
  43. package/src/schemaInterface.ts +0 -135
  44. package/src/schemaNode.ts +0 -279
  45. package/src/schemaValidator.ts +0 -32
  46. package/src/validators.ts +0 -217
@@ -1,135 +0,0 @@
1
- import { Control, ControlSetup } from "@react-typed-forms/core";
2
- import {
3
- EqualityFunc,
4
- FieldOption,
5
- SchemaField,
6
- ValidationMessageType,
7
- } from "./schemaField";
8
- import { SchemaDataNode } from "./schemaDataNode";
9
- import { SchemaNode } from "./schemaNode";
10
-
11
- /**
12
- * Interface for schema-related operations.
13
- */
14
- export interface SchemaInterface {
15
- /**
16
- * Checks if the value of a field is empty.
17
- * @param field The schema field.
18
- * @param value The value to check.
19
- * @returns True if the value is empty, false otherwise.
20
- */
21
- isEmptyValue(field: SchemaField, value: any): boolean;
22
-
23
- /**
24
- * Gets the text representation of a field's value.
25
- * @param field The schema field.
26
- * @param value The value to convert.
27
- * @param element Indicates if the value is an element, optional.
28
- * @param options The field options, optional.
29
- * @returns The text representation of the value.
30
- */
31
- textValue(
32
- field: SchemaField,
33
- value: any,
34
- element?: boolean,
35
- options?: FieldOption[],
36
- ): string | undefined;
37
-
38
- /**
39
- * Gets the text representation of a field's value for a data node.
40
- * @param dataNode
41
- */
42
- textValueForData(dataNode: SchemaDataNode): string | undefined;
43
-
44
- /**
45
- * Gets the length of a control's value.
46
- * @param field The schema field.
47
- * @param control The control to check.
48
- * @returns The length of the control's value.
49
- */
50
- controlLength(field: SchemaField, control: Control<any>): number;
51
-
52
- /**
53
- * Gets the length of a field's value.
54
- * @param field The schema field.
55
- * @param value The value to check.
56
- * @returns The length of the value.
57
- */
58
- valueLength(field: SchemaField, value: any): number;
59
-
60
- /**
61
- * Gets the data options for a schema data node.
62
- * @param node The schema data node.
63
- * @returns The data options.
64
- */
65
- getDataOptions(node: SchemaDataNode): FieldOption[] | null | undefined;
66
-
67
- /**
68
- * Gets the node options for a schema node.
69
- * @param node The schema node.
70
- * @returns The node options.
71
- */
72
- getNodeOptions(node: SchemaNode): FieldOption[] | null | undefined;
73
-
74
- /**
75
- * Gets the options for a schema field.
76
- * @param field The schema field.
77
- * @returns The field options.
78
- */
79
- getOptions(field: SchemaField): FieldOption[] | undefined | null;
80
-
81
- /**
82
- * Gets the filter options for a schema data node and field.
83
- * @param array The schema data node.
84
- * @param field The schema node.
85
- * @returns The filter options.
86
- */
87
- getFilterOptions(
88
- array: SchemaDataNode,
89
- field: SchemaNode,
90
- ): FieldOption[] | undefined | null;
91
-
92
- /**
93
- * Parses a string value to milliseconds.
94
- * @param field The schema field.
95
- * @param v The string value to parse.
96
- * @returns The parsed value in milliseconds.
97
- */
98
- parseToMillis(field: SchemaField, v: string): number;
99
-
100
- /**
101
- * Gets the validation message text for a field.
102
- * @param field The schema field.
103
- * @param messageType The type of validation message.
104
- * @param actual The actual value.
105
- * @param expected The expected value.
106
- * @returns The validation message text.
107
- */
108
- validationMessageText(
109
- field: SchemaField,
110
- messageType: ValidationMessageType,
111
- actual: any,
112
- expected: any,
113
- ): string;
114
-
115
- /**
116
- * Compares two values of a field.
117
- * @param field The schema field.
118
- * @param v1 The first value.
119
- * @param v2 The second value.
120
- * @returns The comparison result.
121
- */
122
- compareValue(field: SchemaField, v1: unknown, v2: unknown): number;
123
-
124
- /**
125
- * Gets the search text for a field's value.
126
- * @param field The schema field.
127
- * @param value The value to search.
128
- * @returns The search text.
129
- */
130
- searchText(field: SchemaField, value: any): string;
131
-
132
- makeEqualityFunc(field: SchemaNode, element?: boolean): EqualityFunc;
133
-
134
- makeControlSetup(field: SchemaNode, element?: boolean): ControlSetup<any>;
135
- }
package/src/schemaNode.ts DELETED
@@ -1,279 +0,0 @@
1
- import {
2
- CompoundField,
3
- FieldType,
4
- isCompoundField,
5
- missingField,
6
- SchemaField,
7
- } from "./schemaField";
8
-
9
- export interface SchemaTreeLookup {
10
- getSchema(schemaId: string): SchemaNode | undefined;
11
-
12
- getSchemaTree(schemaId: string): SchemaTree | undefined;
13
- }
14
-
15
- export abstract class SchemaTree {
16
- abstract rootNode: SchemaNode;
17
-
18
- abstract getSchemaTree(schemaId: string): SchemaTree | undefined;
19
-
20
- createChildNode(parent: SchemaNode, field: SchemaField): SchemaNode {
21
- return new SchemaNode(parent.id + "/" + field.field, field, this, parent);
22
- }
23
-
24
- getSchema(schemaId: string): SchemaNode | undefined {
25
- return this.getSchemaTree(schemaId)?.rootNode;
26
- }
27
- }
28
-
29
- class SchemaTreeImpl extends SchemaTree {
30
- rootNode: SchemaNode;
31
-
32
- getSchemaTree(schemaId: string): SchemaTree | undefined {
33
- return this.lookup?.getSchemaTree(schemaId);
34
- }
35
-
36
- constructor(
37
- rootFields: SchemaField[],
38
- private lookup?: SchemaTreeLookup,
39
- ) {
40
- super();
41
- this.rootNode = new SchemaNode(
42
- "",
43
- {
44
- type: FieldType.Compound,
45
- field: "",
46
- children: rootFields,
47
- } as CompoundField,
48
- this,
49
- );
50
- }
51
- }
52
-
53
- export function createSchemaTree(
54
- rootFields: SchemaField[],
55
- lookup?: SchemaTreeLookup,
56
- ): SchemaTree {
57
- return new SchemaTreeImpl(rootFields, lookup);
58
- }
59
-
60
- export class SchemaNode {
61
- public constructor(
62
- public id: string,
63
- public field: SchemaField,
64
- public tree: SchemaTree,
65
- public parent?: SchemaNode,
66
- ) {}
67
-
68
- getSchema(schemaId: string): SchemaNode | undefined {
69
- return this.tree.getSchema(schemaId);
70
- }
71
-
72
- getUnresolvedFields(): SchemaField[] {
73
- return isCompoundField(this.field) ? this.field.children : [];
74
- }
75
-
76
- getResolvedParent(): SchemaNode | undefined {
77
- const f = this.field;
78
- if (!isCompoundField(f)) return undefined;
79
- const parentNode = f.schemaRef
80
- ? this.tree.getSchema(f.schemaRef)
81
- : f.treeChildren
82
- ? this.parent?.getResolvedParent()
83
- : undefined;
84
- return parentNode ?? this;
85
- }
86
-
87
- getResolvedFields(): SchemaField[] {
88
- const resolvedParent = this.getResolvedParent();
89
- return resolvedParent?.getUnresolvedFields() ?? [];
90
- }
91
-
92
- getChildNodes(): SchemaNode[] {
93
- const node = this;
94
- return node.getResolvedFields().map((x) => node.createChildNode(x));
95
- }
96
-
97
- getChildField(field: string): SchemaField {
98
- return (
99
- this.getResolvedFields().find((x) => x.field === field) ??
100
- missingField(field)
101
- );
102
- }
103
-
104
- createChildNode(field: SchemaField): SchemaNode {
105
- return this.tree.createChildNode(this, field);
106
- }
107
-
108
- getChildNode(field: string): SchemaNode {
109
- return this.createChildNode(this.getChildField(field));
110
- }
111
- }
112
-
113
- export function resolveSchemaNode(
114
- node: SchemaNode,
115
- fieldSegment: string,
116
- ): SchemaNode | undefined {
117
- if (fieldSegment == ".") return node;
118
- if (fieldSegment == "..") return node.parent;
119
- return node.getChildNode(fieldSegment);
120
- }
121
-
122
- export function createSchemaNode(
123
- field: SchemaField,
124
- lookup: SchemaTree,
125
- parent: SchemaNode | undefined,
126
- ): SchemaNode {
127
- return new SchemaNode(
128
- parent ? parent.id + "/" + field.field : field.field,
129
- field,
130
- lookup,
131
- parent,
132
- );
133
- }
134
-
135
- export function createSchemaLookup<A extends Record<string, SchemaField[]>>(
136
- schemaMap: A,
137
- ): {
138
- getSchema(schemaId: keyof A): SchemaNode;
139
- getSchemaTree(schemaId: keyof A): SchemaTree;
140
- } {
141
- const lookup = {
142
- getSchemaTree,
143
- getSchema,
144
- };
145
- return lookup;
146
-
147
- function getSchema(schemaId: keyof A): SchemaNode {
148
- return getSchemaTree(schemaId)!.rootNode;
149
- }
150
-
151
- function getSchemaTree(schemaId: keyof A): SchemaTree {
152
- const fields = schemaMap[schemaId];
153
- if (fields) {
154
- return new SchemaTreeImpl(fields, lookup);
155
- }
156
- return undefined!;
157
- }
158
- }
159
-
160
- export function schemaForFieldRef(
161
- fieldRef: string | undefined,
162
- schema: SchemaNode,
163
- ): SchemaNode {
164
- return schemaForFieldPath(fieldRef?.split("/") ?? [], schema);
165
- }
166
-
167
- export function traverseSchemaPath<A>(
168
- fieldPath: string[],
169
- schema: SchemaNode,
170
- acc: A,
171
- next: (acc: A, node: SchemaNode) => A,
172
- ): A {
173
- let i = 0;
174
- while (i < fieldPath.length) {
175
- const nextField = fieldPath[i];
176
- let childNode = resolveSchemaNode(schema, nextField);
177
- if (!childNode) {
178
- childNode = createSchemaNode(
179
- missingField(nextField),
180
- schema.tree,
181
- schema,
182
- );
183
- }
184
- acc = next(acc, childNode);
185
- schema = childNode;
186
- i++;
187
- }
188
- return acc;
189
- }
190
-
191
- export function traverseData(
192
- fieldPath: string[],
193
- root: SchemaNode,
194
- data: { [k: string]: any },
195
- ): unknown {
196
- return traverseSchemaPath(
197
- fieldPath,
198
- root,
199
- data,
200
- (acc, n) => acc?.[n.field.field] as any,
201
- );
202
- }
203
-
204
- export function schemaForFieldPath(
205
- fieldPath: string[],
206
- schema: SchemaNode,
207
- ): SchemaNode {
208
- let i = 0;
209
- while (i < fieldPath.length) {
210
- const nextField = fieldPath[i];
211
- let childNode = resolveSchemaNode(schema, nextField);
212
- if (!childNode) {
213
- childNode = createSchemaNode(
214
- missingField(nextField),
215
- schema.tree,
216
- schema,
217
- );
218
- }
219
- schema = childNode;
220
- i++;
221
- }
222
- return schema;
223
- }
224
-
225
- export function getSchemaNodePath(node: SchemaNode) {
226
- const paths: string[] = [];
227
- let curNode: SchemaNode | undefined = node;
228
- while (curNode) {
229
- paths.push(curNode.field.field);
230
- curNode = curNode.parent;
231
- }
232
- return paths.reverse();
233
- }
234
-
235
- export function getSchemaNodePathString(node: SchemaNode) {
236
- return getSchemaNodePath(node).join("/");
237
- }
238
-
239
- export function isCompoundNode(node: SchemaNode) {
240
- return isCompoundField(node.field);
241
- }
242
-
243
- /**
244
- * Returns the relative path from a parent node to a child node.
245
- * @param parent
246
- * @param child
247
- */
248
- export function relativePath(parent: SchemaNode, child: SchemaNode): string {
249
- // return the path from child to parent
250
- if (parent.id === child.id) return ".";
251
-
252
- const parentPath = getSchemaNodePath(parent);
253
- const childPath = getSchemaNodePath(child);
254
- return relativeSegmentPath(parentPath, childPath);
255
- }
256
-
257
- /**
258
- * Returns the relative path from a parent node to a child node.
259
- * @param parentPath
260
- * @param childPath
261
- */
262
- export function relativeSegmentPath(
263
- parentPath: string[],
264
- childPath: string[],
265
- ): string {
266
- let i = 0;
267
- while (
268
- i < parentPath.length &&
269
- i < childPath.length &&
270
- parentPath[i] === childPath[i]
271
- ) {
272
- i++;
273
- }
274
-
275
- const upLevels = parentPath.length - i;
276
- const downPath = childPath.slice(i).join("/");
277
-
278
- return "../".repeat(upLevels) + downPath;
279
- }
@@ -1,32 +0,0 @@
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
- }
package/src/validators.ts DELETED
@@ -1,217 +0,0 @@
1
- import {
2
- ControlDefinition,
3
- DataControlDefinition,
4
- isDataControl,
5
- ControlDataContext,
6
- getRootDataNode,
7
- getJsonPath,
8
- } from "./controlDefinition";
9
- import {
10
- Control,
11
- useValidator,
12
- useValueChangeEffect,
13
- } from "@react-typed-forms/core";
14
- import { useCallback } from "react";
15
- import { useUpdatedRef } from "./util";
16
- import { useJsonataExpression } from "./hooks";
17
- import { makeHookDepString } from "./dynamicHooks";
18
- import {
19
- DateComparison,
20
- DateValidator,
21
- JsonataValidator,
22
- LengthValidator,
23
- SchemaValidator,
24
- ValidatorType,
25
- } from "./schemaValidator";
26
- import { FieldType, SchemaField, ValidationMessageType } from "./schemaField";
27
-
28
- interface ValidationHookContext {
29
- hiddenControl: Control<boolean | null | undefined>;
30
- dataContext: ControlDataContext;
31
- control: Control<any>;
32
- }
33
-
34
- export interface ValidationContext extends ValidationHookContext {
35
- definition: DataControlDefinition;
36
- field: SchemaField;
37
- index: number;
38
- }
39
-
40
- export function useMakeValidationHook(
41
- definition: ControlDefinition,
42
- useValidatorFor: (
43
- validator: SchemaValidator,
44
- ctx: ValidationContext,
45
- ) => void = useDefaultValidator,
46
- ): (ctx: ValidationHookContext) => void {
47
- const dd = isDataControl(definition) ? definition : undefined;
48
-
49
- const refData = useUpdatedRef({ dd, useValidatorFor });
50
- const depString = dd
51
- ? makeHookDepString(dd.validators ?? [], (x) => x.type)
52
- : "~";
53
- return useCallback(
54
- (ctx) => {
55
- const { dd } = refData.current;
56
- if (!dd) return;
57
- const field = ctx.dataContext.dataNode?.schema.field ?? {
58
- field: "__missing",
59
- type: FieldType.Any,
60
- };
61
- const {
62
- control,
63
- hiddenControl,
64
- dataContext: { schemaInterface },
65
- } = ctx;
66
-
67
- useValueChangeEffect(control, () => control.setError("default", ""));
68
- if (dd.required)
69
- useValidator(
70
- control,
71
- (v) => {
72
- return !hiddenControl.value &&
73
- schemaInterface.isEmptyValue(field, v)
74
- ? schemaInterface.validationMessageText(
75
- field,
76
- ValidationMessageType.NotEmpty,
77
- false,
78
- true,
79
- )
80
- : null;
81
- },
82
- "required",
83
- );
84
- dd.validators?.forEach((v, i) =>
85
- useValidatorFor(v, { ...ctx, index: i, field, definition: dd }),
86
- );
87
- },
88
- [!!dd, dd?.required, depString, useValidatorFor],
89
- );
90
- }
91
-
92
- const useDefaultValidator = (
93
- validator: SchemaValidator,
94
- ctx: ValidationContext,
95
- ) => {
96
- switch (validator.type) {
97
- case ValidatorType.Length:
98
- useLengthValidator(validator as LengthValidator, ctx);
99
- break;
100
- case ValidatorType.Jsonata:
101
- useJsonataValidator(validator as JsonataValidator, ctx);
102
- break;
103
- case ValidatorType.Date:
104
- useDateValidator(validator as DateValidator, ctx);
105
- break;
106
- }
107
- };
108
-
109
- export function useJsonataValidator(
110
- validator: JsonataValidator,
111
- ctx: ValidationContext,
112
- ) {
113
- const sdn = ctx.dataContext.parentNode;
114
- const errorMsg = useJsonataExpression(
115
- validator.expression,
116
- getRootDataNode(sdn).control!,
117
- getJsonPath(sdn),
118
- undefined,
119
- (v) => (v == null ? null : typeof v === "string" ? v : JSON.stringify(v)),
120
- );
121
- useValidator(
122
- ctx.control,
123
- () => (!ctx.hiddenControl.value ? errorMsg.value : null),
124
- "jsonata" + ctx.index,
125
- );
126
- }
127
-
128
- export function useLengthValidator(
129
- lv: LengthValidator,
130
- ctx: ValidationContext,
131
- ) {
132
- const {
133
- control,
134
- dataContext: { schemaInterface },
135
- hiddenControl,
136
- field,
137
- } = ctx;
138
- useValidator(
139
- control,
140
- (v) => {
141
- const len = schemaInterface.controlLength(field, control);
142
- const hidden = hiddenControl.value;
143
- if (hidden) {
144
- return undefined;
145
- }
146
- if (lv.min != null && len < lv.min) {
147
- if (field?.collection) {
148
- control.setValue((v) =>
149
- Array.isArray(v)
150
- ? v.concat(Array.from({ length: lv.min! - v.length }))
151
- : Array.from({ length: lv.min! }),
152
- );
153
- } else {
154
- return schemaInterface.validationMessageText(
155
- field,
156
- ValidationMessageType.MinLength,
157
- len,
158
- lv.min,
159
- );
160
- }
161
- } else if (lv.max != null && len > lv.max) {
162
- return schemaInterface.validationMessageText(
163
- field,
164
- ValidationMessageType.MaxLength,
165
- len,
166
- lv.max,
167
- );
168
- }
169
- return undefined;
170
- },
171
- "length" + ctx.index,
172
- );
173
- }
174
-
175
- export function useDateValidator(dv: DateValidator, ctx: ValidationContext) {
176
- const {
177
- control,
178
- field,
179
- index,
180
- dataContext: { schemaInterface },
181
- } = ctx;
182
- let comparisonDate: number;
183
- if (dv.fixedDate) {
184
- comparisonDate = schemaInterface.parseToMillis(field, dv.fixedDate);
185
- } else {
186
- const nowDate = new Date();
187
- comparisonDate = Date.UTC(
188
- nowDate.getFullYear(),
189
- nowDate.getMonth(),
190
- nowDate.getDate(),
191
- );
192
- if (dv.daysFromCurrent) {
193
- comparisonDate += dv.daysFromCurrent * 86400000;
194
- }
195
- }
196
- useValidator(
197
- control,
198
- (v) => {
199
- if (v) {
200
- const selDate = schemaInterface.parseToMillis(field, v);
201
- const notAfter = dv.comparison === DateComparison.NotAfter;
202
- if (notAfter ? selDate > comparisonDate : selDate < comparisonDate) {
203
- return schemaInterface.validationMessageText(
204
- field,
205
- notAfter
206
- ? ValidationMessageType.NotAfterDate
207
- : ValidationMessageType.NotBeforeDate,
208
- selDate,
209
- comparisonDate,
210
- );
211
- }
212
- }
213
- return null;
214
- },
215
- "date" + index,
216
- );
217
- }