@donotdev/functions 0.2.3 → 0.3.1

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.
Files changed (114) hide show
  1. package/guidelines/VERCEL.md +2 -2
  2. package/lib/chunks/{chunk-UH445BGK.js → chunk-2N73OZFE.js} +2 -2
  3. package/lib/chunks/chunk-472JD6X7.js +22 -0
  4. package/lib/chunks/chunk-472JD6X7.js.map +7 -0
  5. package/lib/chunks/{chunk-QXIOO2RL.js → chunk-BTWJEJCV.js} +2 -2
  6. package/lib/chunks/chunk-CN6WXKYF.js +22 -0
  7. package/lib/chunks/chunk-CN6WXKYF.js.map +7 -0
  8. package/lib/chunks/{chunk-GDIUOJMD.js → chunk-DGBZDD6D.js} +2 -2
  9. package/lib/chunks/chunk-GBZQPOX4.js +151 -0
  10. package/lib/chunks/{chunk-F3WBFWPO.js.map → chunk-GBZQPOX4.js.map} +3 -3
  11. package/lib/chunks/chunk-ORBGTVJI.js +22 -0
  12. package/lib/chunks/{chunk-EZKRLLSQ.js.map → chunk-ORBGTVJI.js.map} +1 -1
  13. package/lib/chunks/chunk-R3I7U5QT.js +22 -0
  14. package/lib/chunks/chunk-R3I7U5QT.js.map +7 -0
  15. package/lib/chunks/{chunk-6NXIK35R.js → chunk-XWMG2FWZ.js} +2 -2
  16. package/lib/chunks/{chunk-6NXIK35R.js.map → chunk-XWMG2FWZ.js.map} +1 -1
  17. package/lib/chunks/chunk-ZCY6GTIC.js +22 -0
  18. package/lib/chunks/chunk-ZCY6GTIC.js.map +7 -0
  19. package/lib/chunks/{monitoring-LXNVBD3G.js → monitoring-KF7FRUQ6.js} +2 -2
  20. package/lib/chunks/{rateLimiter-GG5LPWXJ.js → rateLimiter-S7TITXOD.js} +2 -2
  21. package/lib/chunks/{server-NODQAE3F.js → server-SU4BVPPF.js} +2 -2
  22. package/lib/firebase/auth/removeCustomClaims.d.ts +1 -1
  23. package/lib/firebase/crud/create.d.ts +22 -1
  24. package/lib/firebase/crud/update.d.ts +27 -3
  25. package/lib/firebase/index.js +1 -1
  26. package/lib/firebase/index.js.map +3 -3
  27. package/lib/shared/billing/helpers/updateUserSubscription.d.ts +2 -8
  28. package/lib/shared/billing/index.d.ts +2 -0
  29. package/lib/shared/billing/verifyOrderCheckout.d.ts +53 -0
  30. package/lib/shared/crud/bulkCore.d.ts +38 -10
  31. package/lib/shared/crud/index.d.ts +1 -0
  32. package/lib/shared/crud/written.d.ts +21 -0
  33. package/lib/shared/index.d.ts +0 -1
  34. package/lib/shared/index.js +1 -1
  35. package/lib/shared/utils.d.ts +0 -9
  36. package/lib/supabase/crud/bulk.d.ts +7 -0
  37. package/lib/supabase/crud/create.d.ts +5 -1
  38. package/lib/supabase/crud/entityStore.d.ts +54 -14
  39. package/lib/supabase/crud/update.d.ts +7 -2
  40. package/lib/supabase/index.d.ts +4 -0
  41. package/lib/supabase/index.js +1 -1
  42. package/lib/supabase/index.js.map +4 -4
  43. package/lib/vercel/api/crud/create.d.ts +6 -1
  44. package/lib/vercel/api/crud/update.d.ts +8 -1
  45. package/lib/vercel/api/index.js +1 -1
  46. package/package.json +3 -3
  47. package/src/firebase/auth/removeCustomClaims.ts +31 -1
  48. package/src/firebase/auth/setCustomClaims.ts +3 -0
  49. package/src/firebase/crud/__tests__/bulk.test.ts +50 -1
  50. package/src/firebase/crud/__tests__/create.test.ts +64 -0
  51. package/src/firebase/crud/__tests__/update.test.ts +97 -0
  52. package/src/firebase/crud/aggregate.ts +10 -21
  53. package/src/firebase/crud/bulk.ts +23 -10
  54. package/src/firebase/crud/create.ts +19 -18
  55. package/src/firebase/crud/get.ts +3 -12
  56. package/src/firebase/crud/list.ts +5 -4
  57. package/src/firebase/crud/update.ts +35 -19
  58. package/src/firebase/registerCrudFunctions.ts +1 -0
  59. package/src/shared/__tests__/bulkCore.test.ts +191 -0
  60. package/src/shared/__tests__/removeCustomClaims.test.ts +117 -0
  61. package/src/shared/__tests__/setCustomClaims.test.ts +103 -0
  62. package/src/shared/__tests__/utils.test.ts +0 -30
  63. package/src/shared/billing/__tests__/updateUserSubscription.test.ts +139 -0
  64. package/src/shared/billing/__tests__/verifyOrderCheckout.exports.test.ts +47 -0
  65. package/src/shared/billing/__tests__/verifyOrderCheckout.supabase.integration.test.ts +107 -0
  66. package/src/shared/billing/__tests__/verifyOrderCheckout.test.ts +233 -0
  67. package/src/shared/billing/helpers/updateUserSubscription.ts +10 -14
  68. package/src/shared/billing/index.ts +2 -0
  69. package/src/shared/billing/verifyOrderCheckout.ts +142 -0
  70. package/src/shared/crud/bulkCore.ts +110 -81
  71. package/src/shared/crud/index.ts +1 -0
  72. package/src/shared/crud/written.ts +56 -0
  73. package/src/shared/index.d.ts +0 -9
  74. package/src/shared/index.ts +0 -1
  75. package/src/shared/utils.ts +0 -43
  76. package/src/supabase/__tests__/bulk.test.ts +60 -2
  77. package/src/supabase/__tests__/create.test.ts +86 -0
  78. package/src/supabase/__tests__/entityStore.test.ts +91 -25
  79. package/src/supabase/__tests__/update.test.ts +107 -0
  80. package/src/supabase/auth/removeCustomClaims.ts +7 -2
  81. package/src/supabase/auth/setCustomClaims.ts +4 -2
  82. package/src/supabase/crud/bulk.ts +47 -0
  83. package/src/supabase/crud/create.ts +14 -24
  84. package/src/supabase/crud/entityStore.ts +106 -17
  85. package/src/supabase/crud/get.ts +3 -12
  86. package/src/supabase/crud/list.ts +5 -4
  87. package/src/supabase/crud/update.ts +20 -18
  88. package/src/supabase/index.ts +7 -0
  89. package/src/supabase/registerCrudFunctions.ts +1 -0
  90. package/src/vercel/__tests__/bulk.test.ts +16 -8
  91. package/src/vercel/api/crud/bulk.ts +27 -4
  92. package/src/vercel/api/crud/create.ts +17 -19
  93. package/src/vercel/api/crud/get.ts +5 -6
  94. package/src/vercel/api/crud/list.ts +4 -5
  95. package/src/vercel/api/crud/update.ts +20 -18
  96. package/lib/chunks/chunk-EZKRLLSQ.js +0 -22
  97. package/lib/chunks/chunk-F3PHE627.js +0 -22
  98. package/lib/chunks/chunk-F3PHE627.js.map +0 -7
  99. package/lib/chunks/chunk-F3WBFWPO.js +0 -151
  100. package/lib/chunks/chunk-G5DRV3DH.js +0 -22
  101. package/lib/chunks/chunk-G5DRV3DH.js.map +0 -7
  102. package/lib/chunks/chunk-Q2WLRFOE.js +0 -22
  103. package/lib/chunks/chunk-Q2WLRFOE.js.map +0 -7
  104. package/lib/chunks/chunk-W74UL7AY.js +0 -22
  105. package/lib/chunks/chunk-W74UL7AY.js.map +0 -7
  106. package/lib/shared/schema.d.ts +0 -56
  107. package/src/shared/__tests__/schema.test.ts +0 -219
  108. package/src/shared/schema.ts +0 -188
  109. /package/lib/chunks/{chunk-UH445BGK.js.map → chunk-2N73OZFE.js.map} +0 -0
  110. /package/lib/chunks/{chunk-QXIOO2RL.js.map → chunk-BTWJEJCV.js.map} +0 -0
  111. /package/lib/chunks/{chunk-GDIUOJMD.js.map → chunk-DGBZDD6D.js.map} +0 -0
  112. /package/lib/chunks/{monitoring-LXNVBD3G.js.map → monitoring-KF7FRUQ6.js.map} +0 -0
  113. /package/lib/chunks/{rateLimiter-GG5LPWXJ.js.map → rateLimiter-S7TITXOD.js.map} +0 -0
  114. /package/lib/chunks/{server-NODQAE3F.js.map → server-SU4BVPPF.js.map} +0 -0
