@stonecrop/stonecrop 0.13.12 → 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.
Files changed (45) hide show
  1. package/README.md +6 -3
  2. package/dist/probe.tsbuildinfo +1 -0
  3. package/dist/src/client-handler.d.ts +43 -0
  4. package/dist/src/client-handler.d.ts.map +1 -0
  5. package/dist/src/client-handler.js +42 -0
  6. package/dist/src/doctype.d.ts +42 -7
  7. package/dist/src/doctype.d.ts.map +1 -1
  8. package/dist/src/doctype.js +77 -18
  9. package/dist/src/index.d.ts +5 -1
  10. package/dist/src/index.d.ts.map +1 -1
  11. package/dist/src/index.js +6 -0
  12. package/dist/src/registry.d.ts +1 -1
  13. package/dist/src/registry.d.ts.map +1 -1
  14. package/dist/src/registry.js +46 -38
  15. package/dist/src/schema-validator.d.ts.map +1 -1
  16. package/dist/src/schema-validator.js +44 -47
  17. package/dist/src/stonecrop.d.ts +0 -6
  18. package/dist/src/stonecrop.d.ts.map +1 -1
  19. package/dist/src/stores/index.d.ts +2 -0
  20. package/dist/src/stores/index.d.ts.map +1 -1
  21. package/dist/src/stores/index.js +1 -0
  22. package/dist/src/stores/operation-log.d.ts +0 -6
  23. package/dist/src/stores/operation-log.d.ts.map +1 -1
  24. package/dist/src/stores/operation-log.js +0 -1
  25. package/dist/src/stores/validation.d.ts +83 -0
  26. package/dist/src/stores/validation.d.ts.map +1 -0
  27. package/dist/src/stores/validation.js +104 -0
  28. package/dist/src/types/doctype.d.ts +1 -1
  29. package/dist/src/types/doctype.d.ts.map +1 -1
  30. package/dist/src/types/operation-log.d.ts +0 -2
  31. package/dist/src/types/operation-log.d.ts.map +1 -1
  32. package/dist/stonecrop.d.ts +166 -22
  33. package/dist/stonecrop.js +5241 -1958
  34. package/dist/stonecrop.js.map +1 -1
  35. package/package.json +4 -4
  36. package/src/client-handler.ts +55 -0
  37. package/src/doctype.ts +83 -22
  38. package/src/index.ts +12 -0
  39. package/src/registry.ts +45 -40
  40. package/src/schema-validator.ts +52 -48
  41. package/src/stores/index.ts +2 -0
  42. package/src/stores/operation-log.ts +7 -10
  43. package/src/stores/validation.ts +138 -0
  44. package/src/types/doctype.ts +1 -1
  45. package/src/types/operation-log.ts +0 -3
