dpdp-kit 0.2.0 → 0.4.0

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 (46) hide show
  1. package/CHANGELOG.md +90 -0
  2. package/README.md +240 -17
  3. package/dist/access.d.ts +111 -0
  4. package/dist/audit.d.ts +3 -1
  5. package/dist/breach.d.ts +170 -0
  6. package/dist/chunk-2ELTZSW3.js +94 -0
  7. package/dist/chunk-5TVA2TKG.js +37 -0
  8. package/dist/chunk-HXVP446N.js +1051 -0
  9. package/dist/chunk-KLMR7S7I.js +438 -0
  10. package/dist/cli.js +53 -17
  11. package/dist/config.d.ts +69 -0
  12. package/dist/data-access.d.ts +6 -2
  13. package/dist/ddl.d.ts +10 -0
  14. package/dist/dpdp.d.ts +210 -0
  15. package/dist/drizzle.d.ts +403 -11
  16. package/dist/drizzle.js +209 -56
  17. package/dist/errors.d.ts +1 -1
  18. package/dist/express.d.ts +17 -1
  19. package/dist/express.js +10 -2
  20. package/dist/fastify.d.ts +41 -0
  21. package/dist/fastify.js +25 -0
  22. package/dist/guard.d.ts +19 -0
  23. package/dist/hono.d.ts +16 -1
  24. package/dist/hono.js +11 -2
  25. package/dist/index.d.ts +7 -4
  26. package/dist/index.js +420 -100
  27. package/dist/mongodb.d.ts +87 -0
  28. package/dist/mongodb.js +344 -0
  29. package/dist/nestjs.d.ts +65 -0
  30. package/dist/nestjs.js +41 -0
  31. package/dist/postgres.d.ts +45 -0
  32. package/dist/postgres.js +27 -0
  33. package/dist/prisma.d.ts +43 -0
  34. package/dist/prisma.js +281 -0
  35. package/dist/protect.d.ts +74 -0
  36. package/dist/protect.js +114 -0
  37. package/dist/retention.d.ts +1 -1
  38. package/dist/run-audit.d.ts +13 -1
  39. package/dist/safeguards.d.ts +23 -0
  40. package/dist/schedule.d.ts +37 -0
  41. package/dist/schedule.js +66 -0
  42. package/dist/schema-check.d.ts +25 -8
  43. package/dist/store.d.ts +11 -1
  44. package/package.json +58 -5
  45. package/dist/chunk-FGZAH2NF.js +0 -16
  46. package/dist/chunk-KVTUMHXO.js +0 -491
package/dist/store.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import type { AccessEntry, AccessLogStore } from "./access.js";
2
+ import type { Breach, BreachNotice, BreachStore } from "./breach.js";
1
3
  import type { NoticeLanguage } from "./languages.js";
2
4
  import { type ErasureTask, type RequestStore, type RightsRequest, type TaskStore } from "./records.js";
3
5
  export type ConsentAction = "grant" | "withdraw";
@@ -38,8 +40,16 @@ export type DpdpStore = ConsentStore & RequestStore & TaskStore;
38
40
  /** Whether a store can keep rights requests and erasure tasks too (custom stores may not). */
39
41
  export declare function isDpdpStore(store: ConsentStore): store is DpdpStore;
40
42
  /** Keeps everything in memory. For tests and trying the library out; everything is lost on restart. */
