@workbasehq/core 0.2.1 → 0.2.2
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 +7 -2
- package/src/display-field.ts +51 -0
- 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 } from './display-field'
|
|
14
|
+
export { DisplayFieldError, 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.2'
|
|
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,6 +247,7 @@ export function defineInstance(input: Omit<ShorthandInput, 'roles' | 'entities'
|
|
|
243
247
|
}
|
|
244
248
|
validateLabelExpressions(config)
|
|
245
249
|
validatePolicies(config)
|
|
250
|
+
validateDisplayFields(config)
|
|
246
251
|
return config
|
|
247
252
|
}
|
|
248
253
|
export function bodyValidator(entity: InstanceConfig['entities'][string], partial = false) {
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { baseFields } 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 class DisplayFieldError extends Error {}
|
|
16
|
+
|
|
17
|
+
export function suggestedDisplayField(entity: DisplayEntity, readable = (key: string) => !entity.fields[key].sensitive): string | undefined {
|
|
18
|
+
return Object.keys(entity.fields).find(key => entity.fields[key].required && entity.fields[key].type === 'string' && readable(key))
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function validateDisplayFields(schema: DisplaySchema) {
|
|
22
|
+
// Every entity with a read grant can be opened as a console list. Navigation
|
|
23
|
+
// 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)) {
|
|
26
|
+
for (const field of Object.values(entity.fields)) if (field.type === 'ref' && field.entity) required.add(field.entity)
|
|
27
|
+
for (const relation of Object.values(entity.relations ?? {})) {
|
|
28
|
+
required.add(relation.entity)
|
|
29
|
+
if (relation.through) required.add(relation.through.entity)
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
for (const name of required) if (!schema.entities[name]) throw new DisplayFieldError(`Invalid displayField: ${name}; target entity is missing`)
|
|
33
|
+
for (const name of required) {
|
|
34
|
+
const entity = schema.entities[name]
|
|
35
|
+
const readers = Object.keys(schema.permissions).filter(role => schema.permissions[role][name]?.includes('read'))
|
|
36
|
+
const readable = (key: string) => {
|
|
37
|
+
const field = entity.fields[key] ?? (baseFields.includes(key) ? { type: 'string' } : undefined)
|
|
38
|
+
return !!field && (readers.length ? readers.some(role => {
|
|
39
|
+
const policy = schema.policies?.[name]?.[role]
|
|
40
|
+
// Public /schema exposes policy presence only. Its true value may hide
|
|
41
|
+
// an explicit sensitive-field allowlist already checked by defineInstance.
|
|
42
|
+
if (policy === true) return true
|
|
43
|
+
const allow = typeof policy === 'object' ? policy.fields : undefined
|
|
44
|
+
return (!allow || allow.includes(key)) && (!field.sensitive || ['owner', 'administrator'].includes(role) || !!allow?.includes(key))
|
|
45
|
+
}) : !field.sensitive)
|
|
46
|
+
}
|
|
47
|
+
if (entity.displayField && readable(entity.displayField)) continue
|
|
48
|
+
const suggestion = suggestedDisplayField(entity, readable)
|
|
49
|
+
throw new DisplayFieldError(`Invalid displayField: ${name}; set displayField${suggestion ? `: '${suggestion}'` : ''} to an existing readable field`)
|
|
50
|
+
}
|
|
51
|
+
}
|
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 }),
|