@stonecrop/stonecrop 0.13.14 → 0.14.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/README.md +6 -3
- package/dist/probe.tsbuildinfo +1 -0
- package/dist/src/doctype.d.ts +42 -7
- package/dist/src/doctype.d.ts.map +1 -1
- package/dist/src/doctype.js +77 -18
- package/dist/src/index.d.ts +5 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +6 -0
- package/dist/src/registry.d.ts +1 -1
- package/dist/src/registry.d.ts.map +1 -1
- package/dist/src/registry.js +46 -38
- package/dist/src/schema-validator.d.ts.map +1 -1
- package/dist/src/schema-validator.js +44 -47
- package/dist/src/stonecrop.d.ts +0 -6
- package/dist/src/stonecrop.d.ts.map +1 -1
- package/dist/src/stores/index.d.ts +2 -0
- package/dist/src/stores/index.d.ts.map +1 -1
- package/dist/src/stores/index.js +1 -0
- package/dist/src/stores/operation-log.d.ts +0 -6
- package/dist/src/stores/operation-log.d.ts.map +1 -1
- package/dist/src/stores/operation-log.js +0 -1
- package/dist/src/stores/validation.d.ts +83 -0
- package/dist/src/stores/validation.d.ts.map +1 -0
- package/dist/src/stores/validation.js +104 -0
- package/dist/src/types/doctype.d.ts +1 -1
- package/dist/src/types/doctype.d.ts.map +1 -1
- package/dist/src/types/operation-log.d.ts +0 -2
- package/dist/src/types/operation-log.d.ts.map +1 -1
- package/dist/stonecrop.d.ts +166 -22
- package/dist/stonecrop.js +5235 -1950
- package/dist/stonecrop.js.map +1 -1
- package/package.json +4 -4
- package/src/client-handler.ts +55 -0
- package/src/doctype.ts +83 -22
- package/src/index.ts +12 -0
- package/src/registry.ts +45 -40
- package/src/schema-validator.ts +52 -48
- package/src/stores/index.ts +2 -0
- package/src/stores/operation-log.ts +0 -1
- package/src/stores/validation.ts +138 -0
- package/src/types/doctype.ts +1 -1
- package/src/types/operation-log.ts +0 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stonecrop/stonecrop",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"author": {
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"pinia-shared-state": "^1.0.1",
|
|
35
35
|
"pinia-xstate": "^3.0.0",
|
|
36
36
|
"xstate": "^5.25.0",
|
|
37
|
-
"@stonecrop/schema": "0.
|
|
37
|
+
"@stonecrop/schema": "0.14.0"
|
|
38
38
|
},
|
|
39
39
|
"peerDependencies": {
|
|
40
40
|
"pinia": "^3.0.4",
|
|
@@ -56,8 +56,8 @@
|
|
|
56
56
|
"vitest": "^4.1.5",
|
|
57
57
|
"vue": "^3.5.33",
|
|
58
58
|
"vue-router": "^5.0.6",
|
|
59
|
-
"@stonecrop/
|
|
60
|
-
"@stonecrop/
|
|
59
|
+
"@stonecrop/atable": "0.14.0",
|
|
60
|
+
"@stonecrop/aform": "0.14.0",
|
|
61
61
|
"stonecrop-rig": "0.7.0"
|
|
62
62
|
},
|
|
63
63
|
"description": "Schema-driven framework with XState workflows and HST state management",
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* clientHandler execution
|
|
3
|
+
* Pure executor for docbuilder-authored client handlers.
|
|
4
|
+
* @packageDocumentation
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Named capabilities injected into a clientHandler body as function parameters.
|
|
9
|
+
* The caller (the assembly composable) owns what each name resolves to — typically
|
|
10
|
+
* `router`, `record`, `runAction`, and a read-only `graphql`.
|
|
11
|
+
*
|
|
12
|
+
* @public
|
|
13
|
+
*/
|
|
14
|
+
export type ClientHandlerApi = Record<string, unknown>
|
|
15
|
+
|
|
16
|
+
// AsyncFunction is not a global; reach it via an async function's constructor so authored
|
|
17
|
+
// bodies can use top-level `await` without writing an async wrapper.
|
|
18
|
+
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- an async function's constructor is always the AsyncFunction constructor
|
|
19
|
+
const AsyncFunction = Object.getPrototypeOf(async () => {}).constructor as new (
|
|
20
|
+
...args: string[]
|
|
21
|
+
) => (...args: unknown[]) => Promise<unknown>
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Execute a docbuilder-authored `clientHandler` body against an injected API map.
|
|
25
|
+
*
|
|
26
|
+
* `code` is a function *body* (statements), not a full function — authored in the
|
|
27
|
+
* docbuilder code editor and stored on `ActionDefinition.clientHandler`. It is compiled
|
|
28
|
+
* with the AsyncFunction constructor, so `await` works directly. Each key of `api`
|
|
29
|
+
* becomes a parameter name bound to its value, so a handler can reference `router`,
|
|
30
|
+
* `record`, `runAction`, etc. by name.
|
|
31
|
+
*
|
|
32
|
+
* This executor is deliberately concern-free: it performs no routing, dispatch, or HST
|
|
33
|
+
* writes itself — the caller assembles `api`. The injected set is an *intent* contract,
|
|
34
|
+
* not a sandbox (an AsyncFunction body can still reach `fetch`/`window`); enforcement of
|
|
35
|
+
* what a handler may actually do lives server-side, not here.
|
|
36
|
+
*
|
|
37
|
+
* Errors are propagated to the caller as a rejected promise — syntax errors at compile
|
|
38
|
+
* time and thrown errors / rejections at run time — so callers handle both uniformly.
|
|
39
|
+
*
|
|
40
|
+
* @param code - the clientHandler body, e.g. `"router.push('/users')"`
|
|
41
|
+
* @param api - named capabilities injected as parameters (default: none)
|
|
42
|
+
* @returns whatever the body returns (typically `void`)
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```ts
|
|
46
|
+
* await executeClientHandler("await runAction('Assign', [record.id])", { runAction, record })
|
|
47
|
+
* ```
|
|
48
|
+
*
|
|
49
|
+
* @public
|
|
50
|
+
*/
|
|
51
|
+
export async function executeClientHandler(code: string, api: ClientHandlerApi = {}): Promise<unknown> {
|
|
52
|
+
const names = Object.keys(api)
|
|
53
|
+
const fn = new AsyncFunction(...names, code)
|
|
54
|
+
return fn(...names.map(name => api[name]))
|
|
55
|
+
}
|
package/src/doctype.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type { DoctypeField, LinkDeclaration, WorkflowMeta } from '@stonecrop/schema'
|
|
1
|
+
import type { DoctypeField, LinkDeclaration, TriggerDefinition, WorkflowMeta } from '@stonecrop/schema'
|
|
2
|
+
import { isActionAllowedInState, normalizeFieldKind } from '@stonecrop/schema'
|
|
2
3
|
import { List, Map } from 'immutable'
|
|
3
4
|
import { Component } from 'vue'
|
|
4
5
|
|
|
@@ -110,8 +111,8 @@ export default class Doctype {
|
|
|
110
111
|
* const planDoctype = Doctype.fromObject({
|
|
111
112
|
* name: 'Plan',
|
|
112
113
|
* fields: [
|
|
113
|
-
* { fieldname: 'title', label: 'Title',
|
|
114
|
-
* { fieldname: 'status', label: 'Status',
|
|
114
|
+
* { fieldname: 'title', label: 'Title', component: 'ATextInput' },
|
|
115
|
+
* { fieldname: 'status', label: 'Status', component: 'ATextInput' },
|
|
115
116
|
* ],
|
|
116
117
|
* workflow: {
|
|
117
118
|
* id: 'plan',
|
|
@@ -124,7 +125,13 @@ export default class Doctype {
|
|
|
124
125
|
* @public
|
|
125
126
|
*/
|
|
126
127
|
static fromObject(config: DoctypeConfig): Doctype {
|
|
127
|
-
|
|
128
|
+
// Authored JSON may omit the `kind` discriminant; the Zod schema injects it via a
|
|
129
|
+
// preprocess, but this path bypasses Zod. Normalize here so the registry's
|
|
130
|
+
// `resolveFields` (which gates link/fieldset handling on `field.kind`) sees the same
|
|
131
|
+
// shape a parsed doctype would — otherwise links silently fail to expand.
|
|
132
|
+
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- normalizeFieldKind only injects the `kind` discriminant; the field shape is otherwise preserved
|
|
133
|
+
const fields = config.fields?.map(normalizeFieldKind) as DoctypeField[] | undefined
|
|
134
|
+
const schema = fields ? List(fields) : List<DoctypeField>()
|
|
128
135
|
const actions = config.actions ? Map(config.actions) : Map<string, string[]>()
|
|
129
136
|
|
|
130
137
|
return new Doctype(config.name, schema, config.workflow, actions, undefined, config.links)
|
|
@@ -192,18 +199,16 @@ export default class Doctype {
|
|
|
192
199
|
const actions = (workflow as WorkflowMeta).actions
|
|
193
200
|
if (!actions) return []
|
|
194
201
|
|
|
195
|
-
return
|
|
196
|
-
.
|
|
197
|
-
|
|
198
|
-
//
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
targetState: currentState,
|
|
206
|
-
}))
|
|
202
|
+
return (
|
|
203
|
+
Object.entries(actions)
|
|
204
|
+
// Exclude stateless Commands — they change no state and are surfaced separately
|
|
205
|
+
// via getAvailableCommands(). Genuine transitions carry a nextState.
|
|
206
|
+
.filter(([, actionDef]) => !actionDef.stateless && isActionAllowedInState(actionDef, currentState))
|
|
207
|
+
.map(([name, actionDef]) => ({
|
|
208
|
+
name,
|
|
209
|
+
targetState: actionDef.nextState ?? currentState,
|
|
210
|
+
}))
|
|
211
|
+
)
|
|
207
212
|
}
|
|
208
213
|
|
|
209
214
|
// XState format: use the on property of the state
|
|
@@ -217,6 +222,63 @@ export default class Doctype {
|
|
|
217
222
|
}))
|
|
218
223
|
}
|
|
219
224
|
|
|
225
|
+
/**
|
|
226
|
+
* Returns the stateless **Commands** available in a given workflow state — side-effect
|
|
227
|
+
* actions (save/print/email…) that do not change workflow state. Unlike transitions,
|
|
228
|
+
* Commands may exist on a workflow that declares no `states` (a commands-only doctype),
|
|
229
|
+
* and a Command with no `allowedStates` is available in every state.
|
|
230
|
+
*
|
|
231
|
+
* Only meaningful for WorkflowMeta format; XState workflows have no Commands.
|
|
232
|
+
*
|
|
233
|
+
* @param currentState - The record's current state, used to honor a Command's `allowedStates`
|
|
234
|
+
* @returns Array of command descriptors with `name`
|
|
235
|
+
*
|
|
236
|
+
* @example
|
|
237
|
+
* ```ts
|
|
238
|
+
* const commands = doctype.getAvailableCommands('Draft')
|
|
239
|
+
* // [{ name: 'Print' }]
|
|
240
|
+
* ```
|
|
241
|
+
*
|
|
242
|
+
* @public
|
|
243
|
+
*/
|
|
244
|
+
getAvailableCommands(currentState?: string): Array<{ name: string }> {
|
|
245
|
+
const workflow = this.workflow
|
|
246
|
+
if (!workflow) return []
|
|
247
|
+
|
|
248
|
+
// Only WorkflowMeta carries an `actions` map; XState workflows have no Commands.
|
|
249
|
+
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- probing the workflow union for the optional WorkflowMeta actions map
|
|
250
|
+
const actions = (workflow as WorkflowMeta).actions
|
|
251
|
+
if (!actions) return []
|
|
252
|
+
|
|
253
|
+
return Object.entries(actions)
|
|
254
|
+
.filter(([, actionDef]) => actionDef.stateless === true && isActionAllowedInState(actionDef, currentState ?? ''))
|
|
255
|
+
.map(([name]) => ({ name }))
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Returns the field-validation **triggers** declared on this doctype's workflow (advisory,
|
|
260
|
+
* client-side). Keyed by trigger name. Returns undefined when the workflow is absent, is an
|
|
261
|
+
* XState machine (no triggers), or simply declares none.
|
|
262
|
+
*
|
|
263
|
+
* @returns The `workflow.triggers` map, or undefined
|
|
264
|
+
*
|
|
265
|
+
* @example
|
|
266
|
+
* ```ts
|
|
267
|
+
* const triggers = doctype.getTriggers()
|
|
268
|
+
* // { dateOrder: { on: ['start_date', 'end_date'], clientHandler: '…' } }
|
|
269
|
+
* ```
|
|
270
|
+
*
|
|
271
|
+
* @public
|
|
272
|
+
*/
|
|
273
|
+
getTriggers(): Record<string, TriggerDefinition> | undefined {
|
|
274
|
+
const workflow = this.workflow
|
|
275
|
+
if (!workflow) return undefined
|
|
276
|
+
|
|
277
|
+
// Only WorkflowMeta carries a `triggers` map; XState workflows have none.
|
|
278
|
+
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- probing the workflow union for the optional WorkflowMeta triggers map
|
|
279
|
+
return (workflow as WorkflowMeta).triggers
|
|
280
|
+
}
|
|
281
|
+
|
|
220
282
|
/**
|
|
221
283
|
* Returns metadata for a specific action, if available.
|
|
222
284
|
* Only works with WorkflowMeta format; returns undefined for XState format.
|
|
@@ -227,7 +289,7 @@ export default class Doctype {
|
|
|
227
289
|
* @example
|
|
228
290
|
* ```ts
|
|
229
291
|
* const actionMeta = doctype.getActionMeta('submit')
|
|
230
|
-
* // { label: 'Submit',
|
|
292
|
+
* // { label: 'Submit', allowedStates: ['draft'] }
|
|
231
293
|
* ```
|
|
232
294
|
*
|
|
233
295
|
* @public
|
|
@@ -235,17 +297,16 @@ export default class Doctype {
|
|
|
235
297
|
getActionMeta(actionName: string):
|
|
236
298
|
| {
|
|
237
299
|
label: string
|
|
238
|
-
handler: string
|
|
239
300
|
requiredFields?: string[]
|
|
240
301
|
allowedStates?: string[]
|
|
241
|
-
confirm?: boolean
|
|
242
|
-
args?: Record<string, unknown>
|
|
243
302
|
}
|
|
244
303
|
| undefined {
|
|
245
304
|
const workflow = this.workflow
|
|
246
|
-
if (!workflow
|
|
305
|
+
if (!workflow) return undefined
|
|
247
306
|
|
|
248
|
-
//
|
|
307
|
+
// Only WorkflowMeta carries an `actions` map (with labels); XState workflows have none.
|
|
308
|
+
// Don't require a `states` array — a commands-only doctype has labeled actions but no states.
|
|
309
|
+
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- probing the workflow union for the optional WorkflowMeta actions map
|
|
249
310
|
const actions = (workflow as WorkflowMeta).actions
|
|
250
311
|
return actions?.[actionName]
|
|
251
312
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export type * from '@stonecrop/aform/types'
|
|
2
2
|
export type * from '@stonecrop/atable/types'
|
|
3
3
|
|
|
4
|
+
import { executeClientHandler } from './client-handler'
|
|
4
5
|
import { useLazyLink } from './composables/lazy-link'
|
|
5
6
|
import { useStonecrop } from './composables/stonecrop'
|
|
6
7
|
import { useOperationLog, useUndoRedoShortcuts, withBatch } from './composables/operation-log'
|
|
@@ -19,12 +20,19 @@ import Registry from './registry'
|
|
|
19
20
|
import { Stonecrop, getStonecrop } from './stonecrop'
|
|
20
21
|
import { HST, createHST, type HSTNode } from './stores/hst'
|
|
21
22
|
import { useOperationLogStore } from './stores/operation-log'
|
|
23
|
+
import { useValidationStore } from './stores/validation'
|
|
22
24
|
import { SchemaValidator, createValidator, validateSchema } from './schema-validator'
|
|
23
25
|
import { ValidationSeverity } from './types/schema-validator'
|
|
24
26
|
|
|
25
27
|
// Export all types from ./types
|
|
26
28
|
export type * from './types'
|
|
27
29
|
|
|
30
|
+
// clientHandler executor types
|
|
31
|
+
export type { ClientHandlerApi } from './client-handler'
|
|
32
|
+
|
|
33
|
+
// Field-validation store types
|
|
34
|
+
export type { ValidationError } from './stores/validation'
|
|
35
|
+
|
|
28
36
|
// Export enum as value (enums need runtime export, not just type)
|
|
29
37
|
export { ValidationSeverity }
|
|
30
38
|
|
|
@@ -34,6 +42,8 @@ export {
|
|
|
34
42
|
Stonecrop,
|
|
35
43
|
useLazyLink,
|
|
36
44
|
useStonecrop,
|
|
45
|
+
// clientHandler executor
|
|
46
|
+
executeClientHandler,
|
|
37
47
|
// HST exports for advanced usage
|
|
38
48
|
HST,
|
|
39
49
|
createHST,
|
|
@@ -55,6 +65,8 @@ export {
|
|
|
55
65
|
useOperationLogStore,
|
|
56
66
|
useUndoRedoShortcuts,
|
|
57
67
|
withBatch,
|
|
68
|
+
// Field-validation store
|
|
69
|
+
useValidationStore,
|
|
58
70
|
// Utility functions
|
|
59
71
|
getStonecrop,
|
|
60
72
|
}
|
package/src/registry.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { ResolvedField, ResolvedLink, ResolvedScalar, ResolvedTable, ResolvedFieldset } from '@stonecrop/aform'
|
|
2
2
|
import type { ColumnSchema, DoctypeField, LinkDeclaration, TableViewConfig, ValueField } from '@stonecrop/schema'
|
|
3
|
+
import { componentCategory, componentLinkExpansion, resolveLinkRenderMode } from '@stonecrop/schema'
|
|
3
4
|
import { Router } from 'vue-router'
|
|
4
5
|
|
|
5
6
|
import Doctype from './doctype'
|
|
@@ -165,23 +166,22 @@ export default class Registry {
|
|
|
165
166
|
const resolved: ResolvedField[] = []
|
|
166
167
|
|
|
167
168
|
for (const field of fields) {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
}
|
|
169
|
+
// A field is a link when it carries `doctype` (the marker) or has a LinkDeclaration.
|
|
170
|
+
const linkDecl = field.kind === 'field' ? links.get(field.fieldname) : undefined
|
|
171
|
+
if (field.kind === 'field' && (linkDecl || field.doctype)) {
|
|
172
|
+
const link = linkDecl
|
|
173
|
+
// The declaration's target wins over the field's own `doctype`.
|
|
174
|
+
const linkTarget = link?.target ?? field.doctype
|
|
175
|
+
|
|
176
|
+
// An undeclared link, or a declared one whose component renders an inline picker,
|
|
177
|
+
// stays a scalar: the target is not expanded, it only needs the slug for async
|
|
178
|
+
// display-text resolution and navigation.
|
|
179
|
+
if (!link || resolveLinkRenderMode(link, field.component) === 'inline') {
|
|
180
180
|
const { cardinality: _c, ...rest } = field
|
|
181
181
|
resolved.push({
|
|
182
182
|
...rest,
|
|
183
183
|
component: rest.component || 'AFormLink',
|
|
184
|
-
...(
|
|
184
|
+
...(linkTarget !== undefined ? { doctype: linkTarget } : {}),
|
|
185
185
|
})
|
|
186
186
|
continue
|
|
187
187
|
}
|
|
@@ -195,9 +195,9 @@ export default class Registry {
|
|
|
195
195
|
}
|
|
196
196
|
|
|
197
197
|
const childSchema = this.resolveSchema(targetDoctype, new Set(visited))
|
|
198
|
-
const {
|
|
198
|
+
const { options: _opt, cardinality: _card, kind: _kind, ...fieldRest } = field
|
|
199
199
|
|
|
200
|
-
if (link
|
|
200
|
+
if (resolveLinkRenderMode(link, field.component) === 'table') {
|
|
201
201
|
resolved.push(this.buildTableConfig(field, childSchema, link.component))
|
|
202
202
|
} else {
|
|
203
203
|
const linkEntry: ResolvedLink = {
|
|
@@ -229,7 +229,18 @@ export default class Registry {
|
|
|
229
229
|
}
|
|
230
230
|
resolved.push(tableEntry)
|
|
231
231
|
} else {
|
|
232
|
-
// Scalar field (kind: 'field', not a
|
|
232
|
+
// Scalar field (kind: 'field', not a link) — strip cardinality.
|
|
233
|
+
// A link-rendering component with nothing to point at renders an empty picker (or an
|
|
234
|
+
// empty embedded form), which is indistinguishable from "the record has no value".
|
|
235
|
+
// Link-ness comes from `doctype`/`links`, so this is a field that named a link
|
|
236
|
+
// component and forgot the target.
|
|
237
|
+
if (field.kind === 'field' && componentLinkExpansion(field.component)) {
|
|
238
|
+
console.warn(
|
|
239
|
+
`[Stonecrop] Field "${field.fieldname}" renders with "${field.component}", which is a link component, ` +
|
|
240
|
+
`but the field is not a link: it has no \`doctype\` and no \`links\` declaration. It will render ` +
|
|
241
|
+
`as a plain field. Add \`"doctype": "<doctype-slug>"\` to the field definition.`
|
|
242
|
+
)
|
|
243
|
+
}
|
|
233
244
|
const { cardinality: _c, ...rest } = field
|
|
234
245
|
resolved.push({ ...rest })
|
|
235
246
|
}
|
|
@@ -250,7 +261,7 @@ export default class Registry {
|
|
|
250
261
|
.map(({ kind: _k, ...col }) => col)
|
|
251
262
|
|
|
252
263
|
const config: TableViewConfig = (field as ValueField & { config?: TableViewConfig }).config ?? { view: 'list' }
|
|
253
|
-
const {
|
|
264
|
+
const { options: _opt, cardinality: _card, ...fieldRest } = field
|
|
254
265
|
|
|
255
266
|
return {
|
|
256
267
|
...fieldRest,
|
|
@@ -268,7 +279,7 @@ export default class Registry {
|
|
|
268
279
|
*
|
|
269
280
|
* - `kind: 'table'` or `kind: 'link'` → `[]` or `{}`
|
|
270
281
|
* - `kind: 'fieldset'` → recursively initializes children as `{}`
|
|
271
|
-
* - `kind: 'field'` → derives default from
|
|
282
|
+
* - `kind: 'field'` → derives the default from the component's category; falls back to `null`
|
|
272
283
|
*
|
|
273
284
|
* @param schema - The resolved schema array to derive defaults from
|
|
274
285
|
* @returns A plain object with default values for each field
|
|
@@ -285,32 +296,26 @@ export default class Registry {
|
|
|
285
296
|
} else if (field.kind === 'fieldset') {
|
|
286
297
|
record[field.fieldname] = this.initializeRecord(field.schema)
|
|
287
298
|
} else {
|
|
288
|
-
// kind: 'field' —
|
|
299
|
+
// kind: 'field' — the empty default comes from the component's category.
|
|
289
300
|
const fieldDefault = field.default
|
|
290
301
|
if (fieldDefault !== undefined) {
|
|
291
302
|
record[field.fieldname] = fieldDefault
|
|
292
303
|
} else {
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
break
|
|
309
|
-
case 'JSON':
|
|
310
|
-
record[field.fieldname] = {}
|
|
311
|
-
break
|
|
312
|
-
default:
|
|
313
|
-
record[field.fieldname] = null
|
|
304
|
+
const category = componentCategory(field.component)
|
|
305
|
+
if (category === 'text') {
|
|
306
|
+
record[field.fieldname] = ''
|
|
307
|
+
} else if (category === 'number') {
|
|
308
|
+
record[field.fieldname] = 0
|
|
309
|
+
} else if (category === 'boolean') {
|
|
310
|
+
record[field.fieldname] = false
|
|
311
|
+
} else if (category === 'code' && field.language) {
|
|
312
|
+
// A JSON editor starts from an empty object; any other language from empty source.
|
|
313
|
+
record[field.fieldname] = field.language === 'json' ? {} : ''
|
|
314
|
+
} else {
|
|
315
|
+
// date / datetime / select / link / attach — plus two cases with no better answer
|
|
316
|
+
// than "no value": an unknown (custom) component, and a code field whose missing
|
|
317
|
+
// `language` doesn't say which kind of empty it wants.
|
|
318
|
+
record[field.fieldname] = null
|
|
314
319
|
}
|
|
315
320
|
}
|
|
316
321
|
}
|
package/src/schema-validator.ts
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
import type { DoctypeField } from '@stonecrop/schema'
|
|
8
8
|
import type { LinkDeclaration } from '@stonecrop/schema'
|
|
9
|
+
import { componentLinkExpansion } from '@stonecrop/schema'
|
|
9
10
|
import type { List, Map as ImmutableMap } from 'immutable'
|
|
10
11
|
import type { AnyStateNodeConfig } from 'xstate'
|
|
11
12
|
|
|
@@ -59,7 +60,7 @@ export class SchemaValidator {
|
|
|
59
60
|
|
|
60
61
|
// Validate required properties
|
|
61
62
|
if (this.options.validateRequiredProperties) {
|
|
62
|
-
issues.push(...this.validateRequiredProperties(doctype, schemaArray))
|
|
63
|
+
issues.push(...this.validateRequiredProperties(doctype, schemaArray, links))
|
|
63
64
|
}
|
|
64
65
|
|
|
65
66
|
// Validate Link field targets
|
|
@@ -102,7 +103,11 @@ export class SchemaValidator {
|
|
|
102
103
|
* Validates that required schema properties are present
|
|
103
104
|
* @internal
|
|
104
105
|
*/
|
|
105
|
-
private validateRequiredProperties(
|
|
106
|
+
private validateRequiredProperties(
|
|
107
|
+
doctype: string,
|
|
108
|
+
schema: DoctypeField[],
|
|
109
|
+
links?: Record<string, LinkDeclaration>
|
|
110
|
+
): ValidationIssue[] {
|
|
106
111
|
const issues: ValidationIssue[] = []
|
|
107
112
|
|
|
108
113
|
for (const field of schema) {
|
|
@@ -118,12 +123,34 @@ export class SchemaValidator {
|
|
|
118
123
|
continue
|
|
119
124
|
}
|
|
120
125
|
|
|
121
|
-
//
|
|
122
|
-
|
|
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
|
+
) {
|
|
123
148
|
issues.push({
|
|
124
149
|
severity: ValidationSeverity.ERROR,
|
|
125
|
-
rule: '
|
|
126
|
-
message:
|
|
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`,
|
|
127
154
|
doctype,
|
|
128
155
|
fieldname: field.fieldname,
|
|
129
156
|
})
|
|
@@ -131,7 +158,7 @@ export class SchemaValidator {
|
|
|
131
158
|
|
|
132
159
|
// Validate nested schemas recursively (fieldset only; table columns are ColumnSchema not DoctypeField)
|
|
133
160
|
if (field.kind === 'fieldset') {
|
|
134
|
-
issues.push(...this.validateRequiredProperties(doctype, field.schema))
|
|
161
|
+
issues.push(...this.validateRequiredProperties(doctype, field.schema, links))
|
|
135
162
|
}
|
|
136
163
|
}
|
|
137
164
|
|
|
@@ -146,30 +173,14 @@ export class SchemaValidator {
|
|
|
146
173
|
const issues: ValidationIssue[] = []
|
|
147
174
|
|
|
148
175
|
for (const field of schema) {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
fieldname: field.fieldname,
|
|
158
|
-
})
|
|
159
|
-
continue
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
const targetDoctype = typeof options === 'string' ? options : ''
|
|
163
|
-
if (!targetDoctype) {
|
|
164
|
-
issues.push({
|
|
165
|
-
severity: ValidationSeverity.ERROR,
|
|
166
|
-
rule: 'link-invalid-options',
|
|
167
|
-
message: `Link field "${field.fieldname}" has invalid options format (expected string doctype name)`,
|
|
168
|
-
doctype,
|
|
169
|
-
fieldname: field.fieldname,
|
|
170
|
-
})
|
|
171
|
-
continue
|
|
172
|
-
}
|
|
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
|
|
173
184
|
const targetMeta = registry.registry[targetDoctype] || registry.registry[targetDoctype.toLowerCase()]
|
|
174
185
|
|
|
175
186
|
if (!targetMeta) {
|
|
@@ -205,10 +216,10 @@ export class SchemaValidator {
|
|
|
205
216
|
): ValidationIssue[] {
|
|
206
217
|
const issues: ValidationIssue[] = []
|
|
207
218
|
|
|
208
|
-
// Build a map of
|
|
219
|
+
// Build a map of link fields by fieldname for quick lookup
|
|
209
220
|
const linkFieldsByFieldname = new Map<string, DoctypeField>()
|
|
210
221
|
for (const field of schema) {
|
|
211
|
-
if (field.kind === 'field' && field.
|
|
222
|
+
if (field.kind === 'field' && field.doctype) {
|
|
212
223
|
linkFieldsByFieldname.set(field.fieldname, field)
|
|
213
224
|
}
|
|
214
225
|
}
|
|
@@ -269,7 +280,8 @@ export class SchemaValidator {
|
|
|
269
280
|
if (link.fieldname) {
|
|
270
281
|
const linkField = linkFieldsByFieldname.get(link.fieldname)
|
|
271
282
|
if (linkField && linkField.kind === 'field') {
|
|
272
|
-
|
|
283
|
+
// Map membership implies `doctype` is set — that is the map's build condition.
|
|
284
|
+
const linkFieldTarget = linkField.doctype
|
|
273
285
|
if (linkFieldTarget && linkFieldTarget !== link.target) {
|
|
274
286
|
issues.push({
|
|
275
287
|
severity: ValidationSeverity.ERROR,
|
|
@@ -284,20 +296,12 @@ export class SchemaValidator {
|
|
|
284
296
|
}
|
|
285
297
|
}
|
|
286
298
|
|
|
287
|
-
//
|
|
288
|
-
//
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
severity: ValidationSeverity.ERROR,
|
|
294
|
-
rule: 'link-field-without-declaration',
|
|
295
|
-
message: `Link field "${fieldname}" has no corresponding link declaration`,
|
|
296
|
-
doctype,
|
|
297
|
-
fieldname,
|
|
298
|
-
})
|
|
299
|
-
}
|
|
300
|
-
}
|
|
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).
|
|
301
305
|
|
|
302
306
|
return issues
|
|
303
307
|
}
|
package/src/stores/index.ts
CHANGED
|
@@ -116,7 +116,6 @@ export const useOperationLogStore = defineStore('hst-operation-log', () => {
|
|
|
116
116
|
const config = ref<OperationLogConfig>({
|
|
117
117
|
maxOperations: 100,
|
|
118
118
|
enableCrossTabSync: true,
|
|
119
|
-
autoSyncInterval: 30000,
|
|
120
119
|
enablePersistence: false,
|
|
121
120
|
persistenceKeyPrefix: 'stonecrop-ops',
|
|
122
121
|
})
|