@stonecrop/stonecrop 0.31.0 → 0.33.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 (121) hide show
  1. package/dist/stonecrop.js +3548 -2740
  2. package/dist/stonecrop.js.map +1 -1
  3. package/dist/tsdoc-metadata.json +1 -1
  4. package/package.json +33 -27
  5. package/dist/src/client-action.d.ts +0 -31
  6. package/dist/src/client-action.d.ts.map +0 -1
  7. package/dist/src/client-action.js +0 -169
  8. package/dist/src/client-handler.d.ts +0 -43
  9. package/dist/src/client-handler.d.ts.map +0 -1
  10. package/dist/src/client-handler.js +0 -42
  11. package/dist/src/composables/lazy-link.d.ts +0 -25
  12. package/dist/src/composables/lazy-link.d.ts.map +0 -1
  13. package/dist/src/composables/lazy-link.js +0 -124
  14. package/dist/src/composables/operation-log.d.ts +0 -136
  15. package/dist/src/composables/operation-log.d.ts.map +0 -1
  16. package/dist/src/composables/operation-log.js +0 -224
  17. package/dist/src/composables/stonecrop.d.ts +0 -37
  18. package/dist/src/composables/stonecrop.d.ts.map +0 -1
  19. package/dist/src/composables/stonecrop.js +0 -433
  20. package/dist/src/doctype.d.ts +0 -243
  21. package/dist/src/doctype.d.ts.map +0 -1
  22. package/dist/src/doctype.js +0 -325
  23. package/dist/src/draft.d.ts +0 -35
  24. package/dist/src/draft.d.ts.map +0 -1
  25. package/dist/src/draft.js +0 -36
  26. package/dist/src/exceptions.d.ts +0 -14
  27. package/dist/src/exceptions.d.ts.map +0 -1
  28. package/dist/src/exceptions.js +0 -16
  29. package/dist/src/field-triggers.d.ts +0 -184
  30. package/dist/src/field-triggers.d.ts.map +0 -1
  31. package/dist/src/field-triggers.js +0 -577
  32. package/dist/src/index.d.ts +0 -25
  33. package/dist/src/index.d.ts.map +0 -1
  34. package/dist/src/index.js +0 -39
  35. package/dist/src/plugins/index.d.ts +0 -35
  36. package/dist/src/plugins/index.d.ts.map +0 -1
  37. package/dist/src/plugins/index.js +0 -98
  38. package/dist/src/registry.d.ts +0 -161
  39. package/dist/src/registry.d.ts.map +0 -1
  40. package/dist/src/registry.js +0 -384
  41. package/dist/src/schema-validator.d.ts +0 -78
  42. package/dist/src/schema-validator.d.ts.map +0 -1
  43. package/dist/src/schema-validator.js +0 -398
  44. package/dist/src/stonecrop.d.ts +0 -539
  45. package/dist/src/stonecrop.d.ts.map +0 -1
  46. package/dist/src/stonecrop.js +0 -610
  47. package/dist/src/stores/hst.d.ts +0 -92
  48. package/dist/src/stores/hst.d.ts.map +0 -1
  49. package/dist/src/stores/hst.js +0 -487
  50. package/dist/src/stores/index.d.ts +0 -9
  51. package/dist/src/stores/index.d.ts.map +0 -1
  52. package/dist/src/stores/index.js +0 -13
  53. package/dist/src/stores/operation-log.d.ts +0 -262
  54. package/dist/src/stores/operation-log.d.ts.map +0 -1
  55. package/dist/src/stores/operation-log.js +0 -571
  56. package/dist/src/stores/validation.d.ts +0 -83
  57. package/dist/src/stores/validation.d.ts.map +0 -1
  58. package/dist/src/stores/validation.js +0 -104
  59. package/dist/src/types/client-action.d.ts +0 -121
  60. package/dist/src/types/client-action.d.ts.map +0 -1
  61. package/dist/src/types/client-action.js +0 -0
  62. package/dist/src/types/composable.d.ts +0 -268
  63. package/dist/src/types/composable.d.ts.map +0 -1
  64. package/dist/src/types/composable.js +0 -0
  65. package/dist/src/types/doctype.d.ts +0 -34
  66. package/dist/src/types/doctype.d.ts.map +0 -1
  67. package/dist/src/types/doctype.js +0 -0
  68. package/dist/src/types/field-triggers.d.ts +0 -186
  69. package/dist/src/types/field-triggers.d.ts.map +0 -1
  70. package/dist/src/types/field-triggers.js +0 -4
  71. package/dist/src/types/hst.d.ts +0 -70
  72. package/dist/src/types/hst.d.ts.map +0 -1
  73. package/dist/src/types/hst.js +0 -0
  74. package/dist/src/types/index.d.ts +0 -11
  75. package/dist/src/types/index.d.ts.map +0 -1
  76. package/dist/src/types/index.js +0 -11
  77. package/dist/src/types/operation-log.d.ts +0 -163
  78. package/dist/src/types/operation-log.d.ts.map +0 -1
  79. package/dist/src/types/operation-log.js +0 -0
  80. package/dist/src/types/plugin.d.ts +0 -37
  81. package/dist/src/types/plugin.d.ts.map +0 -1
  82. package/dist/src/types/plugin.js +0 -0
  83. package/dist/src/types/registry.d.ts +0 -11
  84. package/dist/src/types/registry.d.ts.map +0 -1
  85. package/dist/src/types/registry.js +0 -0
  86. package/dist/src/types/schema-validator.d.ts +0 -66
  87. package/dist/src/types/schema-validator.d.ts.map +0 -1
  88. package/dist/src/types/schema-validator.js +0 -13
  89. package/dist/src/types/stonecrop.d.ts +0 -32
  90. package/dist/src/types/stonecrop.d.ts.map +0 -1
  91. package/dist/src/types/stonecrop.js +0 -0
  92. package/dist/stonecrop.tsbuildinfo +0 -1
  93. package/src/client-action.ts +0 -207
  94. package/src/client-handler.ts +0 -55
  95. package/src/composables/lazy-link.ts +0 -145
  96. package/src/composables/operation-log.ts +0 -257
  97. package/src/composables/stonecrop.ts +0 -556
  98. package/src/doctype.ts +0 -360
  99. package/src/draft.ts +0 -38
  100. package/src/exceptions.ts +0 -16
  101. package/src/field-triggers.ts +0 -686
  102. package/src/index.ts +0 -81
  103. package/src/plugins/index.ts +0 -113
  104. package/src/registry.ts +0 -433
  105. package/src/schema-validator.ts +0 -476
  106. package/src/stonecrop.ts +0 -721
  107. package/src/stores/hst.ts +0 -626
  108. package/src/stores/index.ts +0 -21
  109. package/src/stores/operation-log.ts +0 -675
  110. package/src/stores/validation.ts +0 -138
  111. package/src/types/client-action.ts +0 -121
  112. package/src/types/composable.ts +0 -288
  113. package/src/types/doctype.ts +0 -35
  114. package/src/types/field-triggers.ts +0 -201
  115. package/src/types/hst.ts +0 -77
  116. package/src/types/index.ts +0 -11
  117. package/src/types/operation-log.ts +0 -202
  118. package/src/types/plugin.ts +0 -38
  119. package/src/types/registry.ts +0 -10
  120. package/src/types/schema-validator.ts +0 -69
  121. package/src/types/stonecrop.ts +0 -33