@@ -1,56 +0,0 @@
1
- /** Visibility levels matching @donotdev/core */
2
- type Visibility = 'guest' | 'user' | 'admin' | 'technical' | 'hidden' | 'owner';
3
- /**
4
- * Determines if a field should be visible based on its visibility setting
5
- * and the user's authentication/admin status.
6
- *
7
- * Visibility levels:
8
- * - 'guest': Always visible (even to unauthenticated users)
9
- * - 'user': Visible to authenticated users (users see both guest and user fields)
10
- * - 'admin': Visible only to admins
11
- * - 'technical': Visible to admins only (shown as read-only in edit forms)
12
- * - 'hidden': Never visible (passwords, tokens, API keys - only in DB)
13
- * - 'owner': Visible only when uid matches one of entity.ownership.ownerFields (requires document context; here treated as not visible for aggregate)
14
- * - undefined: Defaults to 'user' behavior (visible to authenticated users)
15
- *
16
- * @param key - The name (key) of the field.
17
- * @param visibility - The visibility setting.
18
- * @param isAdmin - Whether the current user is an admin.
19
- * @param isAuthenticated - Whether the current user is authenticated (defaults to true for backward compat).
20
- * @returns True if the field should be visible, false otherwise.
21
- *
22
- * @version 0.1.0
23
- * @since 0.0.1
24
- * @author AMBROISE PARK Consulting
25
- */
26
- export declare function isFieldVisible(key: string, visibility: Visibility | undefined, isAdmin: boolean, isAuthenticated?: boolean): boolean;
27
- /**
28
- * Extracts the names of all fields from a Valibot object schema that should be visible
29
- * based on the user's administrative status and the visibility rules defined in the schema.
30
- *
31
- * @param schema - The Valibot schema, expected to be an object schema (v.object).
32
- * @param isAdmin - Whether the current user is an admin.
33
- * @returns An array of strings, where each string is the name of a visible field.
34
- * Returns an empty array if the schema is not an object schema or has no entries.
35
- *
36
- * @version 0.1.0
37
- * @since 0.0.1
38
- * @author AMBROISE PARK Consulting
39
- */
40
- export declare function getVisibleFields(schema: any, // Accept any Valibot type, but process only objects
41
- isAdmin: boolean): string[];
42
- /**
43
- * Filters the fields of a data object based on visibility rules defined
44
- * in a Valibot schema and the user's admin status.
45
- *
46
- * @template T - The expected type of the data object.
47
- * @param {T | null | undefined} data - The document data object to filter.
48
- * @param {any} schema - The Valibot schema defining the structure and field visibility.
49
- * Expected to be a v.object schema with entries property.
50
- * @param {boolean} isAdmin - Whether the current user has administrative privileges.
51
- * @returns {Partial<T>} A new object containing only the fields visible to the user,
52
- * or an empty object if input data is null/undefined.
53
- */
54
- export declare function filterVisibleFields<T extends Record<string, any>>(data: T | null | undefined, schema: any, // Use any for broader compatibility
55
- isAdmin: boolean): Partial<T>;
56
- export {};
@@ -1,219 +0,0 @@
1
- import { describe, it, expect, vi } from 'vitest';
2
-
3
- import {
4
- isFieldVisible,
5
- getVisibleFields,
6
- filterVisibleFields,
7
- } from '../schema';
8
-
9
- // ---------------------------------------------------------------------------
10
- // isFieldVisible
11
- // ---------------------------------------------------------------------------
12
-
13
- describe('isFieldVisible', () => {
14
- describe('hidden visibility', () => {
15
- it('never visible regardless of auth/admin', () => {
16
- expect(isFieldVisible('secret', 'hidden', true, true)).toBe(false);
17
- expect(isFieldVisible('secret', 'hidden', false, true)).toBe(false);
18
- expect(isFieldVisible('secret', 'hidden', true, false)).toBe(false);
19
- expect(isFieldVisible('secret', 'hidden', false, false)).toBe(false);
20
- });
21
- });
22
-
23
- describe('guest visibility', () => {
24
- it('always visible regardless of auth/admin', () => {
25
- expect(isFieldVisible('title', 'guest', true, true)).toBe(true);
26
- expect(isFieldVisible('title', 'guest', false, true)).toBe(true);
27
- expect(isFieldVisible('title', 'guest', true, false)).toBe(true);
28
- expect(isFieldVisible('title', 'guest', false, false)).toBe(true);
29
- });
30
- });
31
-
32
- describe('admin visibility', () => {
33
- it('visible only to admins', () => {
34
- expect(isFieldVisible('config', 'admin', true, true)).toBe(true);
35
- expect(isFieldVisible('config', 'admin', false, true)).toBe(false);
36
- expect(isFieldVisible('config', 'admin', true, false)).toBe(true);
37
- expect(isFieldVisible('config', 'admin', false, false)).toBe(false);
38
- });
39
- });
40
-
41
- describe('technical visibility', () => {
42
- it('visible only to admins', () => {
43
- expect(isFieldVisible('debug', 'technical', true)).toBe(true);
44
- expect(isFieldVisible('debug', 'technical', false)).toBe(false);
45
- });
46
- });
47
-
48
- describe('owner visibility', () => {
49
- it('returns false in aggregate context', () => {
50
- expect(isFieldVisible('email', 'owner', true, true)).toBe(false);
51
- expect(isFieldVisible('email', 'owner', false, true)).toBe(false);
52
- });
53
- });
54
-
55
- describe('user visibility', () => {
56
- it('visible to authenticated users', () => {
57
- expect(isFieldVisible('profile', 'user', false, true)).toBe(true);
58
- expect(isFieldVisible('profile', 'user', false, false)).toBe(false);
59
- });
60
-
61
- it('visible to admins', () => {
62
- expect(isFieldVisible('profile', 'user', true, true)).toBe(true);
63
- });
64
- });
65
-
66
- describe('undefined visibility (defaults to user)', () => {
67
- it('visible to authenticated users', () => {
68
- expect(isFieldVisible('field', undefined, false, true)).toBe(true);
69
- expect(isFieldVisible('field', undefined, false, false)).toBe(false);
70
- });
71
-
72
- it('defaults isAuthenticated to true', () => {
73
- expect(isFieldVisible('field', undefined, false)).toBe(true);
74
- });
75
- });
76
- });
77
-
78
- // ---------------------------------------------------------------------------
79
- // getVisibleFields
80
- // ---------------------------------------------------------------------------
81
-
82
- describe('getVisibleFields', () => {
83
- it('returns empty array for non-object schema', () => {
84
- const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
85
-
86
- expect(getVisibleFields(null, false)).toEqual([]);
87
- expect(getVisibleFields('string', false)).toEqual([]);
88
- expect(getVisibleFields({}, false)).toEqual([]);
89
-
90
- warnSpy.mockRestore();
91
- });
92
-
93
- it('returns all fields without visibility for authenticated user', () => {
94
- const schema = {
95
- entries: {
96
- name: {},
97
- email: {},
98
- },
99
- };
100
-
101
- const result = getVisibleFields(schema, false);
102
-
103
- expect(result).toContain('name');
104
- expect(result).toContain('email');
105
- });
106
-
107
- it('excludes hidden fields', () => {
108
- const schema = {
109
- entries: {
110
- name: {},
111
- password: { visibility: 'hidden' },
112
- },
113
- };
114
-
115
- const result = getVisibleFields(schema, true);
116
-
117
- expect(result).toContain('name');
118
- expect(result).not.toContain('password');
119
- });
120
-
121
- it('includes admin fields only for admins', () => {
122
- const schema = {
123
- entries: {
124
- name: {},
125
- config: { visibility: 'admin' },
126
- },
127
- };
128
-
129
- expect(getVisibleFields(schema, true)).toContain('config');
130
- expect(getVisibleFields(schema, false)).not.toContain('config');
131
- });
132
-
133
- it('includes guest fields for everyone', () => {
134
- const schema = {
135
- entries: {
136
- title: { visibility: 'guest' },
137
- },
138
- };
139
-
140
- expect(getVisibleFields(schema, false)).toContain('title');
141
- expect(getVisibleFields(schema, true)).toContain('title');
142
- });
143
- });
144
-
145
- // ---------------------------------------------------------------------------
146
- // filterVisibleFields
147
- // ---------------------------------------------------------------------------
148
-
149
- describe('filterVisibleFields', () => {
150
- it('returns empty object for null data', () => {
151
- const schema = { entries: { name: {} } };
152
-
153
- expect(filterVisibleFields(null, schema, false)).toEqual({});
154
- });
155
-
156
- it('returns empty object for undefined data', () => {
157
- const schema = { entries: { name: {} } };
158
-
159
- expect(filterVisibleFields(undefined, schema, false)).toEqual({});
160
- });
161
-
162
- it('filters data to only visible fields', () => {
163
- const schema = {
164
- entries: {
165
- name: { visibility: 'guest' },
166
- secret: { visibility: 'hidden' },
167
- config: { visibility: 'admin' },
168
- },
169
- };
170
-
171
- const data = { name: 'Alice', secret: 'pw123', config: 'x', extra: 'y' };
172
-
173
- const result = filterVisibleFields(data, schema, false);
174
-
175
- expect(result).toEqual({ name: 'Alice' });
176
- });
177
-
178
- it('includes admin fields when isAdmin is true', () => {
179
- const schema = {
180
- entries: {
181
- name: { visibility: 'guest' },
182
- config: { visibility: 'admin' },
183
- },
184
- };
185
-
186
- const data = { name: 'Alice', config: 'x' };
187
- const result = filterVisibleFields(data, schema, true);
188
-
189
- expect(result).toEqual({ name: 'Alice', config: 'x' });
190
- });
191
-
192
- it('ignores data keys not in schema', () => {
193
- const schema = {
194
- entries: {
195
- name: {},
196
- },
197
- };
198
-
199
- const data = { name: 'Alice', extraField: 'ignored' };
200
- const result = filterVisibleFields(data, schema, false);
201
-
202
- expect(result).toEqual({ name: 'Alice' });
203
- });
204
-
205
- it('handles missing data keys gracefully', () => {
206
- const schema = {
207
- entries: {
208
- name: {},
209
- missing: {},
210
- },
211
- };
212
-
213
- const data = { name: 'Alice' };
214
- const result = filterVisibleFields(data, schema, false);
215
-
216
- expect(result).toEqual({ name: 'Alice' });
217
- expect(result).not.toHaveProperty('missing');
218
- });
219
- });
@@ -1,188 +0,0 @@
1
- // packages/functions/src/shared/schema.ts
2
-
3
- /**
4
- * @fileoverview Schema utilities for functions
5
- * @description Server-safe schema field visibility utilities
6
- *
7
- * @version 0.1.0
8
- * @since 0.0.1
9
- * @author AMBROISE PARK Consulting
10
- */
11
-
12
- import type * as v from 'valibot';
13
-
14
- /** Visibility levels matching @donotdev/core */
15
- type Visibility = 'guest' | 'user' | 'admin' | 'technical' | 'hidden' | 'owner';
16
-
17
- // Define a type for the custom visibility property we might add to Valibot schemas
18
- interface ValibotSchemaWithVisibility extends v.GenericSchema {
19
- visibility?: Visibility;
20
- }
21
-
22
- /**
23
- * Safely extracts the visibility setting from a Valibot field schema.
24
- * Looks for a custom 'visibility' property on the schema object.
25
- *
26
- * @param field - A Valibot schema field (e.g., v.string()).
27
- * @returns The visibility setting if found, otherwise undefined.
28
- *
29
- * @version 0.1.0
30
- * @since 0.0.1
31
- * @author AMBROISE PARK Consulting
32
- */
33
- function getFieldVisibility(field: unknown): Visibility | undefined {
34
- // Check if the field has visibility property
35
- if (field && typeof field === 'object' && 'visibility' in field) {
36
- return (field as ValibotSchemaWithVisibility).visibility;
37
- }
38
- return undefined;
39
- }
40
-
41
- /**
42
- * Determines if a field should be visible based on its visibility setting
43
- * and the user's authentication/admin status.
44
- *
45
- * Visibility levels:
46
- * - 'guest': Always visible (even to unauthenticated users)
47
- * - 'user': Visible to authenticated users (users see both guest and user fields)
48
- * - 'admin': Visible only to admins
49
- * - 'technical': Visible to admins only (shown as read-only in edit forms)
50
- * - 'hidden': Never visible (passwords, tokens, API keys - only in DB)
51
- * - 'owner': Visible only when uid matches one of entity.ownership.ownerFields (requires document context; here treated as not visible for aggregate)
52
- * - undefined: Defaults to 'user' behavior (visible to authenticated users)
53
- *
54
- * @param key - The name (key) of the field.
55
- * @param visibility - The visibility setting.
56
- * @param isAdmin - Whether the current user is an admin.
57
- * @param isAuthenticated - Whether the current user is authenticated (defaults to true for backward compat).
58
- * @returns True if the field should be visible, false otherwise.
59
- *
60
- * @version 0.1.0
61
- * @since 0.0.1
62
- * @author AMBROISE PARK Consulting
63
- */
64
- export function isFieldVisible(
65
- key: string,
66
- visibility: Visibility | undefined,
67
- isAdmin: boolean,
68
- isAuthenticated: boolean = true
69
- ): boolean {
70
- // Hidden fields are never exposed
71
- if (visibility === 'hidden') {
72
- return false;
73
- }
74
-
75
- // Guest fields are always visible
76
- if (visibility === 'guest') {
77
- return true;
78
- }
79
-
80
- // Admin fields are visible only to admins
81
- if (visibility === 'admin') {
82
- return isAdmin;
83
- }
84
-
85
- // Technical fields are visible only to admins
86
- if (visibility === 'technical') {
87
- return isAdmin;
88
- }
89
-
90
- // Owner: requires per-document check (documentData, uid, ownership); not available in aggregate context
91
- if (visibility === 'owner') {
92
- return false;
93
- }
94
-
95
- // User fields (or undefined/default) are visible to authenticated users
96
- if (visibility === 'user' || visibility === undefined) {
97
- return isAuthenticated;
98
- }
99
-
100
- // Fallback: hide unknown visibility levels
101
- return false;
102
- }
103
-
104
- /**
105
- * Extracts the names of all fields from a Valibot object schema that should be visible
106
- * based on the user's administrative status and the visibility rules defined in the schema.
107
- *
108
- * @param schema - The Valibot schema, expected to be an object schema (v.object).
109
- * @param isAdmin - Whether the current user is an admin.
110
- * @returns An array of strings, where each string is the name of a visible field.
111
- * Returns an empty array if the schema is not an object schema or has no entries.
112
- *
113
- * @version 0.1.0
114
- * @since 0.0.1
115
- * @author AMBROISE PARK Consulting
116
- */
117
- export function getVisibleFields(
118
- // eslint-disable-next-line @typescript-eslint/no-explicit-any -- why: exported signature; narrowing it is a breaking change for importers (deferred to a major, any-audit 2026-11-11)
119
- schema: any, // Accept any Valibot type, but process only objects
120
- isAdmin: boolean
121
- ): string[] {
122
- // Valibot object schemas use .entries property
123
- // Ensure the schema is an object schema and has entries
124
- if (!schema || typeof schema !== 'object' || !schema.entries) {
125
- console.warn(
126
- 'getVisibleFields expects a Valibot object schema (v.object).'
127
- );
128
- return []; // Return empty array if not an object schema
129
- }
130
-
131
- // Extract the entries (field definitions) from the schema
132
- const entries: Record<string, unknown> = schema.entries;
133
- const visibleFieldNames: string[] = [];
134
-
135
- // Iterate over each field defined in the schema's entries
136
- for (const [key, field] of Object.entries(entries)) {
137
- // Get the visibility setting for the current field
138
- const visibility = getFieldVisibility(field);
139
-
140
- // Check if the field is visible based on its key, visibility setting, and admin status
141
- if (isFieldVisible(key, visibility, isAdmin)) {
142
- visibleFieldNames.push(key); // Add the field name to the list if visible
143
- }
144
- }
145
-
146
- return visibleFieldNames;
147
- }
148
-
149
- /**
150
- * Filters the fields of a data object based on visibility rules defined
151
- * in a Valibot schema and the user's admin status.
152
- *
153
- * @template T - The expected type of the data object.
154
- * @param {T | null | undefined} data - The document data object to filter.
155
- * @param {any} schema - The Valibot schema defining the structure and field visibility.
156
- * Expected to be a v.object schema with entries property.
157
- * @param {boolean} isAdmin - Whether the current user has administrative privileges.
158
- * @returns {Partial<T>} A new object containing only the fields visible to the user,
159
- * or an empty object if input data is null/undefined.
160
- */
161
- // eslint-disable-next-line @typescript-eslint/no-explicit-any -- why: interface-typed records have no implicit index signature; Record<string, unknown> would reject them
162
- export function filterVisibleFields<T extends Record<string, any>>(
163
- data: T | null | undefined,
164
- // eslint-disable-next-line @typescript-eslint/no-explicit-any -- why: exported signature; narrowing it is a breaking change for importers (deferred to a major, any-audit 2026-11-11)
165
- schema: any, // Use any for broader compatibility
166
- isAdmin: boolean
167
- ): Partial<T> {
168
- if (!data) {
169
- return {}; // Return empty object if data is null or undefined
170
- }
171
-
172
- // Get the list of field names visible based on the schema and admin status
173
- const visibleFieldNames = getVisibleFields(schema, isAdmin);
174
-
175
- // Create a new result object
176
- const result: Partial<T> = {};
177
-
178
- // Iterate over the visible field names and copy corresponding values from the original data
179
- for (const key of visibleFieldNames) {
180
- // Check if the key exists in the original data object
181
- if (Object.prototype.hasOwnProperty.call(data, key)) {
182
- // Type assertion needed as key is string, but we expect it to be keyof T
183
- result[key as keyof T] = data[key as keyof T];
184
- }
185
- }
186
-
187
- return result;
188
- }