@astroapps/forms-core 1.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.
@@ -0,0 +1,102 @@
1
+ import { EqualityFunc, FieldOption, SchemaField, ValidationMessageType } from "./schemaField";
2
+ import { SchemaDataNode } from "./schemaDataNode";
3
+ import { SchemaNode } from "./schemaNode";
4
+ import { Control, ControlSetup } from "@astroapps/controls";
5
+ /**
6
+ * Interface for schema-related operations.
7
+ */
8
+ export interface SchemaInterface {
9
+ /**
10
+ * Checks if the value of a field is empty.
11
+ * @param field The schema field.
12
+ * @param value The value to check.
13
+ * @returns True if the value is empty, false otherwise.
14
+ */
15
+ isEmptyValue(field: SchemaField, value: any): boolean;
16
+ /**
17
+ * Gets the text representation of a field's value.
18
+ * @param field The schema field.
19
+ * @param value The value to convert.
20
+ * @param element Indicates if the value is an element, optional.
21
+ * @param options The field options, optional.
22
+ * @returns The text representation of the value.
23
+ */
24
+ textValue(field: SchemaField, value: any, element?: boolean, options?: FieldOption[]): string | undefined;
25
+ /**
26
+ * Gets the text representation of a field's value for a data node.
27
+ * @param dataNode
28
+ */
29
+ textValueForData(dataNode: SchemaDataNode): string | undefined;
30
+ /**
31
+ * Gets the length of a control's value.
32
+ * @param field The schema field.
33
+ * @param control The control to check.
34
+ * @returns The length of the control's value.
35
+ */
36
+ controlLength(field: SchemaField, control: Control<any>): number;
37
+ /**
38
+ * Gets the length of a field's value.
39
+ * @param field The schema field.
40
+ * @param value The value to check.
41
+ * @returns The length of the value.
42
+ */
43
+ valueLength(field: SchemaField, value: any): number;
44
+ /**
45
+ * Gets the data options for a schema data node.
46
+ * @param node The schema data node.
47
+ * @returns The data options.
48
+ */
49
+ getDataOptions(node: SchemaDataNode): FieldOption[] | null | undefined;
50
+ /**
51
+ * Gets the node options for a schema node.
52
+ * @param node The schema node.
53
+ * @returns The node options.
54
+ */
55
+ getNodeOptions(node: SchemaNode): FieldOption[] | null | undefined;
56
+ /**
57
+ * Gets the options for a schema field.
58
+ * @param field The schema field.
59
+ * @returns The field options.
60
+ */
61
+ getOptions(field: SchemaField): FieldOption[] | undefined | null;
62
+ /**
63
+ * Gets the filter options for a schema data node and field.
64
+ * @param array The schema data node.
65
+ * @param field The schema node.
66
+ * @returns The filter options.
67
+ */
68
+ getFilterOptions(array: SchemaDataNode, field: SchemaNode): FieldOption[] | undefined | null;
69
+ /**
70
+ * Parses a string value to milliseconds.
71
+ * @param field The schema field.
72
+ * @param v The string value to parse.
73
+ * @returns The parsed value in milliseconds.
74
+ */
75
+ parseToMillis(field: SchemaField, v: string): number;
76
+ /**
77
+ * Gets the validation message text for a field.
78
+ * @param field The schema field.
79
+ * @param messageType The type of validation message.
80
+ * @param actual The actual value.
81
+ * @param expected The expected value.
82
+ * @returns The validation message text.
83
+ */
84
+ validationMessageText(field: SchemaField, messageType: ValidationMessageType, actual: any, expected: any): string;
85
+ /**
86
+ * Compares two values of a field.
87
+ * @param field The schema field.
88
+ * @param v1 The first value.
89
+ * @param v2 The second value.
90
+ * @returns The comparison result.
91
+ */
92
+ compareValue(field: SchemaField, v1: unknown, v2: unknown): number;
93
+ /**
94
+ * Gets the search text for a field's value.
95
+ * @param field The schema field.
96
+ * @param value The value to search.
97
+ * @returns The search text.
98
+ */
99
+ searchText(field: SchemaField, value: any): string;
100
+ makeEqualityFunc(field: SchemaNode, element?: boolean): EqualityFunc;
101
+ makeControlSetup(field: SchemaNode, element?: boolean): ControlSetup<any>;
102
+ }
@@ -0,0 +1,54 @@
1
+ import { SchemaField } from "./schemaField";
2
+ export interface SchemaTreeLookup {
3
+ getSchema(schemaId: string): SchemaNode | undefined;
4
+ getSchemaTree(schemaId: string, additional?: SchemaField[]): SchemaTree | undefined;
5
+ }
6
+ export declare abstract class SchemaTree {
7
+ abstract rootNode: SchemaNode;
8
+ abstract getSchemaTree(schemaId: string): SchemaTree | undefined;
9
+ createChildNode(parent: SchemaNode, field: SchemaField): SchemaNode;
10
+ getSchema(schemaId: string): SchemaNode | undefined;
11
+ }
12
+ export declare function createSchemaTree(rootFields: SchemaField[], lookup?: SchemaTreeLookup): SchemaTree;
13
+ export declare class SchemaNode {
14
+ id: string;
15
+ field: SchemaField;
16
+ tree: SchemaTree;
17
+ parent?: SchemaNode | undefined;
18
+ constructor(id: string, field: SchemaField, tree: SchemaTree, parent?: SchemaNode | undefined);
19
+ getSchema(schemaId: string): SchemaNode | undefined;
20
+ getUnresolvedFields(): SchemaField[];
21
+ getResolvedParent(noRecurse?: boolean): SchemaNode | undefined;
22
+ getResolvedFields(): SchemaField[];
23
+ getChildNodes(): SchemaNode[];
24
+ getChildField(field: string): SchemaField;
25
+ createChildNode(field: SchemaField): SchemaNode;
26
+ getChildNode(field: string): SchemaNode;
27
+ }
28
+ export declare function resolveSchemaNode(node: SchemaNode, fieldSegment: string): SchemaNode | undefined;
29
+ export declare function createSchemaNode(field: SchemaField, lookup: SchemaTree, parent: SchemaNode | undefined): SchemaNode;
30
+ export declare function createSchemaLookup<A extends Record<string, SchemaField[]>>(schemaMap: A): {
31
+ getSchema(schemaId: keyof A): SchemaNode;
32
+ getSchemaTree(schemaId: keyof A, additional?: SchemaField[]): SchemaTree;
33
+ };
34
+ export declare function schemaForFieldRef(fieldRef: string | undefined, schema: SchemaNode): SchemaNode;
35
+ export declare function traverseSchemaPath<A>(fieldPath: string[], schema: SchemaNode, acc: A, next: (acc: A, node: SchemaNode) => A): A;
36
+ export declare function traverseData(fieldPath: string[], root: SchemaNode, data: {
37
+ [k: string]: any;
38
+ }): unknown;
39
+ export declare function schemaForFieldPath(fieldPath: string[], schema: SchemaNode): SchemaNode;
40
+ export declare function getSchemaNodePath(node: SchemaNode): string[];
41
+ export declare function getSchemaNodePathString(node: SchemaNode): string;
42
+ export declare function isCompoundNode(node: SchemaNode): boolean;
43
+ /**
44
+ * Returns the relative path from a parent node to a child node.
45
+ * @param parent
46
+ * @param child
47
+ */
48
+ export declare function relativePath(parent: SchemaNode, child: SchemaNode): string;
49
+ /**
50
+ * Returns the relative path from a parent node to a child node.
51
+ * @param parentPath
52
+ * @param childPath
53
+ */
54
+ export declare function relativeSegmentPath(parentPath: string[], childPath: string[]): string;
@@ -0,0 +1,27 @@
1
+ export declare enum ValidatorType {
2
+ Jsonata = "Jsonata",
3
+ Date = "Date",
4
+ Length = "Length"
5
+ }
6
+ export interface SchemaValidator {
7
+ type: string;
8
+ }
9
+ export interface JsonataValidator extends SchemaValidator {
10
+ type: ValidatorType.Jsonata;
11
+ expression: string;
12
+ }
13
+ export interface LengthValidator extends SchemaValidator {
14
+ type: ValidatorType.Length;
15
+ min?: number | null;
16
+ max?: number | null;
17
+ }
18
+ export declare enum DateComparison {
19
+ NotBefore = "NotBefore",
20
+ NotAfter = "NotAfter"
21
+ }
22
+ export interface DateValidator extends SchemaValidator {
23
+ type: ValidatorType.Date;
24
+ comparison: DateComparison;
25
+ fixedDate?: string | null;
26
+ daysFromCurrent?: number | null;
27
+ }
package/lib/util.d.ts ADDED
@@ -0,0 +1,14 @@
1
+ import { CleanupScope, Control, ControlSetup } from "@astroapps/controls";
2
+ /**
3
+ * Type representing a JSON path, which can be a string or a number.
4
+ */
5
+ export type JsonPath = string | number;
6
+ /**
7
+ * Converts a JSON path array to a string.
8
+ * @param jsonPath - The JSON path array to convert.
9
+ * @param customIndex - Optional function to customize the index format.
10
+ * @returns The JSON path string.
11
+ */
12
+ export declare function jsonPathString(jsonPath: JsonPath[], customIndex?: (n: number) => string): string;
13
+ export declare function createScopedComputed<T>(parent: CleanupScope, compute: () => T): Control<T>;
14
+ export declare function createScoped<T>(parent: CleanupScope, value: T, setup?: ControlSetup<T>): Control<T>;
@@ -0,0 +1,23 @@
1
+ import { DateValidator, JsonataValidator, LengthValidator, SchemaValidator } from "./schemaValidator";
2
+ import { ControlDefinition } from "./controlDefinition";
3
+ import { SchemaDataNode } from "./schemaDataNode";
4
+ import { Control } from "@astroapps/controls";
5
+ import { SchemaInterface } from "./schemaInterface";
6
+ import { FormContextOptions } from "./formState";
7
+ import { FormNode } from "./formNode";
8
+ export interface ValidationEvalContext {
9
+ addSync(validate: (value: unknown) => string | undefined | null): void;
10
+ addCleanup(cleanup: () => void): void;
11
+ validationEnabled: Control<boolean>;
12
+ parentData: SchemaDataNode;
13
+ data: SchemaDataNode;
14
+ schemaInterface: SchemaInterface;
15
+ formContext: Control<FormContextOptions>;
16
+ }
17
+ export type ValidatorEval<T extends SchemaValidator> = (validation: T, context: ValidationEvalContext) => void;
18
+ export declare const jsonataValidator: ValidatorEval<JsonataValidator>;
19
+ export declare const lengthValidator: ValidatorEval<LengthValidator>;
20
+ export declare const dateValidator: ValidatorEval<DateValidator>;
21
+ export declare const defaultValidators: Record<string, ValidatorEval<any>>;
22
+ export declare function createValidators(def: ControlDefinition, context: ValidationEvalContext): void;
23
+ export declare function setupValidation(controlImpl: Control<FormContextOptions>, definition: ControlDefinition, dataNode: Control<SchemaDataNode | undefined>, schemaInterface: SchemaInterface, parent: SchemaDataNode, formNode: FormNode): void;
package/package.json ADDED
@@ -0,0 +1,60 @@
1
+ {
2
+ "name": "@astroapps/forms-core",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "type": "module",
6
+ "main": "lib/index.cjs",
7
+ "module": "lib/index.js",
8
+ "types": "lib/index.d.ts",
9
+ "exports": {
10
+ "types": "./lib/index.d.ts",
11
+ "require": "./lib/index.cjs",
12
+ "default": "./lib/index.js"
13
+ },
14
+ "author": "Astrolabe Enterprises",
15
+ "license": "ISC",
16
+ "bugs": {
17
+ "url": "https://github.com/astrolabe-apps/astrolabe-common/issues"
18
+ },
19
+ "homepage": "https://github.com/astrolabe-apps/astrolabe-common#readme",
20
+ "publishConfig": {
21
+ "access": "public"
22
+ },
23
+ "keywords": [
24
+ "react",
25
+ "typescript",
26
+ "forms"
27
+ ],
28
+ "dependencies": {
29
+ "clsx": "^1 || ^2",
30
+ "uuid": "^10.0.0",
31
+ "jsonata": "^2.0.4"
32
+ },
33
+ "peerDependencies": {
34
+ "@astroapps/controls": "^1.3.0"
35
+ },
36
+ "devDependencies": {
37
+ "@astroapps/controls": "^1.3.0",
38
+ "jest": "^29.7.0",
39
+ "tsx": "^4.19.1",
40
+ "fast-check": "^3.22.0",
41
+ "ts-jest": "^29.2.5",
42
+ "@jest/globals": "^29.7.0",
43
+ "typedoc": "^0.27.2",
44
+ "@types/uuid": "^10.0.0",
45
+ "@types/react": "^18.2.28",
46
+ "markdown-magic": "^2.6.1",
47
+ "microbundle": "^0.15.1",
48
+ "nswag": "^13.18.2",
49
+ "prettier": "^3.0.3",
50
+ "rimraf": "^3.0.2",
51
+ "typescript": "^5.6.2"
52
+ },
53
+ "scripts": {
54
+ "build": "rimraf ./lib/ node_modules/.cache && microbundle -f modern,cjs --no-compress",
55
+ "watch": "microbundle -w -f modern,cjs --no-compress",
56
+ "test": "jest --coverage",
57
+ "play": "tsx test/play.ts",
58
+ "update-readme": "md-magic --path README.md"
59
+ }
60
+ }