@workbasehq/core 0.2.1 → 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 +19 -2
- package/src/display-field.ts +82 -0
- package/src/migrations/planner.ts +4 -2
- package/src/migrations/versions.ts +8 -2
- package/src/public-schema.ts +2 -1
package/package.json
CHANGED
package/src/config.ts
CHANGED
|
@@ -10,10 +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, isDisplayFieldError } from './display-field'
|
|
14
|
+
export { DisplayFieldError, isDisplayFieldError, displayFieldErrors, suggestedDisplayField, validateDisplayFields } from './display-field'
|
|
13
15
|
|
|
14
16
|
export { Q, Fn, Cond }
|
|
15
17
|
|
|
16
|
-
export const CORE_VERSION = '0.2.
|
|
18
|
+
export const CORE_VERSION = '0.2.3'
|
|
17
19
|
const navigationIconSchema = z.enum(['LayoutDashboard', 'Users', 'MapPin', 'Coins', 'Wallet', 'ArrowLeftRight', 'Settings', 'Building2', 'Activity', 'Workflow', 'Database', 'Folder'])
|
|
18
20
|
const identifier = z.string().regex(/^[a-z][a-z0-9_]*$/)
|
|
19
21
|
export const fieldSchema = z.object({
|
|
@@ -194,7 +196,9 @@ export function defineInstance(input: Omit<ShorthandInput, 'roles' | 'entities'
|
|
|
194
196
|
for (const role of config.account.managedBy ?? []) if (!config.roles.includes(role)) throw new Error(`Unknown account manager role: ${role}`)
|
|
195
197
|
for (const field of config.account.assignments ?? []) if (!Object.hasOwn(account.fields, field)) throw new Error(`Unknown account assignment field: ${field}`)
|
|
196
198
|
}
|
|
197
|
-
for (const [name, entity] of Object.entries(config.entities))
|
|
199
|
+
for (const [name, entity] of Object.entries(config.entities)) {
|
|
200
|
+
if (!entity.displayField && input.entities?.[name]?.displayField !== false) entity.displayField = ['name', 'title'].find(field => entity.fields[field])
|
|
201
|
+
}
|
|
198
202
|
// Presentation hints are validated at load; readable capabilities further narrow them at runtime.
|
|
199
203
|
const navigation = config.ui?.navigation
|
|
200
204
|
if (navigation) {
|
|
@@ -243,8 +247,21 @@ export function defineInstance(input: Omit<ShorthandInput, 'roles' | 'entities'
|
|
|
243
247
|
}
|
|
244
248
|
validateLabelExpressions(config)
|
|
245
249
|
validatePolicies(config)
|
|
250
|
+
try { validateDisplayFields(config) }
|
|
251
|
+
catch (error) {
|
|
252
|
+
if (isDisplayFieldError(error)) error.config = config
|
|
253
|
+
throw error
|
|
254
|
+
}
|
|
246
255
|
return config
|
|
247
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
|
+
}
|
|
248
265
|
export function bodyValidator(entity: InstanceConfig['entities'][string], partial = false) {
|
|
249
266
|
const shape: Record<string, z.ZodType> = {}
|
|
250
267
|
for (const [name, f] of Object.entries(entity.fields)) {
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { baseFields, type InstanceConfig } from './config'
|
|
2
|
+
|
|
3
|
+
/** Shared by config validation and the served /schema deployment probe. */
|
|
4
|
+
interface DisplayEntity {
|
|
5
|
+
displayField?: string
|
|
6
|
+
fields: Record<string, { type: string; required?: boolean; sensitive?: boolean; entity?: string }>
|
|
7
|
+
relations?: Record<string, { entity: string; through?: { entity: string } }>
|
|
8
|
+
}
|
|
9
|
+
interface DisplaySchema {
|
|
10
|
+
entities: Record<string, DisplayEntity>
|
|
11
|
+
permissions: Record<string, Record<string, readonly string[]>>
|
|
12
|
+
policies?: Record<string, Record<string, string | { fields?: string[] } | boolean>>
|
|
13
|
+
}
|
|
14
|
+
|
|
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
|
+
}
|
|
33
|
+
|
|
34
|
+
export function suggestedDisplayField(entity: DisplayEntity, readable = (key: string) => !entity.fields[key].sensitive): string | undefined {
|
|
35
|
+
return Object.keys(entity.fields).find(key => entity.fields[key].required && entity.fields[key].type === 'string' && readable(key))
|
|
36
|
+
}
|
|
37
|
+
|
|
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[] = []
|
|
41
|
+
// Every entity with a read grant can be opened as a console list. Navigation
|
|
42
|
+
// hiding is presentation only; ref/relation targets also need picker labels.
|
|
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)) {
|
|
45
|
+
for (const field of Object.values(entity.fields)) if (field.type === 'ref' && field.entity) required.add(field.entity)
|
|
46
|
+
for (const relation of Object.values(entity.relations ?? {})) {
|
|
47
|
+
required.add(relation.entity)
|
|
48
|
+
if (relation.through) required.add(relation.through.entity)
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
for (const name of required) {
|
|
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
|
+
}
|
|
57
|
+
const readers = Object.keys(schema.permissions).filter(role => schema.permissions[role][name]?.includes('read'))
|
|
58
|
+
const readable = (key: string) => {
|
|
59
|
+
const field = entity.fields[key] ?? (baseFields.includes(key) ? { type: 'string' } : undefined)
|
|
60
|
+
return !!field && field.type !== 'ref' && (readers.length ? readers.some(role => {
|
|
61
|
+
const policy = schema.policies?.[name]?.[role]
|
|
62
|
+
// Public /schema exposes policy presence only. Its true value may hide
|
|
63
|
+
// an explicit sensitive-field allowlist already checked by defineInstance.
|
|
64
|
+
if (policy === true) return true
|
|
65
|
+
const allow = typeof policy === 'object' ? policy.fields : undefined
|
|
66
|
+
return (!allow || allow.includes(key)) && (!field.sensitive || ['owner', 'administrator'].includes(role) || !!allow?.includes(key))
|
|
67
|
+
}) : !field.sensitive)
|
|
68
|
+
}
|
|
69
|
+
if (entity.displayField && readable(entity.displayField)) continue
|
|
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
|
+
})
|
|
76
|
+
}
|
|
77
|
+
return errors
|
|
78
|
+
}
|
|
79
|
+
export function validateDisplayFields(schema: DisplaySchema) {
|
|
80
|
+
const errors = displayFieldErrors(schema)
|
|
81
|
+
if (errors.length) throw new DisplayFieldError(errors)
|
|
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,
|
package/src/public-schema.ts
CHANGED
|
@@ -27,7 +27,8 @@ export function publicSchema(config: InstanceConfig, options: RuntimeOptions) {
|
|
|
27
27
|
...(config.displayName ? { displayName: config.displayName } : {}),
|
|
28
28
|
...(config.ui ? { ui: config.ui } : {}),
|
|
29
29
|
entities: Object.fromEntries(Object.entries(config.entities).map(([name, entity]) => [name, {
|
|
30
|
-
...(entity.
|
|
30
|
+
...(entity.displayField ? { displayField: entity.displayField } : {}),
|
|
31
|
+
...(entity.label ? { labelDependencies: labelDependencies(config, name) } : {}),
|
|
31
32
|
fields: Object.fromEntries(Object.entries(entity.fields).map(([fieldName, field]) => [fieldName, {
|
|
32
33
|
type: field.type, required: field.required,
|
|
33
34
|
...(field.values === undefined ? {} : { values: field.values }),
|