@platform-sec/sensitive-string-zod 0.2.0 → 0.2.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.
package/dist/base.d.ts ADDED
@@ -0,0 +1,173 @@
1
+ /**
2
+ * The base schema factories: plain strings, validated strings, optional
3
+ * fields, and the serialized wire format.
4
+ *
5
+ * @packageDocumentation
6
+ */
7
+ import { z } from 'zod';
8
+ import { SensitiveString, SensitivityClass } from '@platform-sec/sensitive-string-core';
9
+ import type { SensitiveSchema } from './types.js';
10
+ /**
11
+ * Create a Zod schema that parses strings into SensitiveString instances.
12
+ *
13
+ * @typeParam C - The sensitivity class for the resulting SensitiveString
14
+ * @param klass - The sensitivity classification to apply
15
+ * @param label - Optional label for audit/compliance categorization
16
+ * @returns A Zod schema that transforms strings to SensitiveString<C>
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * const schema = z.object({
21
+ * apiKey: zSensitiveString('credential', 'api_key'),
22
+ * email: zSensitiveString('sensitive', 'email'),
23
+ * });
24
+ *
25
+ * const result = schema.parse({
26
+ * apiKey: 'sk-xxx',
27
+ * email: 'user@example.com',
28
+ * });
29
+ *
30
+ * console.log(result.apiKey); // SensitiveString<credential:api_key> { [REDACTED:credential] }
31
+ * ```
32
+ */
33
+ export declare function zSensitiveString<C extends SensitivityClass>(klass: C, label?: string): SensitiveSchema<C, string>;
34
+ /**
35
+ * Create a Zod schema for sensitive data (PII, business data, etc.).
36
+ * Shorthand for zSensitiveString('sensitive', label).
37
+ *
38
+ * @param label - Optional label for audit/compliance (e.g., 'email', 'phone', 'address')
39
+ * @returns A Zod schema that transforms strings to SensitiveString<'sensitive'>
40
+ *
41
+ * @example
42
+ * ```ts
43
+ * const schema = z.object({
44
+ * email: zSensitive('email'),
45
+ * phone: zSensitive('phone'),
46
+ * name: zSensitive('name'),
47
+ * });
48
+ * ```
49
+ */
50
+ export declare function zSensitive(label?: string): SensitiveSchema<'sensitive', string>;
51
+ /**
52
+ * Create a Zod schema for credential data (passwords, API keys, tokens).
53
+ * Shorthand for zSensitiveString('credential', label).
54
+ * Values will use constant-time comparison to prevent timing attacks.
55
+ *
56
+ * @param label - Optional label for audit/compliance (e.g., 'api_key', 'password', 'session_token')
57
+ * @returns A Zod schema that transforms strings to SensitiveString<'credential'>
58
+ *
59
+ * @example
60
+ * ```ts
61
+ * const schema = z.object({
62
+ * password: zCredential('password'),
63
+ * apiKey: zCredential('api_key'),
64
+ * sessionToken: zCredential('session_token'),
65
+ * });
66
+ * ```
67
+ */
68
+ export declare function zCredential(label?: string): SensitiveSchema<'credential', string>;
69
+ /**
70
+ * Create a Zod schema with additional string validation before wrapping.
71
+ *
72
+ * The schema parameter is any string-to-string schema, not specifically a
73
+ * `z.ZodString`. Zod 4 promoted the string formats to top-level constructors
74
+ * with classes of their own, so `z.email()` is a `ZodEmail` and is not a
75
+ * `ZodString`; requiring the narrower type would push callers back onto the
76
+ * deprecated `z.string().email()` spelling to use this package.
77
+ *
78
+ * @typeParam C - The sensitivity class for the resulting SensitiveString
79
+ * @param klass - The sensitivity classification to apply
80
+ * @param schema - A Zod schema that takes and returns a string
81
+ * @param label - Optional label for audit/compliance
82
+ * @returns A Zod schema that validates then transforms to SensitiveString<C>
83
+ *
84
+ * @example
85
+ * ```ts
86
+ * // API key must be at least 32 characters
87
+ * const apiKeySchema = zSensitiveWithValidation(
88
+ * 'credential',
89
+ * z.string().min(32, 'API key too short'),
90
+ * 'api_key'
91
+ * );
92
+ *
93
+ * // A v4 format constructor works directly.
94
+ * const emailSchema = zSensitiveWithValidation('sensitive', z.email(), 'email');
95
+ *
96
+ * // Password with complexity requirements
97
+ * const passwordSchema = zSensitiveWithValidation(
98
+ * 'credential',
99
+ * z.string()
100
+ * .min(8, 'Password must be at least 8 characters')
101
+ * .regex(/[A-Z]/, 'Password must contain uppercase'),
102
+ * 'password'
103
+ * );
104
+ * ```
105
+ */
106
+ export declare function zSensitiveWithValidation<C extends SensitivityClass>(klass: C, schema: z.ZodType<string, string>, label?: string): SensitiveSchema<C, string>;
107
+ /**
108
+ * Create a Zod schema for optional sensitive strings.
109
+ * Returns null for undefined/null input, SensitiveString for valid strings.
110
+ *
111
+ * @typeParam C - The sensitivity class for the resulting SensitiveString
112
+ * @param klass - The sensitivity classification to apply
113
+ * @param label - Optional label for audit/compliance
114
+ * @returns A Zod schema that transforms optional strings
115
+ *
116
+ * @example
117
+ * ```ts
118
+ * const schema = z.object({
119
+ * apiKey: zSensitiveOptional('credential', 'api_key'),
120
+ * });
121
+ *
122
+ * schema.parse({ apiKey: undefined }); // { apiKey: null }
123
+ * schema.parse({ apiKey: 'sk-xxx' }); // { apiKey: SensitiveString<credential> }
124
+ * ```
125
+ */
126
+ export declare function zSensitiveOptional<C extends SensitivityClass>(klass: C, label?: string): z.ZodType<SensitiveString<C> | null, string | null | undefined>;
127
+ /**
128
+ * Create a Zod schema that validates against a serialized format
129
+ * and reconstructs the SensitiveString.
130
+ *
131
+ * Useful for validating incoming payloads that were serialized with serialize().
132
+ *
133
+ * Reconstruction is delegated to `SensitiveString.deserialize()`, so this path
134
+ * enforces exactly what the core wire format does: the sensitivity class must be
135
+ * registered, a deadline carried by the payload is carried into the value, a
136
+ * payload that marks the value revoked produces a revoked value, and environment
137
+ * restrictions and destination bindings arrive with it. Rebuilding the value
138
+ * here instead would silently drop all four, which fails open: an expiring
139
+ * token would arrive as one that never expires, a revoked one as live, one
140
+ * restricted to the EU as readable anywhere, and one bound to a host as
141
+ * sendable to any.
142
+ *
143
+ * A class mismatch, an unregistered class, and a malformed payload are all
144
+ * reported as Zod issues, so `safeParse` reports them rather than throwing.
145
+ *
146
+ * @typeParam C - The expected sensitivity class
147
+ * @param expectedKlass - Optional expected class (validates if provided)
148
+ * @returns A Zod schema that parses serialized forms to SensitiveString
149
+ *
150
+ * @example
151
+ * ```ts
152
+ * const schema = z.object({
153
+ * token: zSensitiveDeserialize('credential'),
154
+ * });
155
+ *
156
+ * // Validates serialized structure and reconstructs
157
+ * const result = schema.parse({
158
+ * token: { __sensitive: true, klass: 'credential', value: 'secret-value', label: 'api_key' }
159
+ * });
160
+ * ```
161
+ */
162
+ export declare function zSensitiveDeserialize<C extends SensitivityClass>(expectedKlass?: C): SensitiveSchema<C, {
163
+ __sensitive: true;
164
+ klass: string;
165
+ value: string;
166
+ label?: string | undefined;
167
+ expiresAt?: string | undefined;
168
+ revoked?: true | undefined;
169
+ revocationReason?: string | undefined;
170
+ restrictions?: unknown[] | undefined;
171
+ destinations?: unknown[] | undefined;
172
+ }>;
173
+ //# sourceMappingURL=base.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../src/base.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,qCAAqC,CAAC;AAExF,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAElD;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,SAAS,gBAAgB,EACzD,KAAK,EAAE,CAAC,EACR,KAAK,CAAC,EAAE,MAAM,GACb,eAAe,CAAC,CAAC,EAAE,MAAM,CAAC,CAM5B;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,UAAU,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,eAAe,CAAC,WAAW,EAAE,MAAM,CAAC,CAE/E;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,WAAW,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,eAAe,CAAC,YAAY,EAAE,MAAM,CAAC,CAEjF;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,wBAAgB,wBAAwB,CAAC,CAAC,SAAS,gBAAgB,EACjE,KAAK,EAAE,CAAC,EACR,MAAM,EAAE,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,MAAM,CAAC,EACjC,KAAK,CAAC,EAAE,MAAM,GACb,eAAe,CAAC,CAAC,EAAE,MAAM,CAAC,CAM5B;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,kBAAkB,CAAC,CAAC,SAAS,gBAAgB,EAC3D,KAAK,EAAE,CAAC,EACR,KAAK,CAAC,EAAE,MAAM,GACb,CAAC,CAAC,OAAO,CAAC,eAAe,CAAC,CAAC,CAAC,GAAG,IAAI,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC,CAWjE;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,wBAAgB,qBAAqB,CAAC,CAAC,SAAS,gBAAgB,EAC9D,aAAa,CAAC,EAAE,CAAC,GAChB,eAAe,CAChB,CAAC,EACD;IACE,WAAW,EAAE,IAAI,CAAC;IAClB,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC3B,SAAS,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC/B,OAAO,CAAC,EAAE,IAAI,GAAG,SAAS,CAAC;IAC3B,gBAAgB,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACtC,YAAY,CAAC,EAAE,OAAO,EAAE,GAAG,SAAS,CAAC;IACrC,YAAY,CAAC,EAAE,OAAO,EAAE,GAAG,SAAS,CAAC;CACtC,CACF,CA8CA"}
package/dist/base.js ADDED
@@ -0,0 +1,238 @@
1
+ "use strict";
2
+ /**
3
+ * The base schema factories: plain strings, validated strings, optional
4
+ * fields, and the serialized wire format.
5
+ *
6
+ * @packageDocumentation
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.zSensitiveString = zSensitiveString;
10
+ exports.zSensitive = zSensitive;
11
+ exports.zCredential = zCredential;
12
+ exports.zSensitiveWithValidation = zSensitiveWithValidation;
13
+ exports.zSensitiveOptional = zSensitiveOptional;
14
+ exports.zSensitiveDeserialize = zSensitiveDeserialize;
15
+ const zod_1 = require("zod");
16
+ const sensitive_string_core_1 = require("@platform-sec/sensitive-string-core");
17
+ const refine_js_1 = require("./refine.js");
18
+ /**
19
+ * Create a Zod schema that parses strings into SensitiveString instances.
20
+ *
21
+ * @typeParam C - The sensitivity class for the resulting SensitiveString
22
+ * @param klass - The sensitivity classification to apply
23
+ * @param label - Optional label for audit/compliance categorization
24
+ * @returns A Zod schema that transforms strings to SensitiveString<C>
25
+ *
26
+ * @example
27
+ * ```ts
28
+ * const schema = z.object({
29
+ * apiKey: zSensitiveString('credential', 'api_key'),
30
+ * email: zSensitiveString('sensitive', 'email'),
31
+ * });
32
+ *
33
+ * const result = schema.parse({
34
+ * apiKey: 'sk-xxx',
35
+ * email: 'user@example.com',
36
+ * });
37
+ *
38
+ * console.log(result.apiKey); // SensitiveString<credential:api_key> { [REDACTED:credential] }
39
+ * ```
40
+ */
41
+ function zSensitiveString(klass, label) {
42
+ return (0, refine_js_1.withSensitiveMethods)(zod_1.z.string().transform((value) => {
43
+ return new sensitive_string_core_1.SensitiveString(value, klass, label);
44
+ }));
45
+ }
46
+ /**
47
+ * Create a Zod schema for sensitive data (PII, business data, etc.).
48
+ * Shorthand for zSensitiveString('sensitive', label).
49
+ *
50
+ * @param label - Optional label for audit/compliance (e.g., 'email', 'phone', 'address')
51
+ * @returns A Zod schema that transforms strings to SensitiveString<'sensitive'>
52
+ *
53
+ * @example
54
+ * ```ts
55
+ * const schema = z.object({
56
+ * email: zSensitive('email'),
57
+ * phone: zSensitive('phone'),
58
+ * name: zSensitive('name'),
59
+ * });
60
+ * ```
61
+ */
62
+ function zSensitive(label) {
63
+ return zSensitiveString('sensitive', label);
64
+ }
65
+ /**
66
+ * Create a Zod schema for credential data (passwords, API keys, tokens).
67
+ * Shorthand for zSensitiveString('credential', label).
68
+ * Values will use constant-time comparison to prevent timing attacks.
69
+ *
70
+ * @param label - Optional label for audit/compliance (e.g., 'api_key', 'password', 'session_token')
71
+ * @returns A Zod schema that transforms strings to SensitiveString<'credential'>
72
+ *
73
+ * @example
74
+ * ```ts
75
+ * const schema = z.object({
76
+ * password: zCredential('password'),
77
+ * apiKey: zCredential('api_key'),
78
+ * sessionToken: zCredential('session_token'),
79
+ * });
80
+ * ```
81
+ */
82
+ function zCredential(label) {
83
+ return zSensitiveString('credential', label);
84
+ }
85
+ /**
86
+ * Create a Zod schema with additional string validation before wrapping.
87
+ *
88
+ * The schema parameter is any string-to-string schema, not specifically a
89
+ * `z.ZodString`. Zod 4 promoted the string formats to top-level constructors
90
+ * with classes of their own, so `z.email()` is a `ZodEmail` and is not a
91
+ * `ZodString`; requiring the narrower type would push callers back onto the
92
+ * deprecated `z.string().email()` spelling to use this package.
93
+ *
94
+ * @typeParam C - The sensitivity class for the resulting SensitiveString
95
+ * @param klass - The sensitivity classification to apply
96
+ * @param schema - A Zod schema that takes and returns a string
97
+ * @param label - Optional label for audit/compliance
98
+ * @returns A Zod schema that validates then transforms to SensitiveString<C>
99
+ *
100
+ * @example
101
+ * ```ts
102
+ * // API key must be at least 32 characters
103
+ * const apiKeySchema = zSensitiveWithValidation(
104
+ * 'credential',
105
+ * z.string().min(32, 'API key too short'),
106
+ * 'api_key'
107
+ * );
108
+ *
109
+ * // A v4 format constructor works directly.
110
+ * const emailSchema = zSensitiveWithValidation('sensitive', z.email(), 'email');
111
+ *
112
+ * // Password with complexity requirements
113
+ * const passwordSchema = zSensitiveWithValidation(
114
+ * 'credential',
115
+ * z.string()
116
+ * .min(8, 'Password must be at least 8 characters')
117
+ * .regex(/[A-Z]/, 'Password must contain uppercase'),
118
+ * 'password'
119
+ * );
120
+ * ```
121
+ */
122
+ function zSensitiveWithValidation(klass, schema, label) {
123
+ return (0, refine_js_1.withSensitiveMethods)(schema.transform((value) => {
124
+ return new sensitive_string_core_1.SensitiveString(value, klass, label);
125
+ }));
126
+ }
127
+ /**
128
+ * Create a Zod schema for optional sensitive strings.
129
+ * Returns null for undefined/null input, SensitiveString for valid strings.
130
+ *
131
+ * @typeParam C - The sensitivity class for the resulting SensitiveString
132
+ * @param klass - The sensitivity classification to apply
133
+ * @param label - Optional label for audit/compliance
134
+ * @returns A Zod schema that transforms optional strings
135
+ *
136
+ * @example
137
+ * ```ts
138
+ * const schema = z.object({
139
+ * apiKey: zSensitiveOptional('credential', 'api_key'),
140
+ * });
141
+ *
142
+ * schema.parse({ apiKey: undefined }); // { apiKey: null }
143
+ * schema.parse({ apiKey: 'sk-xxx' }); // { apiKey: SensitiveString<credential> }
144
+ * ```
145
+ */
146
+ function zSensitiveOptional(klass, label) {
147
+ return zod_1.z
148
+ .string()
149
+ .nullable()
150
+ .optional()
151
+ .transform((value) => {
152
+ if (value == null || value === '') {
153
+ return null;
154
+ }
155
+ return new sensitive_string_core_1.SensitiveString(value, klass, label);
156
+ });
157
+ }
158
+ /**
159
+ * Create a Zod schema that validates against a serialized format
160
+ * and reconstructs the SensitiveString.
161
+ *
162
+ * Useful for validating incoming payloads that were serialized with serialize().
163
+ *
164
+ * Reconstruction is delegated to `SensitiveString.deserialize()`, so this path
165
+ * enforces exactly what the core wire format does: the sensitivity class must be
166
+ * registered, a deadline carried by the payload is carried into the value, a
167
+ * payload that marks the value revoked produces a revoked value, and environment
168
+ * restrictions and destination bindings arrive with it. Rebuilding the value
169
+ * here instead would silently drop all four, which fails open: an expiring
170
+ * token would arrive as one that never expires, a revoked one as live, one
171
+ * restricted to the EU as readable anywhere, and one bound to a host as
172
+ * sendable to any.
173
+ *
174
+ * A class mismatch, an unregistered class, and a malformed payload are all
175
+ * reported as Zod issues, so `safeParse` reports them rather than throwing.
176
+ *
177
+ * @typeParam C - The expected sensitivity class
178
+ * @param expectedKlass - Optional expected class (validates if provided)
179
+ * @returns A Zod schema that parses serialized forms to SensitiveString
180
+ *
181
+ * @example
182
+ * ```ts
183
+ * const schema = z.object({
184
+ * token: zSensitiveDeserialize('credential'),
185
+ * });
186
+ *
187
+ * // Validates serialized structure and reconstructs
188
+ * const result = schema.parse({
189
+ * token: { __sensitive: true, klass: 'credential', value: 'secret-value', label: 'api_key' }
190
+ * });
191
+ * ```
192
+ */
193
+ function zSensitiveDeserialize(expectedKlass) {
194
+ return (0, refine_js_1.withSensitiveMethods)(zod_1.z
195
+ .object({
196
+ __sensitive: zod_1.z.literal(true),
197
+ klass: zod_1.z.string(),
198
+ value: zod_1.z.string(),
199
+ label: zod_1.z.string().optional(),
200
+ expiresAt: zod_1.z.string().optional(),
201
+ // Declared so the object schema does not strip them before
202
+ // `deserialize()` sees them. An unlisted key is dropped, and dropping
203
+ // these would turn a payload that says "do not use this" into one that
204
+ // says nothing, or a value restricted to the EU into one readable
205
+ // anywhere, or one bound to a single host into one that may go to any,
206
+ // which is the same fail-open shape as losing the deadline. Validating
207
+ // them is still core's job, so all four are accepted loosely here and
208
+ // rejected there.
209
+ revoked: zod_1.z.literal(true).optional(),
210
+ revocationReason: zod_1.z.string().optional(),
211
+ restrictions: zod_1.z.array(zod_1.z.unknown()).optional(),
212
+ destinations: zod_1.z.array(zod_1.z.unknown()).optional(),
213
+ })
214
+ .transform((serialized, ctx) => {
215
+ // Reported as issues rather than thrown. A throw from inside a
216
+ // transform escapes `safeParse`, so a caller who chose the
217
+ // non-throwing API would get an exception anyway, from the one schema
218
+ // whose whole job is handling a payload from another service.
219
+ if (expectedKlass && serialized.klass !== expectedKlass) {
220
+ ctx.addIssue({
221
+ code: 'custom',
222
+ message: `Expected sensitivity class '${expectedKlass}', got '${serialized.klass}'`,
223
+ });
224
+ return zod_1.z.NEVER;
225
+ }
226
+ try {
227
+ return sensitive_string_core_1.SensitiveString.deserialize(serialized);
228
+ }
229
+ catch (error) {
230
+ ctx.addIssue({
231
+ code: 'custom',
232
+ message: error instanceof Error ? error.message : 'Invalid serialized sensitive value',
233
+ });
234
+ return zod_1.z.NEVER;
235
+ }
236
+ }));
237
+ }
238
+ //# sourceMappingURL=base.js.map
@@ -0,0 +1 @@
1
+ {"version":3,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AAC,KAAK,KAAK,aAAa,EAAE,CAAC;YACxD,GAAG,CAAC,QAAQ,CAAC;gBACX,IAAI,EAAE,QAAQ;gBACd,OAAO,EAAE,+BAA+B,aAAa,WAAW,UAAU,CAAC,KAAK,GAAG;aACpF,CAAC,CAAC;YACH,OAAO,OAAC,CAAC,KAAK,CAAC;QACjB,CAAC;QAED,IAAI,CAAC;YACH,OAAO,uCAAe,CAAC,WAAW,CAAI,UAAU,CAAC,CAAC;QACpD,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,GAAG,CAAC,QAAQ,CAAC;gBACX,IAAI,EAAE,QAAQ;gBACd,OAAO,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,oCAAoC;aACvF,CAAC,CAAC;YACH,OAAO,OAAC,CAAC,KAAK,CAAC;QACjB,CAAC;IACH,CAAC,CAAC,CACL,CAAC;AACJ,CAAC"}
@@ -0,0 +1,109 @@
1
+ /**
2
+ * Arrays and records of sensitive values.
3
+ *
4
+ * Each helper wraps a per-element sensitive schema in a plain `z.array` or
5
+ * `z.record`, rather than transforming a whole collection at once. That keeps
6
+ * the collection combinators available (`.min()`, `.max()`, `.nonempty()`) and
7
+ * puts a failing element's index or key in the issue path, where a whole-array
8
+ * transform would report only against the array itself.
9
+ *
10
+ * @packageDocumentation
11
+ */
12
+ import { z } from 'zod';
13
+ import type { SensitivityClass } from '@platform-sec/sensitive-string-core';
14
+ import type { SensitiveSchema } from './types.js';
15
+ /**
16
+ * A schema for an array of sensitive values.
17
+ *
18
+ * Exposed as its own name because the element schema is an intersection type
19
+ * and spelling it out at every call site is noise.
20
+ */
21
+ export type SensitiveArraySchema<C extends SensitivityClass> = z.ZodArray<SensitiveSchema<C>>;
22
+ /**
23
+ * A schema for a record of sensitive values, keyed by plain strings.
24
+ *
25
+ * Keys stay plain. A key is a field name, not the secret, and redacting it
26
+ * would leave the record unusable for lookup.
27
+ */
28
+ export type SensitiveRecordSchema<C extends SensitivityClass> = z.ZodRecord<z.ZodString, SensitiveSchema<C>>;
29
+ /**
30
+ * Create a schema for an array whose elements are wrapped individually.
31
+ *
32
+ * @typeParam C - The sensitivity class applied to every element
33
+ * @param klass - The sensitivity classification to apply
34
+ * @param label - Optional label shared by every element
35
+ * @returns A ZodArray of sensitive values
36
+ *
37
+ * @example
38
+ * ```ts
39
+ * const schema = zSensitiveStringArray('credential', 'api_key').min(1);
40
+ * const keys = schema.parse(['sk-a', 'sk-b']);
41
+ * // keys[0] is SensitiveString<'credential'>
42
+ * ```
43
+ */
44
+ export declare function zSensitiveStringArray<C extends SensitivityClass>(klass: C, label?: string): SensitiveArraySchema<C>;
45
+ /**
46
+ * Create a schema for an array of sensitive data (PII, business data).
47
+ * Shorthand for zSensitiveStringArray('sensitive', label).
48
+ *
49
+ * @param label - Optional label shared by every element
50
+ * @returns A ZodArray of SensitiveString<'sensitive'>
51
+ *
52
+ * @example
53
+ * ```ts
54
+ * const schema = z.object({ recipients: zSensitiveArray('email') });
55
+ * schema.parse({ recipients: ['a@example.com', 'b@example.com'] });
56
+ * ```
57
+ */
58
+ export declare function zSensitiveArray(label?: string): SensitiveArraySchema<'sensitive'>;
59
+ /**
60
+ * Create a schema for an array of credentials.
61
+ * Shorthand for zSensitiveStringArray('credential', label).
62
+ *
63
+ * @param label - Optional label shared by every element
64
+ * @returns A ZodArray of SensitiveString<'credential'>
65
+ *
66
+ * @example
67
+ * ```ts
68
+ * const schema = z.object({ keys: zCredentialArray('api_key') });
69
+ * ```
70
+ */
71
+ export declare function zCredentialArray(label?: string): SensitiveArraySchema<'credential'>;
72
+ /**
73
+ * Create a schema for a record whose values are wrapped individually.
74
+ *
75
+ * @typeParam C - The sensitivity class applied to every value
76
+ * @param klass - The sensitivity classification to apply
77
+ * @param label - Optional label shared by every value
78
+ * @returns A ZodRecord of sensitive values
79
+ *
80
+ * @example
81
+ * ```ts
82
+ * const schema = zSensitiveStringRecord('credential', 'secret');
83
+ * const secrets = schema.parse({ stripe: 'sk-a', twilio: 'sk-b' });
84
+ * // secrets.stripe is SensitiveString<'credential'>
85
+ * ```
86
+ */
87
+ export declare function zSensitiveStringRecord<C extends SensitivityClass>(klass: C, label?: string): SensitiveRecordSchema<C>;
88
+ /**
89
+ * Create a schema for a record of sensitive data.
90
+ * Shorthand for zSensitiveStringRecord('sensitive', label).
91
+ *
92
+ * @param label - Optional label shared by every value
93
+ * @returns A ZodRecord of SensitiveString<'sensitive'>
94
+ */
95
+ export declare function zSensitiveRecord(label?: string): SensitiveRecordSchema<'sensitive'>;
96
+ /**
97
+ * Create a schema for a record of credentials.
98
+ * Shorthand for zSensitiveStringRecord('credential', label).
99
+ *
100
+ * @param label - Optional label shared by every value
101
+ * @returns A ZodRecord of SensitiveString<'credential'>
102
+ *
103
+ * @example
104
+ * ```ts
105
+ * const schema = zCredentialRecord('secret');
106
+ * ```
107
+ */
108
+ export declare function zCredentialRecord(label?: string): SensitiveRecordSchema<'credential'>;
109
+ //# sourceMappingURL=collections.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"collections.d.ts","sourceRoot":"","sources":["../src/collections.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,qCAAqC,CAAC;AAE5E,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAElD;;;;;GAKG;AACH,MAAM,MAAM,oBAAoB,CAAC,CAAC,SAAS,gBAAgB,IAAI,CAAC,CAAC,QAAQ,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC,CAAC;AAE9F;;;;;GAKG;AACH,MAAM,MAAM,qBAAqB,CAAC,CAAC,SAAS,gBAAgB,IAAI,CAAC,CAAC,SAAS,CACzE,CAAC,CAAC,SAAS,EACX,eAAe,CAAC,CAAC,CAAC,CACnB,CAAC;AAEF;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,qBAAqB,CAAC,CAAC,SAAS,gBAAgB,EAC9D,KAAK,EAAE,CAAC,EACR,KAAK,CAAC,EAAE,MAAM,GACb,oBAAoB,CAAC,CAAC,CAAC,CAEzB;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,eAAe,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,oBAAoB,CAAC,WAAW,CAAC,CAEjF;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,oBAAoB,CAAC,YAAY,CAAC,CAEnF;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,sBAAsB,CAAC,CAAC,SAAS,gBAAgB,EAC/D,KAAK,EAAE,CAAC,EACR,KAAK,CAAC,EAAE,MAAM,GACb,qBAAqB,CAAC,CAAC,CAAC,CAE1B;AAED;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,qBAAqB,CAAC,WAAW,CAAC,CAEnF;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,qBAAqB,CAAC,YAAY,CAAC,CAErF"}
@@ -0,0 +1,114 @@
1
+ "use strict";
2
+ /**
3
+ * Arrays and records of sensitive values.
4
+ *
5
+ * Each helper wraps a per-element sensitive schema in a plain `z.array` or
6
+ * `z.record`, rather than transforming a whole collection at once. That keeps
7
+ * the collection combinators available (`.min()`, `.max()`, `.nonempty()`) and
8
+ * puts a failing element's index or key in the issue path, where a whole-array
9
+ * transform would report only against the array itself.
10
+ *
11
+ * @packageDocumentation
12
+ */
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.zSensitiveStringArray = zSensitiveStringArray;
15
+ exports.zSensitiveArray = zSensitiveArray;
16
+ exports.zCredentialArray = zCredentialArray;
17
+ exports.zSensitiveStringRecord = zSensitiveStringRecord;
18
+ exports.zSensitiveRecord = zSensitiveRecord;
19
+ exports.zCredentialRecord = zCredentialRecord;
20
+ const zod_1 = require("zod");
21
+ const base_js_1 = require("./base.js");
22
+ /**
23
+ * Create a schema for an array whose elements are wrapped individually.
24
+ *
25
+ * @typeParam C - The sensitivity class applied to every element
26
+ * @param klass - The sensitivity classification to apply
27
+ * @param label - Optional label shared by every element
28
+ * @returns A ZodArray of sensitive values
29
+ *
30
+ * @example
31
+ * ```ts
32
+ * const schema = zSensitiveStringArray('credential', 'api_key').min(1);
33
+ * const keys = schema.parse(['sk-a', 'sk-b']);
34
+ * // keys[0] is SensitiveString<'credential'>
35
+ * ```
36
+ */
37
+ function zSensitiveStringArray(klass, label) {
38
+ return zod_1.z.array((0, base_js_1.zSensitiveString)(klass, label));
39
+ }
40
+ /**
41
+ * Create a schema for an array of sensitive data (PII, business data).
42
+ * Shorthand for zSensitiveStringArray('sensitive', label).
43
+ *
44
+ * @param label - Optional label shared by every element
45
+ * @returns A ZodArray of SensitiveString<'sensitive'>
46
+ *
47
+ * @example
48
+ * ```ts
49
+ * const schema = z.object({ recipients: zSensitiveArray('email') });
50
+ * schema.parse({ recipients: ['a@example.com', 'b@example.com'] });
51
+ * ```
52
+ */
53
+ function zSensitiveArray(label) {
54
+ return zSensitiveStringArray('sensitive', label);
55
+ }
56
+ /**
57
+ * Create a schema for an array of credentials.
58
+ * Shorthand for zSensitiveStringArray('credential', label).
59
+ *
60
+ * @param label - Optional label shared by every element
61
+ * @returns A ZodArray of SensitiveString<'credential'>
62
+ *
63
+ * @example
64
+ * ```ts
65
+ * const schema = z.object({ keys: zCredentialArray('api_key') });
66
+ * ```
67
+ */
68
+ function zCredentialArray(label) {
69
+ return zSensitiveStringArray('credential', label);
70
+ }
71
+ /**
72
+ * Create a schema for a record whose values are wrapped individually.
73
+ *
74
+ * @typeParam C - The sensitivity class applied to every value
75
+ * @param klass - The sensitivity classification to apply
76
+ * @param label - Optional label shared by every value
77
+ * @returns A ZodRecord of sensitive values
78
+ *
79
+ * @example
80
+ * ```ts
81
+ * const schema = zSensitiveStringRecord('credential', 'secret');
82
+ * const secrets = schema.parse({ stripe: 'sk-a', twilio: 'sk-b' });
83
+ * // secrets.stripe is SensitiveString<'credential'>
84
+ * ```
85
+ */
86
+ function zSensitiveStringRecord(klass, label) {
87
+ return zod_1.z.record(zod_1.z.string(), (0, base_js_1.zSensitiveString)(klass, label));
88
+ }
89
+ /**
90
+ * Create a schema for a record of sensitive data.
91
+ * Shorthand for zSensitiveStringRecord('sensitive', label).
92
+ *
93
+ * @param label - Optional label shared by every value
94
+ * @returns A ZodRecord of SensitiveString<'sensitive'>
95
+ */
96
+ function zSensitiveRecord(label) {
97
+ return zSensitiveStringRecord('sensitive', label);
98
+ }
99
+ /**
100
+ * Create a schema for a record of credentials.
101
+ * Shorthand for zSensitiveStringRecord('credential', label).
102
+ *
103
+ * @param label - Optional label shared by every value
104
+ * @returns A ZodRecord of SensitiveString<'credential'>
105
+ *
106
+ * @example
107
+ * ```ts
108
+ * const schema = zCredentialRecord('secret');
109
+ * ```
110
+ */
111
+ function zCredentialRecord(label) {
112
+ return zSensitiveStringRecord('credential', label);
113
+ }
114
+ //# sourceMappingURL=collections.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"collections.js","sourceRoot":"","sources":["../src/collections.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;GAUG;;AAyCH,sDAKC;AAeD,0CAEC;AAcD,4CAEC;AAiBD,wDAKC;AASD,4CAEC;AAcD,8CAEC;AA9HD,6BAAwB;AAExB,uCAA6C;AAsB7C;;;;;;;;;;;;;;GAcG;AACH,SAAgB,qBAAqB,CACnC,KAAQ,EACR,KAAc;IAEd,OAAO,OAAC,CAAC,KAAK,CAAC,IAAA,0BAAgB,EAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC;AACjD,CAAC;AAED;;;;;;;;;;;;GAYG;AACH,SAAgB,eAAe,CAAC,KAAc;IAC5C,OAAO,qBAAqB,CAAC,WAAW,EAAE,KAAK,CAAC,CAAC;AACnD,CAAC;AAED;;;;;;;;;;;GAWG;AACH,SAAgB,gBAAgB,CAAC,KAAc;IAC7C,OAAO,qBAAqB,CAAC,YAAY,EAAE,KAAK,CAAC,CAAC;AACpD,CAAC;AAED;;;;;;;;;;;;;;GAcG;AACH,SAAgB,sBAAsB,CACpC,KAAQ,EACR,KAAc;IAEd,OAAO,OAAC,CAAC,MAAM,CAAC,OAAC,CAAC,MAAM,EAAE,EAAE,IAAA,0BAAgB,EAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC;AAC9D,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,gBAAgB,CAAC,KAAc;IAC7C,OAAO,sBAAsB,CAAC,WAAW,EAAE,KAAK,CAAC,CAAC;AACpD,CAAC;AAED;;;;;;;;;;;GAWG;AACH,SAAgB,iBAAiB,CAAC,KAAc;IAC9C,OAAO,sBAAsB,CAAC,YAAY,EAAE,KAAK,CAAC,CAAC;AACrD,CAAC"}