@@ -1,78 +0,0 @@
1
- /**
2
- * Schema Validation Utilities
3
- * Validates Stonecrop schemas for integrity and consistency
4
- * @packageDocumentation
5
- */
6
- import type { DoctypeField } from '@stonecrop/schema';
7
- import type { LinkDeclaration } from '@stonecrop/schema';
8
- import type { List, Map as ImmutableMap } from 'immutable';
9
- import type { AnyStateNodeConfig } from 'xstate';
10
- import type Registry from './registry';
11
- import type { ValidationResult, ValidatorOptions } from './types/schema-validator';
12
- /**
13
- * Schema validator class
14
- * @public
15
- */
16
- export declare class SchemaValidator {
17
- private options;
18
- /**
19
- * Creates a new SchemaValidator instance
20
- * @param options - Validator configuration options
21
- */
22
- constructor(options?: ValidatorOptions);
23
- /**
24
- * Validates a complete doctype schema
25
- * @param doctype - Doctype name
26
- * @param schema - Schema fields (List or Array)
27
- * @param workflow - Optional workflow configuration
28
- * @param actions - Optional actions map
29
- * @param links - Optional links object
30
- * @returns Validation result
31
- */
32
- validate(doctype: string, schema: List<DoctypeField> | DoctypeField[] | undefined, workflow?: AnyStateNodeConfig, actions?: ImmutableMap<string, string[]> | Map<string, string[]>, links?: Record<string, LinkDeclaration>): ValidationResult;
33
- /**
34
- * Validates that required schema properties are present
35
- * @internal
36
- */
37
- private validateRequiredProperties;
38
- /**
39
- * Validates Link field targets exist in registry
40
- * @internal
41
- */
42
- private validateLinkFields;
43
- /**
44
- * Validates link declarations: target resolution, backlink consistency, Link field correspondence
45
- * @internal
46
- */
47
- private validateLinkDeclarations;
48
- /**
49
- * Validates workflow state machine configuration
50
- * @internal
51
- */
52
- private validateWorkflow;
53
- /**
54
- * Validates that actions are registered in the FieldTriggerEngine
55
- * @internal
56
- */
57
- private validateActionRegistration;
58
- }
59
- /**
60
- * Creates a validator with the given registry
61
- * @param registry - Registry instance
62
- * @param options - Additional validator options
63
- * @returns SchemaValidator instance
64
- * @public
65
- */
66
- export declare function createValidator(registry: Registry, options?: Partial<ValidatorOptions>): SchemaValidator;
67
- /**
68
- * Quick validation helper
69
- * @param doctype - Doctype name
70
- * @param schema - Schema fields
71
- * @param registry - Registry instance
72
- * @param workflow - Optional workflow configuration
73
- * @param actions - Optional actions map
74
- * @returns Validation result
75
- * @public
76
- */
77
- export declare function validateSchema(doctype: string, schema: List<DoctypeField> | DoctypeField[] | undefined, registry: Registry, workflow?: AnyStateNodeConfig, actions?: ImmutableMap<string, string[]> | Map<string, string[]>): ValidationResult;
78
- //# sourceMappingURL=schema-validator.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"schema-validator.d.ts","sourceRoot":"","sources":["../../src/schema-validator.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AACrD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AAExD,OAAO,KAAK,EAAE,IAAI,EAAE,GAAG,IAAI,YAAY,EAAE,MAAM,WAAW,CAAA;AAC1D,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,QAAQ,CAAA;AAGhD,OAAO,KAAK,QAAQ,MAAM,YAAY,CAAA;AAEtC,OAAO,KAAK,EAAmB,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAA;AAEnG;;;GAGG;AACH,qBAAa,eAAe;IAC3B,OAAO,CAAC,OAAO,CAA4B;IAE3C;;;OAGG;gBACS,OAAO,GAAE,gBAAqB;IAW1C;;;;;;;;OAQG;IACH,QAAQ,CACP,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,IAAI,CAAC,YAAY,CAAC,GAAG,YAAY,EAAE,GAAG,SAAS,EACvD,QAAQ,CAAC,EAAE,kBAAkB,EAC7B,OAAO,CAAC,EAAE,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,EAChE,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,GACrC,gBAAgB;IA+CnB;;;OAGG;IACH,OAAO,CAAC,0BAA0B;IA8DlC;;;OAGG;IACH,OAAO,CAAC,kBAAkB;IAmC1B;;;OAGG;IACH,OAAO,CAAC,wBAAwB;IAkGhC;;;OAGG;IACH,OAAO,CAAC,gBAAgB;IAoFxB;;;OAGG;IACH,OAAO,CAAC,0BAA0B;CAwClC;AAED;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,QAAQ,EAAE,QAAQ,EAAE,OAAO,CAAC,EAAE,OAAO,CAAC,gBAAgB,CAAC,GAAG,eAAe,CAKxG;AAED;;;;;;;;;GASG;AACH,wBAAgB,cAAc,CAC7B,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,IAAI,CAAC,YAAY,CAAC,GAAG,YAAY,EAAE,GAAG,SAAS,EACvD,QAAQ,EAAE,QAAQ,EAClB,QAAQ,CAAC,EAAE,kBAAkB,EAC7B,OAAO,CAAC,EAAE,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,GAC9D,gBAAgB,CAGlB"}
@@ -1,398 +0,0 @@
1
- /**
2
- * Schema Validation Utilities
3
- * Validates Stonecrop schemas for integrity and consistency
4
- * @packageDocumentation
5
- */
6
- import { componentLinkExpansion } from '@stonecrop/schema';
7
- import { getGlobalTriggerEngine } from './field-triggers';
8
- import { ValidationSeverity } from './types/schema-validator';
9
- /**
10
- * Schema validator class
11
- * @public
12
- */
13
- export class SchemaValidator {
14
- options;
15
- /**
16
- * Creates a new SchemaValidator instance
17
- * @param options - Validator configuration options
18
- */
19
- constructor(options = {}) {
20
- this.options = {
21
- registry: options.registry || null,
22
- validateLinkTargets: options.validateLinkTargets ?? true,
23
- validateLinks: options.validateLinks ?? true,
24
- validateActions: options.validateActions ?? true,
25
- validateWorkflows: options.validateWorkflows ?? true,
26
- validateRequiredProperties: options.validateRequiredProperties ?? true,
27
- };
28
- }
29
- /**
30
- * Validates a complete doctype schema
31
- * @param doctype - Doctype name
32
- * @param schema - Schema fields (List or Array)
33
- * @param workflow - Optional workflow configuration
34
- * @param actions - Optional actions map
35
- * @param links - Optional links object
36
- * @returns Validation result
37
- */
38
- validate(doctype, schema, workflow, actions, links) {
39
- const issues = [];
40
- // Convert schema to array for easier iteration
41
- const schemaArray = schema ? (Array.isArray(schema) ? schema : schema.toArray()) : [];
42
- // Validate required properties
43
- if (this.options.validateRequiredProperties) {
44
- issues.push(...this.validateRequiredProperties(doctype, schemaArray, links));
45
- }
46
- // Validate Link field targets
47
- if (this.options.validateLinkTargets && this.options.registry) {
48
- issues.push(...this.validateLinkFields(doctype, schemaArray, this.options.registry));
49
- }
50
- // Validate links object
51
- if (this.options.validateLinks && this.options.registry && links) {
52
- issues.push(...this.validateLinkDeclarations(doctype, links, schemaArray, this.options.registry));
53
- }
54
- // Validate workflow configuration
55
- if (this.options.validateWorkflows && workflow) {
56
- issues.push(...this.validateWorkflow(doctype, workflow));
57
- }
58
- // Validate action registration
59
- if (this.options.validateActions && actions) {
60
- const actionsMap = actions instanceof Map ? actions : actions.toObject();
61
- // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- toObject() returns plain object; runtime-safe cast for Map<string, string[]>
62
- issues.push(...this.validateActionRegistration(doctype, actionsMap));
63
- }
64
- // Calculate counts
65
- const errorCount = issues.filter(i => i.severity === ValidationSeverity.ERROR).length;
66
- const warningCount = issues.filter(i => i.severity === ValidationSeverity.WARNING).length;
67
- const infoCount = issues.filter(i => i.severity === ValidationSeverity.INFO).length;
68
- return {
69
- valid: errorCount === 0,
70
- issues,
71
- errorCount,
72
- warningCount,
73
- infoCount,
74
- };
75
- }
76
- /**
77
- * Validates that required schema properties are present
78
- * @internal
79
- */
80
- validateRequiredProperties(doctype, schema, links) {
81
- const issues = [];
82
- for (const field of schema) {
83
- // Check for fieldname
84
- if (!field.fieldname) {
85
- issues.push({
86
- severity: ValidationSeverity.ERROR,
87
- rule: 'required-fieldname',
88
- message: 'Field is missing required property: fieldname',
89
- doctype,
90
- context: { field },
91
- });
92
- continue;
93
- }
94
- // A value field renders through its component and has no other rendering axis;
95
- // fieldset/table have their own structural requirements.
96
- if (field.kind === 'field' && !field.component) {
97
- issues.push({
98
- severity: ValidationSeverity.ERROR,
99
- rule: 'required-component',
100
- message: `Field "${field.fieldname}" must have a component property`,
101
- doctype,
102
- fieldname: field.fieldname,
103
- });
104
- }
105
- // A link component with nothing to point at renders an empty picker, which looks exactly
106
- // like a record that has no value — so this fails silently at runtime rather than loudly.
107
- // Link-ness comes from `doctype` or a `links` declaration, so this is a field that named
108
- // a link component and gave it neither.
109
- if (field.kind === 'field' &&
110
- componentLinkExpansion(field.component) &&
111
- !field.doctype &&
112
- !links?.[field.fieldname]) {
113
- issues.push({
114
- severity: ValidationSeverity.ERROR,
115
- rule: 'link-component-without-doctype',
116
- message: `Field "${field.fieldname}" renders with "${field.component}", which is a link component, ` +
117
- `but has no \`doctype\` to name its target`,
118
- doctype,
119
- fieldname: field.fieldname,
120
- });
121
- }
122
- // Validate nested schemas recursively (fieldset only; table columns are ColumnSchema not DoctypeField)
123
- if (field.kind === 'fieldset') {
124
- issues.push(...this.validateRequiredProperties(doctype, field.schema, links));
125
- }
126
- }
127
- return issues;
128
- }
129
- /**
130
- * Validates Link field targets exist in registry
131
- * @internal
132
- */
133
- validateLinkFields(doctype, schema, registry) {
134
- const issues = [];
135
- for (const field of schema) {
136
- // `doctype` is both the link marker and its target, and the schema types it as a
137
- // non-empty string — so there is no "missing target" or "malformed target" state left
138
- // to check here. The rules that did (`link-missing-options`, `link-invalid-options`)
139
- // were removed with the `options`-as-link-target convention; the case they actually
140
- // guarded — a link component with no target — is now `link-component-without-doctype`,
141
- // which fires in validateRequiredProperties whether or not a registry is configured.
142
- if (field.kind === 'field' && field.doctype) {
143
- const targetDoctype = field.doctype;
144
- const targetMeta = registry.registry[targetDoctype] || registry.registry[targetDoctype.toLowerCase()];
145
- if (!targetMeta) {
146
- issues.push({
147
- severity: ValidationSeverity.ERROR,
148
- rule: 'link-invalid-target',
149
- message: `Link field "${field.fieldname}" references non-existent doctype: "${targetDoctype}"`,
150
- doctype,
151
- fieldname: field.fieldname,
152
- context: { targetDoctype },
153
- });
154
- }
155
- }
156
- // Recursively check nested fieldset schemas
157
- if (field.kind === 'fieldset') {
158
- issues.push(...this.validateLinkFields(doctype, field.schema, registry));
159
- }
160
- }
161
- return issues;
162
- }
163
- /**
164
- * Validates link declarations: target resolution, backlink consistency, Link field correspondence
165
- * @internal
166
- */
167
- validateLinkDeclarations(doctype, links, schema, registry) {
168
- const issues = [];
169
- // Build a map of link fields by fieldname for quick lookup
170
- const linkFieldsByFieldname = new Map();
171
- for (const field of schema) {
172
- if (field.kind === 'field' && field.doctype) {
173
- linkFieldsByFieldname.set(field.fieldname, field);
174
- }
175
- }
176
- for (const [fieldname, link] of Object.entries(links)) {
177
- // Check target resolves in registry
178
- const targetDoctype = registry.registry[link.target];
179
- if (!targetDoctype) {
180
- issues.push({
181
- severity: ValidationSeverity.ERROR,
182
- rule: 'link-invalid-target',
183
- message: `Link "${fieldname}" references non-existent doctype: "${link.target}"`,
184
- doctype,
185
- fieldname,
186
- context: { target: link.target },
187
- });
188
- continue;
189
- }
190
- // Warn on self-referential target
191
- if (link.target === doctype) {
192
- issues.push({
193
- severity: ValidationSeverity.WARNING,
194
- rule: 'link-self-referential',
195
- message: `Link "${fieldname}" is self-referential (target: "${link.target}")`,
196
- doctype,
197
- fieldname,
198
- context: { target: link.target },
199
- });
200
- }
201
- // Check backlink consistency
202
- if (link.backlink && targetDoctype.links) {
203
- const reciprocalLink = targetDoctype.links[link.backlink];
204
- if (!reciprocalLink) {
205
- issues.push({
206
- severity: ValidationSeverity.ERROR,
207
- rule: 'link-backlink-missing',
208
- message: `Backlink "${link.backlink}" not found on target doctype "${link.target}"`,
209
- doctype,
210
- fieldname,
211
- context: { backlink: link.backlink, target: link.target },
212
- });
213
- }
214
- else if (reciprocalLink.target !== doctype) {
215
- issues.push({
216
- severity: ValidationSeverity.WARNING,
217
- rule: 'link-backlink-mismatch',
218
- message: `Backlink "${link.backlink}" on "${link.target}" points to "${reciprocalLink.target}" instead of "${doctype}"`,
219
- doctype,
220
- fieldname,
221
- context: { backlink: link.backlink, target: link.target, actualTarget: reciprocalLink.target },
222
- });
223
- }
224
- }
225
- // If Link field exists with same fieldname, verify it has matching target
226
- // Only check if link has fieldname set (otherwise it's a standalone link without a field)
227
- if (link.fieldname) {
228
- const linkField = linkFieldsByFieldname.get(link.fieldname);
229
- if (linkField && linkField.kind === 'field') {
230
- // Map membership implies `doctype` is set — that is the map's build condition.
231
- const linkFieldTarget = linkField.doctype;
232
- if (linkFieldTarget && linkFieldTarget !== link.target) {
233
- issues.push({
234
- severity: ValidationSeverity.ERROR,
235
- rule: 'link-field-target-mismatch',
236
- message: `Link field "${link.fieldname}" targets "${linkFieldTarget}" but link declaration targets "${link.target}"`,
237
- doctype,
238
- fieldname: link.fieldname,
239
- context: { linkFieldTarget, linkTarget: link.target },
240
- });
241
- }
242
- }
243
- }
244
- }
245
- // NOTE: there is deliberately no "every link field needs a declaration" rule. A plain foreign
246
- // key is a link with no `links` entry — the map is additive, carrying expansion metadata
247
- // (backlink/fetch/cardinality), and is not required. Such a rule would contradict
248
- // `Registry.resolveFields`, which resolves undeclared links to inline pickers, and would flag
249
- // every doctype that mixes an expanded relation with a plain FK (e.g. `country`: a `links`
250
- // map for states/languages/subdivisions plus a plain `continent` picker).
251
- return issues;
252
- }
253
- /**
254
- * Validates workflow state machine configuration
255
- * @internal
256
- */
257
- validateWorkflow(doctype, workflow) {
258
- const issues = [];
259
- // Check for initial state
260
- if (!workflow.initial && !workflow.type) {
261
- issues.push({
262
- severity: ValidationSeverity.WARNING,
263
- rule: 'workflow-missing-initial',
264
- message: 'Workflow is missing initial state property',
265
- doctype,
266
- });
267
- }
268
- // Check for states
269
- if (!workflow.states || Object.keys(workflow.states).length === 0) {
270
- issues.push({
271
- severity: ValidationSeverity.WARNING,
272
- rule: 'workflow-no-states',
273
- message: 'Workflow has no states defined',
274
- doctype,
275
- });
276
- return issues;
277
- }
278
- // Validate initial state exists
279
- if (workflow.initial && typeof workflow.initial === 'string' && !workflow.states[workflow.initial]) {
280
- issues.push({
281
- severity: ValidationSeverity.ERROR,
282
- rule: 'workflow-invalid-initial',
283
- message: `Workflow initial state "${workflow.initial}" does not exist in states`,
284
- doctype,
285
- context: { initialState: workflow.initial },
286
- });
287
- }
288
- // Check state reachability (simple check - all states should have at least one incoming transition or be initial)
289
- const stateNames = Object.keys(workflow.states);
290
- const reachableStates = new Set();
291
- // Initial state is always reachable
292
- if (workflow.initial && typeof workflow.initial === 'string') {
293
- reachableStates.add(workflow.initial);
294
- }
295
- // Find all target states from transitions
296
- for (const [_stateName, stateConfig] of Object.entries(workflow.states)) {
297
- const state = stateConfig;
298
- if (state.on) {
299
- for (const [_event, transition] of Object.entries(state.on)) {
300
- if (typeof transition === 'string') {
301
- reachableStates.add(transition);
302
- }
303
- else if (transition && typeof transition === 'object') {
304
- // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- 'target' in transition guard confirms property; accessing unknown-typed target safely
305
- const target = 'target' in transition ? transition.target : undefined;
306
- if (typeof target === 'string') {
307
- reachableStates.add(target);
308
- }
309
- else if (Array.isArray(target)) {
310
- target.forEach((t) => {
311
- if (typeof t === 'string') {
312
- reachableStates.add(t);
313
- }
314
- });
315
- }
316
- }
317
- }
318
- }
319
- }
320
- // Check for unreachable states
321
- for (const stateName of stateNames) {
322
- if (!reachableStates.has(stateName)) {
323
- issues.push({
324
- severity: ValidationSeverity.WARNING,
325
- rule: 'workflow-unreachable-state',
326
- message: `Workflow state "${stateName}" may not be reachable`,
327
- doctype,
328
- context: { stateName },
329
- });
330
- }
331
- }
332
- return issues;
333
- }
334
- /**
335
- * Validates that actions are registered in the FieldTriggerEngine
336
- * @internal
337
- */
338
- validateActionRegistration(doctype, actions) {
339
- const issues = [];
340
- const triggerEngine = getGlobalTriggerEngine();
341
- for (const [triggerName, actionNames] of Object.entries(actions)) {
342
- if (!Array.isArray(actionNames)) {
343
- issues.push({
344
- severity: ValidationSeverity.ERROR,
345
- rule: 'action-invalid-format',
346
- message: `Action configuration for "${triggerName}" must be an array`,
347
- doctype,
348
- context: { triggerName, actionNames },
349
- });
350
- continue;
351
- }
352
- // Check each action name
353
- for (const actionName of actionNames) {
354
- // Check if action is registered globally
355
- // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- introspecting FieldTriggerEngine private fields for validation; no public API for this check
356
- const engine = triggerEngine;
357
- const isRegistered = engine.globalActions?.has(actionName) || engine.globalTransitionActions?.has(actionName);
358
- if (!isRegistered) {
359
- issues.push({
360
- severity: ValidationSeverity.WARNING,
361
- rule: 'action-not-registered',
362
- message: `Action "${actionName}" referenced in "${triggerName}" is not registered in FieldTriggerEngine`,
363
- doctype,
364
- context: { triggerName, actionName },
365
- });
366
- }
367
- }
368
- }
369
- return issues;
370
- }
371
- }
372
- /**
373
- * Creates a validator with the given registry
374
- * @param registry - Registry instance
375
- * @param options - Additional validator options
376
- * @returns SchemaValidator instance
377
- * @public
378
- */
379
- export function createValidator(registry, options) {
380
- return new SchemaValidator({
381
- registry,
382
- ...options,
383
- });
384
- }
385
- /**
386
- * Quick validation helper
387
- * @param doctype - Doctype name
388
- * @param schema - Schema fields
389
- * @param registry - Registry instance
390
- * @param workflow - Optional workflow configuration
391
- * @param actions - Optional actions map
392
- * @returns Validation result
393
- * @public
394
- */
395
- export function validateSchema(doctype, schema, registry, workflow, actions) {
396
- const validator = createValidator(registry);
397
- return validator.validate(doctype, schema, workflow, actions);
398
- }