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