41
- export declare function memoryStore(): DpdpStore & {
43
+ export declare function memoryStore(): DpdpStore & AccessLogStore & BreachStore & {
42
44
  readonly all: Map<string, ConsentEvent[]>;
43
45
  readonly requests: Map<string, RightsRequest>;
44
46
  readonly tasks: Map<string, ErasureTask>;
47
+ readonly accessLog: AccessEntry[];
48
+ readonly breaches: Map<string, Breach>;
49
+ readonly notices: Map<string, BreachNotice>;
45
50
  };
51
+ /**
52
+ * Purposes whose latest event is a grant: the person's consent state. Pass `until` (ISO 8601) for
53
+ * the state at that moment, counting only events at or before it.
54
+ */
55
+ export declare function grantedPurposes(events: readonly ConsentEvent[], until?: string): Set<string>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dpdp-kit",
3
- "version": "0.2.0",
3
+ "version": "0.4.0",
4
4
  "description": "Build India's DPDP Act 2023 into your Node.js backend: declared purposes, tamper-evident consent records, erasure plans and an audit you can run in CI.",
5
5
  "keywords": [
6
6
  "dpdp",
@@ -12,14 +12,18 @@
12
12
  "data-protection",
13
13
  "erasure",
14
14
  "compliance",
15
- "drizzle"
15
+ "drizzle",
16
+ "prisma",
17
+ "mongodb",
18
+ "mongoose",
19
+ "postgres"
16
20
  ],
17
21
  "license": "MIT",
18
22
  "repository": {
19
23
  "type": "git",
20
24
  "url": "git+https://github.com/yogesh2902/dpdp-kit.git"
21
25
  },
22
- "homepage": "https://github.com/yogesh2902/dpdp-kit#readme",
26
+ "homepage": "https://dpdp-kit.js.org/",
23
27
  "bugs": {
24
28
  "url": "https://github.com/yogesh2902/dpdp-kit/issues"
25
29
  },
@@ -30,7 +34,8 @@
30
34
  },
31
35
  "packageManager": "pnpm@12.15.0",
32
36
  "files": [
33
- "dist"
37
+ "dist",
38
+ "CHANGELOG.md"
34
39
  ],
35
40
  "bin": {
36
41
  "dpdp-kit": "dist/cli.js"
@@ -52,13 +57,42 @@
52
57
  "types": "./dist/hono.d.ts",
53
58
  "default": "./dist/hono.js"
54
59
  },
60
+ "./fastify": {
61
+ "types": "./dist/fastify.d.ts",
62
+ "default": "./dist/fastify.js"
63
+ },
64
+ "./nestjs": {
65
+ "types": "./dist/nestjs.d.ts",
66
+ "default": "./dist/nestjs.js"
67
+ },
68
+ "./schedule": {
69
+ "types": "./dist/schedule.d.ts",
70
+ "default": "./dist/schedule.js"
71
+ },
72
+ "./protect": {
73
+ "types": "./dist/protect.d.ts",
74
+ "default": "./dist/protect.js"
75
+ },
76
+ "./postgres": {
77
+ "types": "./dist/postgres.d.ts",
78
+ "default": "./dist/postgres.js"
79
+ },
80
+ "./prisma": {
81
+ "types": "./dist/prisma.d.ts",
82
+ "default": "./dist/prisma.js"
83
+ },
84
+ "./mongodb": {
85
+ "types": "./dist/mongodb.d.ts",
86
+ "default": "./dist/mongodb.js"
87
+ },
55
88
  "./package.json": "./package.json"
56
89
  },
57
90
  "scripts": {
58
91
  "build": "tsup && tsc -p tsconfig.build.json",
59
92
  "typecheck": "tsc --noEmit",
60
93
  "test": "vitest run",
61
- "prepublishOnly": "pnpm typecheck && pnpm test && pnpm build"
94
+ "prepublishOnly": "pnpm typecheck && pnpm test && pnpm build",
95
+ "site": "node site/build.mjs"
62
96
  },
63
97
  "peerDependencies": {
64
98
  "drizzle-orm": ">=0.38"
@@ -73,8 +107,27 @@
73
107
  },
