@workbasehq/core 0.2.2 → 0.2.3
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/package.json +1 -1
- package/src/config.ts +16 -4
- package/src/display-field.ts +40 -9
- package/src/migrations/planner.ts +4 -2
- package/src/migrations/versions.ts +8 -2
package/package.json
CHANGED
package/src/config.ts
CHANGED
|
@@ -10,12 +10,12 @@ import { validatePolicies } from './policies'
|
|
|
10
10
|
import { filterSchema } from './query/schema'
|
|
11
11
|
import { formDisplaySchema, formSectionsSchema, validateFormDisplay } from './form-display'
|
|
12
12
|
import { detailDisplaySchema, validateDetailDisplay } from './detail-display'
|
|
13
|
-
import { validateDisplayFields } from './display-field'
|
|
14
|
-
export { DisplayFieldError, suggestedDisplayField, validateDisplayFields } from './display-field'
|
|
13
|
+
import { validateDisplayFields, isDisplayFieldError } from './display-field'
|
|
14
|
+
export { DisplayFieldError, isDisplayFieldError, displayFieldErrors, suggestedDisplayField, validateDisplayFields } from './display-field'
|
|
15
15
|
|
|
16
16
|
export { Q, Fn, Cond }
|
|
17
17
|
|
|
18
|
-
export const CORE_VERSION = '0.2.
|
|
18
|
+
export const CORE_VERSION = '0.2.3'
|
|
19
19
|
const navigationIconSchema = z.enum(['LayoutDashboard', 'Users', 'MapPin', 'Coins', 'Wallet', 'ArrowLeftRight', 'Settings', 'Building2', 'Activity', 'Workflow', 'Database', 'Folder'])
|
|
20
20
|
const identifier = z.string().regex(/^[a-z][a-z0-9_]*$/)
|
|
21
21
|
export const fieldSchema = z.object({
|
|
@@ -247,9 +247,21 @@ export function defineInstance(input: Omit<ShorthandInput, 'roles' | 'entities'
|
|
|
247
247
|
}
|
|
248
248
|
validateLabelExpressions(config)
|
|
249
249
|
validatePolicies(config)
|
|
250
|
-
validateDisplayFields(config)
|
|
250
|
+
try { validateDisplayFields(config) }
|
|
251
|
+
catch (error) {
|
|
252
|
+
if (isDisplayFieldError(error)) error.config = config
|
|
253
|
+
throw error
|
|
254
|
+
}
|
|
251
255
|
return config
|
|
252
256
|
}
|
|
257
|
+
/** Diagnostics and migration baselines only; runtime and migration targets use defineInstance. */
|
|
258
|
+
export function inspectInstance(input: Parameters<typeof defineInstance>[0]) {
|
|
259
|
+
try { return { config: defineInstance(input), errors: [] as import('./display-field').DisplayFieldIssue[] } }
|
|
260
|
+
catch (error) {
|
|
261
|
+
if (isDisplayFieldError(error) && error.config) return { config: error.config, errors: error.errors }
|
|
262
|
+
throw error
|
|
263
|
+
}
|
|
264
|
+
}
|
|
253
265
|
export function bodyValidator(entity: InstanceConfig['entities'][string], partial = false) {
|
|
254
266
|
const shape: Record<string, z.ZodType> = {}
|
|
255
267
|
for (const [name, f] of Object.entries(entity.fields)) {
|
package/src/display-field.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { baseFields } from './config'
|
|
1
|
+
import { baseFields, type InstanceConfig } from './config'
|
|
2
2
|
|
|
3
3
|
/** Shared by config validation and the served /schema deployment probe. */
|
|
4
4
|
interface DisplayEntity {
|
|
@@ -12,30 +12,52 @@ interface DisplaySchema {
|
|
|
12
12
|
policies?: Record<string, Record<string, string | { fields?: string[] } | boolean>>
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
export
|
|
15
|
+
export interface DisplayFieldIssue {
|
|
16
|
+
code: 'invalid_display_field'
|
|
17
|
+
entity: string
|
|
18
|
+
message: string
|
|
19
|
+
suggestedChange?: { kind: 'set_display_field'; entity: string; displayField: string }
|
|
20
|
+
}
|
|
21
|
+
export class DisplayFieldError extends Error {
|
|
22
|
+
readonly code = 'invalid_display_field'
|
|
23
|
+
// Only attached by defineInstance after every other validation has passed.
|
|
24
|
+
config?: InstanceConfig
|
|
25
|
+
constructor(public errors: DisplayFieldIssue[]) {
|
|
26
|
+
super(errors.map(error => error.message).join('\n'))
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
// Node's isolated TypeScript module loader can instantiate a second SDK class.
|
|
30
|
+
export function isDisplayFieldError(error: unknown): error is DisplayFieldError {
|
|
31
|
+
return error instanceof Error && 'code' in error && error.code === 'invalid_display_field' && 'errors' in error && Array.isArray(error.errors)
|
|
32
|
+
}
|
|
16
33
|
|
|
17
34
|
export function suggestedDisplayField(entity: DisplayEntity, readable = (key: string) => !entity.fields[key].sensitive): string | undefined {
|
|
18
35
|
return Object.keys(entity.fields).find(key => entity.fields[key].required && entity.fields[key].type === 'string' && readable(key))
|
|
19
36
|
}
|
|
20
37
|
|
|
21
|
-
|
|
38
|
+
// Explicit operations can check a named entity even when the schema gate exempts it.
|
|
39
|
+
export function displayFieldErrors(schema: DisplaySchema, entityNames?: Iterable<string>): DisplayFieldIssue[] {
|
|
40
|
+
const errors: DisplayFieldIssue[] = []
|
|
22
41
|
// Every entity with a read grant can be opened as a console list. Navigation
|
|
23
42
|
// hiding is presentation only; ref/relation targets also need picker labels.
|
|
24
|
-
const required = new Set(Object.values(schema.permissions).flatMap(grants => Object.keys(grants).filter(name => grants[name].includes('read'))))
|
|
25
|
-
for (const entity of Object.values(schema.entities)) {
|
|
43
|
+
const required = new Set(entityNames ?? Object.values(schema.permissions).flatMap(grants => Object.keys(grants).filter(name => grants[name].includes('read'))))
|
|
44
|
+
if (!entityNames) for (const entity of Object.values(schema.entities)) {
|
|
26
45
|
for (const field of Object.values(entity.fields)) if (field.type === 'ref' && field.entity) required.add(field.entity)
|
|
27
46
|
for (const relation of Object.values(entity.relations ?? {})) {
|
|
28
47
|
required.add(relation.entity)
|
|
29
48
|
if (relation.through) required.add(relation.through.entity)
|
|
30
49
|
}
|
|
31
50
|
}
|
|
32
|
-
for (const name of required) if (!schema.entities[name]) throw new DisplayFieldError(`Invalid displayField: ${name}; target entity is missing`)
|
|
33
51
|
for (const name of required) {
|
|
34
52
|
const entity = schema.entities[name]
|
|
53
|
+
if (!entity) {
|
|
54
|
+
errors.push({ code: 'invalid_display_field', entity: name, message: `Invalid displayField: ${name}; target entity is missing` })
|
|
55
|
+
continue
|
|
56
|
+
}
|
|
35
57
|
const readers = Object.keys(schema.permissions).filter(role => schema.permissions[role][name]?.includes('read'))
|
|
36
58
|
const readable = (key: string) => {
|
|
37
59
|
const field = entity.fields[key] ?? (baseFields.includes(key) ? { type: 'string' } : undefined)
|
|
38
|
-
return !!field && (readers.length ? readers.some(role => {
|
|
60
|
+
return !!field && field.type !== 'ref' && (readers.length ? readers.some(role => {
|
|
39
61
|
const policy = schema.policies?.[name]?.[role]
|
|
40
62
|
// Public /schema exposes policy presence only. Its true value may hide
|
|
41
63
|
// an explicit sensitive-field allowlist already checked by defineInstance.
|
|
@@ -45,7 +67,16 @@ export function validateDisplayFields(schema: DisplaySchema) {
|
|
|
45
67
|
}) : !field.sensitive)
|
|
46
68
|
}
|
|
47
69
|
if (entity.displayField && readable(entity.displayField)) continue
|
|
48
|
-
const suggestion = suggestedDisplayField(entity, readable)
|
|
49
|
-
|
|
70
|
+
const suggestion = suggestedDisplayField(entity, readable) ?? Object.keys(entity.fields).find(readable) ?? baseFields.find(readable)
|
|
71
|
+
const suggestedChange = suggestion ? { kind: 'set_display_field' as const, entity: name, displayField: suggestion } : undefined
|
|
72
|
+
errors.push({ code: 'invalid_display_field', entity: name,
|
|
73
|
+
message: `Invalid displayField: ${name}; set displayField${suggestion ? `: '${suggestion}'` : ''} to an existing readable field. Refs are not allowed. ${suggestedChange ? `MCP repair: propose_change with ${JSON.stringify(suggestedChange)}, then validate the proposal.` : 'Make a scalar field readable, then use propose_change with kind: set_display_field.'}`,
|
|
74
|
+
...(suggestedChange ? { suggestedChange } : {}),
|
|
75
|
+
})
|
|
50
76
|
}
|
|
77
|
+
return errors
|
|
78
|
+
}
|
|
79
|
+
export function validateDisplayFields(schema: DisplaySchema) {
|
|
80
|
+
const errors = displayFieldErrors(schema)
|
|
81
|
+
if (errors.length) throw new DisplayFieldError(errors)
|
|
51
82
|
}
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
import { defineInstance, type InstanceConfig } from '../config'
|
|
1
|
+
import { defineInstance, inspectInstance, type InstanceConfig } from '../config'
|
|
2
2
|
import { column, createEntity, initialSchema, literal, quote, revisionSQL } from './schema'
|
|
3
3
|
export interface MigrationPlan { sql: string; destructive: string[]; blocked: string[]; warnings: string[] }
|
|
4
4
|
export function plan(fromConfig: InstanceConfig | null, toConfig: InstanceConfig, options: { allowDestructive?: boolean } = {}): MigrationPlan {
|
|
5
5
|
const to = defineInstance(toConfig)
|
|
6
6
|
if (!fromConfig) return { sql: initialSchema(to), destructive: [], blocked: [], warnings: [] }
|
|
7
|
-
|
|
7
|
+
// An older schema may violate a new presentation invariant; the result must
|
|
8
|
+
// still pass strict validation above. Never alter its physical baseline.
|
|
9
|
+
const from = inspectInstance(fromConfig).config
|
|
8
10
|
if (from.id !== to.id) throw new Error('Cannot migrate between different instance IDs')
|
|
9
11
|
if (to.revision <= from.revision) throw new Error('Target revision must increase')
|
|
10
12
|
const sql: string[] = []; const destructive: string[] = []; const blocked: string[] = []; const warnings: string[] = []
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { cloneInstanceConfig,
|
|
1
|
+
import { cloneInstanceConfig, inspectInstance, serializableInstanceConfig, type InstanceConfig } from '../config'
|
|
2
2
|
import { plan, type MigrationPlan } from './planner'
|
|
3
3
|
|
|
4
4
|
import type { ConfigVersion, ConfigChange, VersionHistory } from './version-contract'
|
|
@@ -80,7 +80,13 @@ export function versionHistory(current: InstanceConfig, history: readonly Config
|
|
|
80
80
|
if (index === -1) versions.push(live)
|
|
81
81
|
else versions[index] = { ...versions[index], config: live.config }
|
|
82
82
|
return { instance: current.id, currentRevision: current.revision, versions: versions.map((version, i) => {
|
|
83
|
-
const
|
|
83
|
+
const inspected = inspectInstance({ ...version.config, revision: version.revision, plugins: undefined, pluginConfig: undefined, interactive: false } as unknown as InstanceConfig)
|
|
84
|
+
if (inspected.errors.length) return {
|
|
85
|
+
revision: version.revision, date: version.date, commit: version.commit, rollbackTo: version.rollbackTo,
|
|
86
|
+
predecessor: versions[i - 1]?.revision, changes: diffVersions(versions[i - 1]?.config ?? null, version.config),
|
|
87
|
+
rollback: { sql: '-- BLOCKED: historical config needs displayField repair before rollback.\n', blocked: inspected.errors.map(error => error.message), destructive: [], warnings: [], unchanged: false, newRevision: current.revision + 1 },
|
|
88
|
+
}
|
|
89
|
+
const historical = inspected.config
|
|
84
90
|
const rollback = planRollback({ ...current, plugins: undefined, pluginConfig: undefined, interactive: false }, historical)
|
|
85
91
|
const unchanged = equalVersionData(version.config, versionData(current))
|
|
86
92
|
return { revision: version.revision, date: version.date, commit: version.commit, rollbackTo: version.rollbackTo,
|