Skip to content

Commit 9583344

Browse files
Copilothotlong
andcommitted
refactor(permission): consolidate RLS into canonical rls.zod.ts
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent d6709ff commit 9583344

1 file changed

Lines changed: 12 additions & 61 deletions

File tree

packages/spec/src/permission/permission.zod.ts

Lines changed: 12 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,6 @@
11
import { z } from 'zod';
22
import { SnakeCaseIdentifierSchema } from '../shared/identifiers.zod';
3-
4-
/**
5-
* Row-Level Security Rule Schema (Simplified)
6-
*
7-
* Simplified RLS rule definition that can be embedded in permission sets.
8-
* For comprehensive RLS features, see ../permission/rls.zod.ts
9-
*
10-
* This schema allows permission sets to include basic row-level filters
11-
* that restrict data access based on user context.
12-
*
13-
* @example Tenant isolation rule
14-
* ```typescript
15-
* {
16-
* name: 'tenant_isolation',
17-
* objectName: 'account',
18-
* operation: 'read',
19-
* filter: {
20-
* field: 'tenant_id',
21-
* operator: 'eq',
22-
* value: { contextVariable: 'current_user.tenant_id' }
23-
* },
24-
* enabled: true,
25-
* priority: 0
26-
* }
27-
* ```
28-
*/
29-
export const RLSRuleSchema = z.object({
30-
name: z.string()
31-
.regex(/^[a-z_][a-z0-9_]*$/)
32-
.describe('Rule unique identifier (snake_case)'),
33-
objectName: z.string().describe('Target object name'),
34-
operation: z.enum(['read', 'create', 'update', 'delete'])
35-
.describe('Database operation this rule applies to'),
36-
filter: z.object({
37-
field: z.string().describe('Field name to filter on'),
38-
operator: z.enum(['eq', 'ne', 'in', 'nin', 'gt', 'gte', 'lt', 'lte'])
39-
.describe('Filter operator'),
40-
value: z.union([
41-
z.string(),
42-
z.number(),
43-
z.boolean(),
44-
z.array(z.any()),
45-
z.object({ contextVariable: z.string() })
46-
.describe('Reference to context variable (e.g., { contextVariable: "current_user.tenant_id" })'),
47-
]).describe('Filter value or context variable reference'),
48-
}).describe('Filter condition for row-level access'),
49-
enabled: z.boolean().default(true).describe('Whether this rule is active'),
50-
priority: z.number().default(0).describe('Rule evaluation priority (higher = evaluated first)'),
51-
});
52-
53-
export type RLSRule = z.infer<typeof RLSRuleSchema>;
3+
import { RowLevelSecurityPolicySchema } from './rls.zod';
544

555
/**
566
* Entity (Object) Level Permissions
@@ -146,26 +96,27 @@ export const PermissionSetSchema = z.object({
14696
/**
14797
* Row-Level Security Rules
14898
*
149-
* Simplified RLS rules that filter records based on user context.
99+
* Row-level security policies that filter records based on user context.
150100
* These rules are applied in addition to object-level permissions.
151101
*
152-
* For comprehensive RLS features, use the dedicated RLS protocol in ../permission/rls.zod.ts
102+
* Uses the canonical RLS protocol from rls.zod.ts for comprehensive
103+
* row-level security features including PostgreSQL-style USING and CHECK clauses.
104+
*
105+
* @see {@link RowLevelSecurityPolicySchema} for full RLS specification
106+
* @see {@link file://../permission/rls.zod.ts} for comprehensive RLS documentation
153107
*
154108
* @example Multi-tenant isolation
155109
* ```typescript
156110
* rls: [{
157111
* name: 'tenant_filter',
158-
* objectName: 'account',
159-
* operation: 'read',
160-
* filter: {
161-
* field: 'tenant_id',
162-
* operator: 'eq',
163-
* value: { contextVariable: 'current_user.tenant_id' }
164-
* }
112+
* object: 'account',
113+
* operation: 'select',
114+
* using: 'tenant_id = current_user.tenant_id'
165115
* }]
166116
* ```
167117
*/
168-
rls: z.array(RLSRuleSchema).optional().describe('Row-level security rules'),
118+
rowLevelSecurity: z.array(RowLevelSecurityPolicySchema).optional()
119+
.describe('Row-level security policies (see rls.zod.ts for full spec)'),
169120

170121
/**
171122
* Context-Based Access Control Variables

0 commit comments

Comments
 (0)