-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathrole.zod.ts
More file actions
56 lines (50 loc) · 1.89 KB
/
Copy pathrole.zod.ts
File metadata and controls
56 lines (50 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { z } from 'zod';
import { SnakeCaseIdentifierSchema } from '../shared/identifiers.zod';
/**
* Role Schema (aka Business Unit / Org Unit)
* Defines the organizational hierarchy (Reporting Structure).
*
* COMPARISON:
* - Salesforce: "Role" (Hierarchy for visibility rollup)
* - Microsoft: "Business Unit" (Structural container for data)
* - Kubernetes/AWS: "Role" usually refers to Permissions (we use PermissionSet for that)
*
* ROLES IN OBJECTSTACK:
* Used primarily for "Reporting Structure" - Managers see subordinates' data.
*
* **NAMING CONVENTION:**
* Role names MUST be lowercase snake_case to prevent security issues.
*
* @example Good role names
* - 'sales_manager'
* - 'ceo'
* - 'region_east_vp'
* - 'engineering_lead'
*
* @example Bad role names (will be rejected)
* - 'SalesManager' (camelCase)
* - 'CEO' (uppercase)
* - 'Region East VP' (spaces and uppercase)
*/
import { lazySchema } from '../shared/lazy-schema';
export const RoleSchema = lazySchema(() => z.object({
/** Identity */
name: SnakeCaseIdentifierSchema.describe('Unique role name (lowercase snake_case)'),
label: z.string().describe('Display label (e.g. VP of Sales)'),
/** Hierarchy */
parent: z.string().optional().describe('Parent Role ID (Reports To)'),
/** Description */
description: z.string().optional(),
}));
export type Role = z.infer<typeof RoleSchema>;
/** Authoring input for {@link Role} — defaulted fields are optional. */
export type RoleInput = z.input<typeof RoleSchema>;
/**
* Type-safe factory for a role in the role hierarchy. Validates at authoring time via
* `.parse()` and accepts input-shape config (optional defaults, CEL
* shorthand) — preferred over a bare `: Role` literal.
*/
export function defineRole(config: z.input<typeof RoleSchema>): Role {
return RoleSchema.parse(config);
}