74
108
  "devDependencies": {
75
109
  "@electric-sql/pglite": "^0.5.16",
110
+ "@nestjs/common": "^12.5.1",
111
+ "@nestjs/core": "^12.5.1",
112
+ "@nestjs/platform-express": "^12.5.1",
113
+ "@prisma/adapter-pg": "7.10.1",
114
+ "@prisma/client": "7.10.1",
115
+ "@types/express": "^5.0.6",
76
116
  "@types/node": "^26.6.6",
117
+ "@types/pg": "^8.23.1",
77
118
  "drizzle-orm": "^0.45.4",
119
+ "express": "^5.3.0",
120
+ "fastify": "^5.12.8",
121
+ "hono": "^4.13.19",
122
+ "marked": "^18.2.1",
123
+ "mongodb": "~7.8.0",
124
+ "mongodb-memory-server": "11.3.0",
125
+ "mongoose": "9.12.2",
126
+ "pg": "^8.23.1",
127
+ "prisma": "7.10.1",
128
+ "reflect-metadata": "^0.2.2",
129
+ "rxjs": "^7.8.2",
130
+ "shiki": "^4.6.0",
78
131
  "tsup": "^9.0.0",
79
132
  "typescript": "^7.0.2",
80
133
  "vitest": "^5.1.0"
@@ -1,16 +0,0 @@
1
- // src/guard.ts
2
- var CONSENT_REQUIRED_BODY = (purpose) => ({
3
- error: "consent_required",
4
- purpose,
5
- message: "You haven't agreed to this yet. You can turn it on in your privacy settings."
6
- });
7
- async function checkConsent(dpdp, purpose, options, ctx) {
8
- const subjectId = await options.subject(ctx);
9
- if (!subjectId) return false;
10
- return dpdp.consent.has(subjectId, purpose);
11
- }
12
-
13
- export {
14
- CONSENT_REQUIRED_BODY,
15
- checkConsent
16
- };
@@ -1,491 +0,0 @@
1
- // src/duration.ts
2
- var PATTERN = /^(\d+)(h|d|y)$/;
3
- var HOUR_MS = 60 * 60 * 1e3;
4
- var UNIT_MS = { h: HOUR_MS, d: 24 * HOUR_MS, y: 365 * 24 * HOUR_MS };
5
- function isDuration(value) {
6
- return typeof value === "string" && PATTERN.test(value);
7
- }
8
- function durationMs(value) {
9
- const match = PATTERN.exec(value);
10
- if (!match) throw new TypeError(`Not a duration: ${JSON.stringify(value)} (use e.g. "48h", "30d", "1y")`);
11
- const [, amount, unit] = match;
12
- return Number(amount) * UNIT_MS[unit];
13
- }
14
- function maxDuration(a, b) {
15
- return durationMs(b) > durationMs(a) ? b : a;
16
- }
17
-
18
- // src/records.ts
19
- var REQUEST_TYPES = ["access", "correction", "erasure", "grievance", "nomination"];
20
- function isRunnable(task, { now, staleBefore, maxAttempts }) {
21
- if (task.dueAt > now) return false;
22
- if (task.status === "pending") return true;
23
- if (task.status === "failed") return task.attempts < maxAttempts;
24
- if (task.status === "running") return (task.claimedAt ?? "") < staleBefore;
25
- return false;
26
- }
27
-
28
- // src/audit.ts
29
- var ONE_YEAR_MS = durationMs("1y");
30
- function checkRetention(retain, path, issues) {
31
- if (retain === "while-active" || isDuration(retain)) return;
32
- if (retain && typeof retain === "object" && "afterWithdrawal" in retain) {
33
- if (!isDuration(retain.afterWithdrawal)) {
34
- issues.push({
35
- level: "error",
36
- code: "invalid-duration",
37
- path: `${path}.afterWithdrawal`,
38
- message: `${JSON.stringify(retain.afterWithdrawal)} is not a duration. Use e.g. "0d", "30d", "1y".`
39
- });
40
- }
41
- return;
42
- }
43
- issues.push({
44
- level: "error",
45
- code: "invalid-retention",
46
- path,
47
- message: `Retention must be "while-active", a duration like "1y", or { afterWithdrawal: "30d" }.`
48
- });
49
- }
50
- function checkPurposeList(purposes, known, used, path, issues) {
51
- if (!purposes || purposes.length === 0) {
52
- issues.push({
53
- level: "error",
54
- code: "field-without-purpose",
55
- path: `${path}.purposes`,
56
- message: "List at least one purpose. Personal data with no purpose must not be collected."
57
- });
58
- return;
59
- }
60
- for (const p of purposes) {
61
- used.add(p);
62
- if (!known.has(p)) {
63
- issues.push({
64
- level: "error",
65
- code: "unknown-purpose",
66
- path: `${path}.purposes`,
67
- message: `Unknown purpose ${JSON.stringify(p)}. Declare it under \`purposes\` or fix the spelling.`
68
- });
69
- }
70
- }
71
- }
72
- function audit(config) {
73
- const issues = [];
74
- const purposes = config.purposes ?? {};
75
- const known = new Set(Object.keys(purposes));
76
- const used = /* @__PURE__ */ new Set();
77
- if (!config.fiduciary?.name) {
78
- issues.push({
79
- level: "error",
80
- code: "fiduciary-missing",
81
- path: "fiduciary.name",
82
- message: "Name the Data Fiduciary (your organisation). It appears on every notice."
83
- });
84
- }
85
- if (!config.fiduciary?.grievanceUrl) {
86
- issues.push({
87
- level: "warning",
88
- code: "grievance-missing",
89
- path: "fiduciary.grievanceUrl",
90
- message: "Add where people can raise a grievance. Section 8(10) requires a grievance mechanism, and Section 5(1)(ii) requires the notice to say how to use it."
91
- });
92
- }
93
- if (known.size === 0) {
94
- issues.push({ level: "error", code: "no-purposes", path: "purposes", message: "Declare at least one purpose." });
95
- }
96
- for (const [name, def] of Object.entries(purposes)) {
97
- const path = `purposes.${name}`;
98
- if (def.basis !== "consent" && def.basis !== "legitimate-use") {
99
- issues.push({
100
- level: "error",
101
- code: "invalid-basis",
102
- path: `${path}.basis`,
103
- message: `Basis must be "consent" or "legitimate-use".`
104
- });
105
- }
106
- checkRetention(def.retain, `${path}.retain`, issues);
107
- if (def.basis === "legitimate-use" && typeof def.retain === "object") {
108
- issues.push({
109
- level: "error",
110
- code: "invalid-retention",
111
- path: `${path}.retain`,
112
- message: 'afterWithdrawal only applies to consent purposes (Section 6(4)). Use "while-active" or a duration for a legitimate use.'
113
- });
114
- }
115
- }
116
- for (const [key, table] of Object.entries(config.data ?? {})) {
117
- const path = `data.${key}`;
118
- if (!table.subject) {
119
- issues.push({
120
- level: "error",
121
- code: "missing-subject-column",
122
- path: `${path}.subject`,
123
- message: "Name the column that holds the person's id, so their rows can be found."
124
- });
125
- }
126
- const fields = Object.entries(table.fields ?? {});
127
- if (fields.length === 0) {
128
- issues.push({
129
- level: "warning",
130
- code: "table-without-fields",
131
- path: `${path}.fields`,
132
- message: "No personal fields listed. Remove the table from the map, or list its fields."
133
- });
134
- }
135
- for (const [field, def] of fields) {
136
- checkPurposeList(def.purposes, known, used, `${path}.fields.${field}`, issues);
137
- }
138
- if (table.legalHold && !isDuration(table.legalHold.keep)) {
139
- issues.push({
140
- level: "error",
141
- code: "invalid-duration",
142
- path: `${path}.legalHold.keep`,
143
- message: `${JSON.stringify(table.legalHold.keep)} is not a duration. Use e.g. "8y".`
144
- });
145
- }
146
- }
147
- for (const [name, processor] of Object.entries(config.processors ?? {})) {
148
- checkPurposeList(processor.purposes, known, used, `processors.${name}`, issues);
149
- }
150
- for (const name of known) {
151
- if (!used.has(name)) {
152
- issues.push({
153
- level: "warning",
154
- code: "unused-purpose",
155
- path: `purposes.${name}`,
156
- message: "No field or processor uses this purpose. Remove it, or map the data it covers."
157
- });
158
- }
159
- }
160
- for (const entry of config.notPersonal ?? []) {
161
- if (!/^[^.\s]+(\.[^.\s]+)*\.([^.\s]+|\*)$/.test(entry)) {
162
- issues.push({
163
- level: "error",
164
- code: "invalid-not-personal",
165
- path: "notPersonal",
166
- message: `${JSON.stringify(entry)} should be "table.column" or "table.*".`
167
- });
168
- }
169
- }
170
- const within = config.rights?.responseWithin;
171
- if (within !== void 0) {
172
- const entries = typeof within === "string" ? [["", within]] : Object.entries(within);
173
- for (const [type, value] of entries) {
174
- const path = type ? `rights.responseWithin.${type}` : "rights.responseWithin";
175
- if (type && !REQUEST_TYPES.includes(type)) {
176
- issues.push({
177
- level: "error",
178
- code: "invalid-request-type",
179
- path,
180
- message: `Unknown request type. Use one of: ${REQUEST_TYPES.join(", ")}.`
181
- });
182
- } else if (!isDuration(value)) {
183
- issues.push({ level: "error", code: "invalid-duration", path, message: `${JSON.stringify(value)} is not a duration. Use e.g. "30d".` });
184
- } else if ((type === "" || type === "grievance") && durationMs(value) > durationMs("90d")) {
185
- issues.push({
186
- level: "error",
187
- code: "grievance-response-too-long",
188
- path,
189
- message: 'Rule 14(3) of the DPDP Rules requires answering grievances within ninety days. Use at most "90d".'
190
- });
191
- }
192
- }
193
- }
194
- const minimum = config.minimumRetention;
195
- if (minimum === void 0) {
196
- issues.push({
197
- level: "warning",
198
- code: "minimum-retention-missing",
199
- path: "minimumRetention",
200
- message: 'Set minimumRetention. Rule 8(3) of the DPDP Rules requires keeping personal data, traffic data and logs of each processing for at least one year from the processing ("1y").'
201
- });
202
- } else if (!isDuration(minimum)) {
203
- issues.push({
204
- level: "error",
205
- code: "invalid-duration",
206
- path: "minimumRetention",
207
- message: `${JSON.stringify(minimum)} is not a duration. Use e.g. "1y".`
208
- });
209
- } else if (durationMs(minimum) < ONE_YEAR_MS) {
210
- issues.push({
211
- level: "error",
212
- code: "minimum-retention-too-short",
213
- path: "minimumRetention",
214
- message: 'Rule 8(3) of the DPDP Rules requires keeping personal data, traffic data and logs of each processing for at least one year. Use at least "1y".'
215
- });
216
- }
217
- return issues;
218
- }
219
- function hasErrors(issues) {
220
- return issues.some((i) => i.level === "error");
221
- }
222
-
223
- // src/personal.ts
224
- var rule = (spec, category, confidence = "strong") => ({
225
- tokens: spec.split(" "),
226
- category,
227
- confidence
228
- });
229
- var RULES = [
230
- // device and online identifiers
231
- rule("ip address", "device"),
232
- rule("ip", "device"),
233
- rule("imei", "device"),
234
- rule("device id", "device"),
235
- rule("user agent", "device"),
236
- rule("fcm token", "device"),
237
- rule("push token", "device"),
238
- rule("advertising id", "device"),
239
- rule("gaid", "device"),
240
- rule("idfa", "device"),
241
- // Indian and other government identifiers
242
- rule("aadhaar", "identity"),
243
- rule("aadhar", "identity"),
244
- rule("uidai", "identity"),
245
- rule("pan", "identity"),
246
- rule("passport", "identity"),
247
- rule("voter id", "identity"),
248
- rule("epic", "identity"),
249
- rule("driving licence", "identity"),
250
- rule("driving license", "identity"),
251
- rule("abha", "health"),
252
- // dates of birth and names
253
- rule("date of birth", "identity"),
254
- rule("birth date", "identity"),
255
- rule("birthdate", "identity"),
256
- rule("birthday", "identity"),
257
- rule("dob", "identity"),
258
- rule("first name", "identity"),
259
- rule("last name", "identity"),
260
- rule("middle name", "identity"),
261
- rule("full name", "identity"),
262
- rule("surname", "identity"),
263
- rule("father name", "identity"),
264
- rule("mother name", "identity"),
265
- rule("guardian name", "identity"),
266
- rule("parent name", "identity"),
267
- // contact
268
- rule("phone", "contact"),
269
- rule("mobile", "contact"),
270
- rule("msisdn", "contact"),
271
- rule("whatsapp", "contact"),
272
- rule("email", "contact"),
273
- rule("address", "contact"),
274
- rule("pincode", "contact"),
275
- rule("pin code", "contact"),
276
- rule("postcode", "contact"),
277
- rule("postal code", "contact"),
278
- rule("zipcode", "contact"),
279
- rule("zip code", "contact"),
280
- // financial
281
- rule("account number", "financial"),
282
- rule("bank account", "financial"),
283
- rule("ifsc", "financial"),
284
- rule("upi", "financial"),
285
- rule("vpa", "financial"),
286
- rule("card number", "financial"),
287
- rule("iban", "financial"),
288
- rule("salary", "financial"),
289
- rule("income", "financial"),
290
- // health and biometric
291
- rule("blood group", "health"),
292
- rule("diagnosis", "health"),
293
- rule("medical", "health"),
294
- rule("allergy", "health"),
295
- rule("allergies", "health"),
296
- rule("prescription", "health"),
297
- rule("fingerprint", "biometric"),
298
- rule("biometric", "biometric"),
299
- rule("face embedding", "biometric"),
300
- // location
301
- rule("latitude", "location"),
302
- rule("longitude", "location"),
303
- rule("lat", "location"),
304
- rule("lng", "location"),
305
- rule("gps", "location"),
306
- // weak: personal in a people table, not in a products table
307
- rule("name", "identity", "weak"),
308
- rule("gender", "identity", "weak"),
309
- rule("age", "identity", "weak"),
310
- rule("photo", "identity", "weak"),
311
- rule("avatar", "identity", "weak"),
312
- rule("selfie", "biometric", "weak"),
313
- rule("signature", "identity", "weak"),
314
- rule("city", "location", "weak"),
315
- rule("street", "contact", "weak"),
316
- rule("location", "location", "weak")
317
- ];
318
- var DESCRIPTIVE_ENDINGS = /* @__PURE__ */ new Set([
319
- "verified",
320
- "verification",
321
- "confirmed",
322
- "enabled",
323
- "disabled",
324
- "count",
325
- "type",
326
- "status",
327
- "kind",
328
- "flag",
329
- "visible",
330
- "public",
331
- "at",
332
- "sent",
333
- "in",
334
- "out",
335
- "allowed",
336
- "required"
337
- ]);
338
- var ORGANISATION_WORDS = /* @__PURE__ */ new Set([
339
- "company",
340
- "business",
341
- "org",
342
- "organisation",
343
- "organization",
344
- "merchant",
345
- "vendor",
346
- "supplier",
347
- "firm",
348
- "branch",
349
- "office",
350
- "school",
351
- "institute",
352
- "college"
353
- ]);
354
- function tokenize(name) {
355
- return name.replace(/([a-z0-9])([A-Z])/g, "$1 $2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2").toLowerCase().split(/[^a-z0-9]+/).filter(Boolean);
356
- }
357
- function containsRun(tokens, run) {
358
- outer: for (let i = 0; i + run.length <= tokens.length; i++) {
359
- for (let j = 0; j < run.length; j++) if (tokens[i + j] !== run[j]) continue outer;
360
- return true;
361
- }
362
- return false;
363
- }
364
- function guessPersonal(columnName) {
365
- const tokens = tokenize(columnName);
366
- if (tokens.length === 0) return void 0;
367
- const last = tokens[tokens.length - 1];
368
- if (tokens.length > 1 && DESCRIPTIVE_ENDINGS.has(last)) return void 0;
369
- const organisational = tokens.some((t) => ORGANISATION_WORDS.has(t));
370
- for (const r of RULES) {
371
- if (containsRun(tokens, r.tokens)) {
372
- return {
373
- category: r.category,
374
- confidence: organisational ? "weak" : r.confidence,
375
- matched: r.tokens.join(" ")
376
- };
377
- }
378
- }
379
- return void 0;
380
- }
381
-
382
- // src/schema-check.ts
383
- var OWN_TABLE_PREFIX = "dpdp_";
384
- var matches = (column, name) => column.name === name || column.key === name;
385
- function parseNotPersonal(entries) {
386
- const columns = /* @__PURE__ */ new Set();
387
- const tables = /* @__PURE__ */ new Set();
388
- for (const entry of entries ?? []) {
389
- const dot = entry.lastIndexOf(".");
390
- if (dot <= 0) continue;
391
- const table = entry.slice(0, dot);
392
- const column = entry.slice(dot + 1);
393
- if (column === "*") tables.add(table);
394
- else columns.add(`${table}.${column}`);
395
- }
396
- return { columns, tables };
397
- }
398
- function strongest(a, b) {
399
- if (!a) return b;
400
- if (!b) return a;
401
- return b.confidence === "strong" && a.confidence === "weak" ? b : a;
402
- }
403
- function checkSchema(config, schema) {
404
- const issues = [];
405
- const byName = /* @__PURE__ */ new Map();
406
- for (const table of schema.tables) if (!byName.has(table.name)) byName.set(table.name, table);
407
- const mapped = /* @__PURE__ */ new Map();
408
- for (const [key, def] of Object.entries(config.data ?? {})) {
409
- const table = byName.get(def.table);
410
- if (!table) {
411
- issues.push({
412
- level: "error",
413
- code: "unknown-table",
414
- path: `data.${key}.table`,
415
- message: `Table "${def.table}" isn't in the schema. Fix the name, or remove it from the data map if the table is gone.`
416
- });
417
- continue;
418
- }
419
- const list = mapped.get(table.name) ?? [];
420
- const subject = table.columns.find((c) => matches(c, def.subject));
421
- if (subject) list.push(subject);
422
- else {
423
- issues.push({
424
- level: "error",
425
- code: "unknown-column",
426
- path: `data.${key}.subject`,
427
- message: `Column "${def.subject}" isn't in table "${table.name}". The subject column must exist to find a person's rows.`
428
- });
429
- }
430
- for (const field of Object.keys(def.fields ?? {})) {
431
- const column = table.columns.find((c) => matches(c, field));
432
- if (column) list.push(column);
433
- else {
434
- issues.push({
435
- level: "error",
436
- code: "unknown-column",
437
- path: `data.${key}.fields.${field}`,
438
- message: `Column "${field}" isn't in table "${table.name}". Fix the name, or remove it if the column was dropped.`
439
- });
440
- }
441
- }
442
- mapped.set(table.name, list);
443
- }
444
- const ignore = parseNotPersonal(config.notPersonal);
445
- for (const entry of config.notPersonal ?? []) {
446
- const dot = entry.lastIndexOf(".");
447
- const tableName = entry.slice(0, Math.max(dot, 0));
448
- const columnName = entry.slice(dot + 1);
449
- const table = byName.get(tableName);
450
- const exists = table && (columnName === "*" || table.columns.some((c) => matches(c, columnName)));
451
- if (!exists) {
452
- issues.push({
453
- level: "warning",
454
- code: "stale-not-personal",
455
- path: "notPersonal",
456
- message: `"${entry}" doesn't match any table or column. Use "table.column" or "table.*", or remove it.`
457
- });
458
- }
459
- }
460
- for (const table of byName.values()) {
461
- if (table.name.startsWith(OWN_TABLE_PREFIX) || ignore.tables.has(table.name)) continue;
462
- const known = mapped.get(table.name) ?? [];
463
- for (const column of table.columns) {
464
- if (known.includes(column)) continue;
465
- if (ignore.columns.has(`${table.name}.${column.name}`)) continue;
466
- if (column.key && ignore.columns.has(`${table.name}.${column.key}`)) continue;
467
- const guess = strongest(guessPersonal(column.name), column.key ? guessPersonal(column.key) : void 0);
468
- if (!guess) continue;
469
- issues.push({
470
- level: guess.confidence === "strong" ? "error" : "warning",
471
- code: "unmapped-personal-column",
472
- path: `schema.${table.name}.${column.name}`,
473
- message: `Looks like personal data (${guess.category}, matched "${guess.matched}") but isn't in the data map. Add it under \`data\` with the purposes it's held for, or add "${table.name}.${column.name}" to \`notPersonal\`.`
474
- });
475
- }
476
- }
477
- return issues;
478
- }
479
-
480
- export {
481
- isDuration,
482
- durationMs,
483
- maxDuration,
484
- REQUEST_TYPES,
485
- isRunnable,
486
- audit,
487
- hasErrors,
488
- tokenize,
489
- guessPersonal,
490
- checkSchema
491
- };