@@ -0,0 +1,138 @@
1
+ import { defineStore } from 'pinia'
2
+ import { ref, computed } from 'vue'
3
+
4
+ import type { TriggerDefinition } from '@stonecrop/schema'
5
+ import { executeClientHandler } from '../client-handler'
6
+
7
+ /**
8
+ * A single validation error contributed by a trigger, displayed on a field.
9
+ * @public
10
+ */
11
+ export interface ValidationError {
12
+ /** The trigger that produced this error — the namespace a re-run clears before repopulating */
13
+ trigger: string
14
+ /** The fieldname the error displays on (the `setError` target, not necessarily a fired field) */
15
+ field: string
16
+ /** The message to display */
17
+ message: string
18
+ }
19
+
20
+ /**
21
+ * Reactive per-field validation error store + the advisory field-validation trigger engine.
22
+ *
23
+ * Holds the errors produced by field-validation triggers (see `TriggerDefinition` in
24
+ * `@stonecrop/schema`) and runs a trigger's `clientHandler` on demand. Errors are **namespaced
25
+ * by trigger**: re-running a trigger clears its own prior contributions before repopulating, so a
26
+ * corrected value clears its stale error without disturbing other triggers.
27
+ *
28
+ * The engine is **advisory** and does **no rollback** — an invalid value stays in the record so the
29
+ * user can fix it; validity is reported separately via `isValid` (read by the save gate) and
30
+ * the per-field messages are surfaced via `errorsByField` / `errorsFor` for display.
31
+ *
32
+ * @public
33
+ */
34
+ export const useValidationStore = defineStore('stonecrop-validation', () => {
35
+ const errors = ref<ValidationError[]>([])
36
+
37
+ /** True when no field currently carries a validation error. */
38
+ const isValid = computed(() => errors.value.length === 0)
39
+
40
+ /** Errors grouped by field — the shape a renderer consumes (e.g. AForm `:errors`). */
41
+ const errorsByField = computed<Record<string, string[]>>(() => {
42
+ const map: Record<string, string[]> = {}
43
+ for (const e of errors.value) {
44
+ if (!map[e.field]) map[e.field] = []
45
+ map[e.field].push(e.message)
46
+ }
47
+ return map
48
+ })
49
+
50
+ /** Messages currently displayed on `field`. */
51
+ function errorsFor(field: string): string[] {
52
+ return errors.value.filter(e => e.field === field).map(e => e.message)
53
+ }
54
+
55
+ /** Record an error contributed by `trigger`, displayed on `field`. */
56
+ function setError(trigger: string, field: string, message: string) {
57
+ errors.value.push({ trigger, field, message })
58
+ }
59
+
60
+ /** Remove every error contributed by `trigger` (its namespace). */
61
+ function clearTrigger(trigger: string) {
62
+ errors.value = errors.value.filter(e => e.trigger !== trigger)
63
+ }
64
+
65
+ /** Remove every validation error. */
66
+ function clearAll() {
67
+ errors.value = []
68
+ }
69
+
70
+ /**
71
+ * Run a single trigger against a read-only snapshot of `record`. Clears the trigger's prior
72
+ * errors, then executes its `clientHandler` with `{ record, value, setError }`. Fails **open** —
73
+ * a throwing validator warns and records nothing, so a broken validator never blocks the user.
74
+ */
75
+ async function runTrigger(name: string, def: TriggerDefinition, record: Record<string, unknown>, value: unknown) {
76
+ clearTrigger(name)
77
+ // Shallow-frozen copy: the validator reads siblings but can neither mutate the record nor
78
+ // reassign fields (advisory reads only) — and mutation can't leak back to the caller.
79
+ const frozen = Object.freeze({ ...record })
80
+ const boundSetError = (field: string, message: string) => setError(name, field, message)
81
+ try {
82
+ await executeClientHandler(def.clientHandler, { record: frozen, value, setError: boundSetError })
83
+ } catch (error) {
84
+ // Advisory by design: a broken validator must never block save (a bug in one trigger must
85
+ // not trap the user, especially in production). It fails OPEN — but a throw is an error in
86
+ // the author's code, so surface it loudly (error severity, trigger named) rather than
87
+ // silently: a broken validator that just "does nothing" is the real footgun here.
88
+ if (typeof console !== 'undefined') {
89
+ console.error(
90
+ `Validation trigger "${name}" threw and was ignored (advisory — save is not blocked). ` +
91
+ `Fix its clientHandler:`,
92
+ error
93
+ )
94
+ }
95
+ }
96
+ }
97
+
98
+ /**
99
+ * Run every trigger whose `on` set includes `changedField`, passing that field's current value.
100
+ * Call on the edit path (desktop-driven) so editing either bound field re-validates the pair.
101
+ */
102
+ async function validateField(
103
+ triggers: Record<string, TriggerDefinition>,
104
+ changedField: string,
105
+ record: Record<string, unknown>
106
+ ): Promise<void> {
107
+ const runs: Promise<void>[] = []
108
+ for (const [name, def] of Object.entries(triggers)) {
109
+ if (def.on.includes(changedField)) {
110
+ runs.push(runTrigger(name, def, record, record[changedField]))
111
+ }
112
+ }
113
+ await Promise.all(runs)
114
+ }
115
+
116
+ /**
117
+ * Run **all** triggers (no fired-field filter) — the authoritative client gate at save-attempt,
118
+ * covering triggers whose fields were never edited. `value` is undefined (no single changed field).
119
+ */
120
+ async function validateRecord(
121
+ triggers: Record<string, TriggerDefinition>,
122
+ record: Record<string, unknown>
123
+ ): Promise<void> {
124
+ await Promise.all(Object.entries(triggers).map(([name, def]) => runTrigger(name, def, record, undefined)))
125
+ }
126
+
127
+ return {
128
+ errors,
129
+ isValid,
130
+ errorsByField,
131
+ errorsFor,
132
+ setError,
133
+ clearTrigger,
134
+ clearAll,
135
+ validateField,
136
+ validateRecord,
137
+ }
138
+ })
@@ -23,7 +23,7 @@ export type DoctypeConfig = {
23
23
  name: string
24
24
  /** URL-friendly slug (kebab-case) */
25
25
  slug?: string
26
- /** Field definitions (including link fields with fieldtype: 'Link') */
26
+ /** Field definitions (a link field is one carrying `doctype`) */
27
27
  fields?: DoctypeField[]
28
28
  /** Relationship links to other doctypes */
29
29
  links?: Record<string, LinkDeclaration>
@@ -123,9 +123,6 @@ export interface OperationLogConfig {
123
123
  /** Enable cross-tab synchronization (default: true) */
124
124
  enableCrossTabSync?: boolean
125
125
 
126
- /** Auto-sync interval in milliseconds (default: 30000) */
127
- autoSyncInterval?: number
128
-
129
126
  /** Enable operation persistence to localStorage (default: false) */
130
127
  enablePersistence?: boolean
131
128