@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 +173 -0
- package/dist/base.d.ts.map +1 -0
- package/dist/base.js +238 -0
- package/dist/base.js.map +1 -0
- package/dist/collections.d.ts +109 -0
- package/dist/collections.d.ts.map +1 -0
- package/dist/collections.js +114 -0
- package/dist/collections.js.map +1 -0
- package/dist/conditional.d.ts +101 -0
- package/dist/conditional.d.ts.map +1 -0
- package/dist/conditional.js +85 -0
- package/dist/conditional.js.map +1 -0
- package/dist/defaults.d.ts +48 -0
- package/dist/defaults.d.ts.map +1 -0
- package/dist/defaults.js +60 -0
- package/dist/defaults.js.map +1 -0
- package/dist/error-map.d.ts +180 -0
- package/dist/error-map.d.ts.map +1 -0
- package/dist/error-map.js +311 -0
- package/dist/error-map.js.map +1 -0
- package/dist/index.d.ts +55 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +91 -0
- package/dist/index.js.map +1 -0
- package/dist/preprocessing.d.ts +106 -0
- package/dist/preprocessing.d.ts.map +1 -0
- package/dist/preprocessing.js +149 -0
- package/dist/preprocessing.js.map +1 -0
- package/dist/refine.d.ts +61 -0
- package/dist/refine.d.ts.map +1 -0
- package/dist/refine.js +114 -0
- package/dist/refine.js.map +1 -0
- package/dist/types.d.ts +75 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +8 -0
- package/dist/types.js.map +1 -0
- package/package.json +11 -11
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Normalizing input before it is wrapped.
|
|
4
|
+
*
|
|
5
|
+
* Normalization has to happen before wrapping: once a value is a
|
|
6
|
+
* SensitiveString, trimming it means revealing it. Doing it here keeps the
|
|
7
|
+
* whole normalize-validate-wrap sequence on the raw string, inside the parse,
|
|
8
|
+
* with no reveal at all.
|
|
9
|
+
*
|
|
10
|
+
* Order is normalize, then validate, then wrap. That matches Zod's own
|
|
11
|
+
* `preprocess`, and it is the order callers expect: `' A@B.COM '` should pass
|
|
12
|
+
* an email check.
|
|
13
|
+
*
|
|
14
|
+
* @packageDocumentation
|
|
15
|
+
*/
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.zSensitiveStringPreprocess = zSensitiveStringPreprocess;
|
|
18
|
+
exports.zSensitivePreprocess = zSensitivePreprocess;
|
|
19
|
+
exports.zCredentialPreprocess = zCredentialPreprocess;
|
|
20
|
+
const zod_1 = require("zod");
|
|
21
|
+
const sensitive_string_core_1 = require("@platform-sec/sensitive-string-core");
|
|
22
|
+
const refine_js_1 = require("./refine.js");
|
|
23
|
+
/**
|
|
24
|
+
* Turn a spec into a single function over the raw string.
|
|
25
|
+
*
|
|
26
|
+
* @throws TypeError If `lowercase` and `uppercase` are both set, or a step is not a function.
|
|
27
|
+
*/
|
|
28
|
+
function compileSpec(spec) {
|
|
29
|
+
if (typeof spec === 'function') {
|
|
30
|
+
return spec;
|
|
31
|
+
}
|
|
32
|
+
if (Array.isArray(spec)) {
|
|
33
|
+
const steps = spec;
|
|
34
|
+
for (const step of steps) {
|
|
35
|
+
if (typeof step !== 'function') {
|
|
36
|
+
throw new TypeError('Preprocess steps must be functions');
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return (value) => steps.reduce((acc, step) => step(acc), value);
|
|
40
|
+
}
|
|
41
|
+
const options = spec;
|
|
42
|
+
if (options.lowercase === true && options.uppercase === true) {
|
|
43
|
+
throw new TypeError('Preprocess options cannot set both lowercase and uppercase');
|
|
44
|
+
}
|
|
45
|
+
return (value) => {
|
|
46
|
+
let result = value;
|
|
47
|
+
if (options.normalize !== undefined) {
|
|
48
|
+
result = result.normalize(options.normalize);
|
|
49
|
+
}
|
|
50
|
+
if (options.collapseWhitespace === true) {
|
|
51
|
+
result = result.replace(/\s+/gu, ' ');
|
|
52
|
+
}
|
|
53
|
+
if (options.stripWhitespace === true) {
|
|
54
|
+
result = result.replace(/\s+/gu, '');
|
|
55
|
+
}
|
|
56
|
+
if (options.trim === true) {
|
|
57
|
+
result = result.trim();
|
|
58
|
+
}
|
|
59
|
+
if (options.lowercase === true) {
|
|
60
|
+
result = result.toLowerCase();
|
|
61
|
+
}
|
|
62
|
+
if (options.uppercase === true) {
|
|
63
|
+
result = result.toUpperCase();
|
|
64
|
+
}
|
|
65
|
+
return result;
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Create a schema that normalizes the input, validates it, then wraps it.
|
|
70
|
+
*
|
|
71
|
+
* A non-string input is passed through untouched, so the inner string schema
|
|
72
|
+
* reports the usual `invalid_type` issue rather than the preprocessor throwing
|
|
73
|
+
* on it.
|
|
74
|
+
*
|
|
75
|
+
* The inner schema is any string-to-string schema, not specifically a
|
|
76
|
+
* `z.ZodString`, so that Zod 4's format constructors work: `z.email()` is a
|
|
77
|
+
* `ZodEmail` and is not a `ZodString`.
|
|
78
|
+
*
|
|
79
|
+
* @typeParam C - The sensitivity class for the resulting SensitiveString
|
|
80
|
+
* @param klass - The sensitivity classification to apply
|
|
81
|
+
* @param spec - Normalization options, a function, or a list of functions
|
|
82
|
+
* @param label - Optional label for audit/compliance
|
|
83
|
+
* @param schema - Optional string-to-string schema, validated after normalization
|
|
84
|
+
* @returns A schema that transforms normalized strings to SensitiveString<C>
|
|
85
|
+
* @throws TypeError If the spec is malformed.
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```ts
|
|
89
|
+
* const email = zSensitiveStringPreprocess(
|
|
90
|
+
* 'sensitive',
|
|
91
|
+
* { trim: true, lowercase: true },
|
|
92
|
+
* 'email',
|
|
93
|
+
* z.email()
|
|
94
|
+
* );
|
|
95
|
+
*
|
|
96
|
+
* email.parse(' Alice@Example.COM ').reveal(); // 'alice@example.com'
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
function zSensitiveStringPreprocess(klass, spec, label, schema) {
|
|
100
|
+
const normalize = compileSpec(spec);
|
|
101
|
+
const built = zod_1.z
|
|
102
|
+
.preprocess((value) => (typeof value === 'string' ? normalize(value) : value), schema ?? zod_1.z.string())
|
|
103
|
+
.transform((value) => new sensitive_string_core_1.SensitiveString(value, klass, label));
|
|
104
|
+
// `z.preprocess` declares its input as a required `unknown`, which is wider
|
|
105
|
+
// than the string this schema actually asks for and would let a caller pass
|
|
106
|
+
// anything without a compile error. The cast narrows it to that string.
|
|
107
|
+
//
|
|
108
|
+
// Under v3 the same cast fixed a different problem: `undefined` extends
|
|
109
|
+
// `unknown`, so the declared input marked the field optional wherever this
|
|
110
|
+
// schema sat in an object. v4 closed that, and the runtime case pinning it
|
|
111
|
+
// still passes.
|
|
112
|
+
return (0, refine_js_1.withSensitiveMethods)(built);
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Normalize, then wrap as sensitive data.
|
|
116
|
+
* Shorthand for zSensitiveStringPreprocess('sensitive', spec, label, schema).
|
|
117
|
+
*
|
|
118
|
+
* @param spec - Normalization options, a function, or a list of functions
|
|
119
|
+
* @param label - Optional label for audit/compliance
|
|
120
|
+
* @param schema - Optional string-to-string schema, validated after normalization
|
|
121
|
+
* @returns A schema producing SensitiveString<'sensitive'>
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* ```ts
|
|
125
|
+
* const schema = zSensitivePreprocess({ trim: true, lowercase: true }, 'email');
|
|
126
|
+
* ```
|
|
127
|
+
*/
|
|
128
|
+
function zSensitivePreprocess(spec, label, schema) {
|
|
129
|
+
return zSensitiveStringPreprocess('sensitive', spec, label, schema);
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Normalize, then wrap as a credential.
|
|
133
|
+
* Shorthand for zSensitiveStringPreprocess('credential', spec, label, schema).
|
|
134
|
+
*
|
|
135
|
+
* @param spec - Normalization options, a function, or a list of functions
|
|
136
|
+
* @param label - Optional label for audit/compliance
|
|
137
|
+
* @param schema - Optional string-to-string schema, validated after normalization
|
|
138
|
+
* @returns A schema producing SensitiveString<'credential'>
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* ```ts
|
|
142
|
+
* // An API key pasted from a console arrives with line breaks in it.
|
|
143
|
+
* const apiKey = zCredentialPreprocess({ stripWhitespace: true }, 'api_key');
|
|
144
|
+
* ```
|
|
145
|
+
*/
|
|
146
|
+
function zCredentialPreprocess(spec, label, schema) {
|
|
147
|
+
return zSensitiveStringPreprocess('credential', spec, label, schema);
|
|
148
|
+
}
|
|
149
|
+
//# sourceMappingURL=preprocessing.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"preprocessing.js","sourceRoot":"","sources":["../src/preprocessing.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;GAaG;;AAmHH,gEAwBC;AAgBD,oDAMC;AAiBD,sDAMC;AAtLD,6BAAwB;AACxB,+EAAwF;AACxF,2CAAmD;AA+BnD;;;;GAIG;AACH,SAAS,WAAW,CAAC,IAAoB;IACvC,IAAI,OAAO,IAAI,KAAK,UAAU,EAAE,CAAC;QAC/B,OAAO,IAAI,CAAC;IACd,CAAC;IAED,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;QACxB,MAAM,KAAK,GAAG,IAAgD,CAAC;QAC/D,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;YACzB,IAAI,OAAO,IAAI,KAAK,UAAU,EAAE,CAAC;gBAC/B,MAAM,IAAI,SAAS,CAAC,oCAAoC,CAAC,CAAC;YAC5D,CAAC;QACH,CAAC;QACD,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,KAAK,CAAC,CAAC;IAClE,CAAC;IAED,MAAM,OAAO,GAAG,IAAyB,CAAC;IAC1C,IAAI,OAAO,CAAC,SAA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,KAAK,EAAE,EAAE,CAAC,CAAC,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,EACjE,MAAM,IAAI,OAAC,CAAC,MAAM,EAAE,CACrB;SACA,SAAS,CAAC,CAAC,KAAK,EAAsB,EAAE,CAAC,IAAI,uCAAe,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC;IAEtF,4EAA4E;IAC5E,4EAA4E;IAC5E,wEAAwE;IACxE,EAAE;IACF,wEAAwE;IACxE,2EAA2E;IAC3E,2EAA2E;IAC3E,gBAAgB;IAChB,OAAO,IAAA,gCAAoB,EAAC,KAAyD,CAAC,CAAC;AACzF,CAAC;AAED;;;;;;;;;;;;;GAaG;AACH,SAAgB,oBAAoB,CAClC,IAAoB,EACpB,KAAc,EACd,MAAkC;IAElC,OAAO,0BAA0B,CAAC,WAAW,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC;AACtE,CAAC;AAED;;;;;;;;;;;;;;GAcG;AACH,SAAgB,qBAAqB,CACnC,IAAoB,EACpB,KAAc,EACd,MAAkC;IAElC,OAAO,0BAA0B,CAAC,YAAY,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC;AACvE,CAAC"}
|
package/dist/refine.d.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Refinements that need the raw value.
|
|
3
|
+
*
|
|
4
|
+
* A refinement that checks a password's shape, or an API key's prefix, has to
|
|
5
|
+
* see the string. Running it before wrapping would hide that read from the
|
|
6
|
+
* audit trail, so these helpers run after wrapping and read through
|
|
7
|
+
* `reveal()`: the same authorization and the same audit hook as any other
|
|
8
|
+
* reveal in the codebase.
|
|
9
|
+
*
|
|
10
|
+
* @packageDocumentation
|
|
11
|
+
*/
|
|
12
|
+
import { z } from 'zod';
|
|
13
|
+
import type { SensitiveString, SensitivityClass } from '@platform-sec/sensitive-string-core';
|
|
14
|
+
import type { RefineSensitiveParams, SensitiveCheck, SensitiveSchema } from './types.js';
|
|
15
|
+
/**
|
|
16
|
+
* Refine a schema that produces a SensitiveString, using the raw value.
|
|
17
|
+
*
|
|
18
|
+
* The predicate receives the revealed string. That read goes through
|
|
19
|
+
* `reveal()`, so an ambient `RevealContext` authorizes it and its
|
|
20
|
+
* `auditReveal` hook sees it with `operation: 'reveal'`. Pass
|
|
21
|
+
* `params.context` to audit the refinement under a context of its own.
|
|
22
|
+
*
|
|
23
|
+
* @typeParam C - The sensitivity class the schema produces
|
|
24
|
+
* @typeParam Input - The schema's input type
|
|
25
|
+
* @param schema - A schema whose output is a SensitiveString
|
|
26
|
+
* @param check - Predicate over the raw value; `false` produces an issue
|
|
27
|
+
* @param params - Message, path, and reveal context for the check
|
|
28
|
+
* @returns A schema producing the same value, refined
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* const password = refineSensitive(
|
|
33
|
+
* zCredential('password'),
|
|
34
|
+
* (raw) => /[A-Z]/.test(raw),
|
|
35
|
+
* { message: 'Password must contain an uppercase letter' }
|
|
36
|
+
* );
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* @remarks
|
|
40
|
+
* An async check requires `parseAsync`. Zod throws `$ZodAsyncError` rather
|
|
41
|
+
* than skipping it if it is reached from a synchronous `parse()`.
|
|
42
|
+
*
|
|
43
|
+
* A reveal the policy refuses, or one denied by a lapsed deadline, throws out
|
|
44
|
+
* of the parse rather than becoming an issue, and that is deliberate. An issue
|
|
45
|
+
* says the input was wrong. A refused reveal says this process may not read a
|
|
46
|
+
* value it holds, which is a configuration or lifetime problem; reporting it as
|
|
47
|
+
* a validation failure would tell a client its payload was bad and hide the
|
|
48
|
+
* real cause. This is the opposite call from `zSensitiveDeserialize`, whose
|
|
49
|
+
* failures really are the payload's fault and so are reported as issues.
|
|
50
|
+
*/
|
|
51
|
+
export declare function refineSensitive<C extends SensitivityClass, Input>(schema: z.ZodType<SensitiveString<C>, Input>, check: SensitiveCheck, params?: RefineSensitiveParams): SensitiveSchema<C, Input>;
|
|
52
|
+
/**
|
|
53
|
+
* Attach `refineSensitive` to a schema this package builds.
|
|
54
|
+
*
|
|
55
|
+
* The method is non-enumerable so that it does not show up when a schema is
|
|
56
|
+
* spread, inspected, or serialized by tooling that walks own properties.
|
|
57
|
+
*
|
|
58
|
+
* @internal
|
|
59
|
+
*/
|
|
60
|
+
export declare function withSensitiveMethods<C extends SensitivityClass, Input>(schema: z.ZodType<SensitiveString<C>, Input>): SensitiveSchema<C, Input>;
|
|
61
|
+
//# sourceMappingURL=refine.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"refine.d.ts","sourceRoot":"","sources":["../src/refine.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,qCAAqC,CAAC;AAC7F,OAAO,KAAK,EACV,qBAAqB,EACrB,cAAc,EACd,eAAe,EAEhB,MAAM,YAAY,CAAC;AA0CpB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,wBAAgB,eAAe,CAAC,CAAC,SAAS,gBAAgB,EAAE,KAAK,EAC/D,MAAM,EAAE,CAAC,CAAC,OAAO,CAAC,eAAe,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,EAC5C,KAAK,EAAE,cAAc,EACrB,MAAM,CAAC,EAAE,qBAAqB,GAC7B,eAAe,CAAC,CAAC,EAAE,KAAK,CAAC,CAoB3B;AAED;;;;;;;GAOG;AACH,wBAAgB,oBAAoB,CAAC,CAAC,SAAS,gBAAgB,EAAE,KAAK,EACpE,MAAM,EAAE,CAAC,CAAC,OAAO,CAAC,eAAe,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,GAC3C,eAAe,CAAC,CAAC,EAAE,KAAK,CAAC,CAY3B"}
|
package/dist/refine.js
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Refinements that need the raw value.
|
|
4
|
+
*
|
|
5
|
+
* A refinement that checks a password's shape, or an API key's prefix, has to
|
|
6
|
+
* see the string. Running it before wrapping would hide that read from the
|
|
7
|
+
* audit trail, so these helpers run after wrapping and read through
|
|
8
|
+
* `reveal()`: the same authorization and the same audit hook as any other
|
|
9
|
+
* reveal in the codebase.
|
|
10
|
+
*
|
|
11
|
+
* @packageDocumentation
|
|
12
|
+
*/
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.refineSensitive = refineSensitive;
|
|
15
|
+
exports.withSensitiveMethods = withSensitiveMethods;
|
|
16
|
+
/**
|
|
17
|
+
* The message used when a check fails and the caller supplied none.
|
|
18
|
+
*
|
|
19
|
+
* Deliberately says nothing about the value, not even its length.
|
|
20
|
+
*/
|
|
21
|
+
const DEFAULT_REFINE_MESSAGE = 'Invalid value';
|
|
22
|
+
/**
|
|
23
|
+
* Build the issue for a failed check.
|
|
24
|
+
*
|
|
25
|
+
* `params.message` is the only caller-controlled text that reaches the issue.
|
|
26
|
+
* Nothing derived from the raw value is added here, and a caller who
|
|
27
|
+
* interpolates the value into their own message is the one leaking it.
|
|
28
|
+
*/
|
|
29
|
+
function refinementIssue(params) {
|
|
30
|
+
const issue = {
|
|
31
|
+
code: 'custom',
|
|
32
|
+
message: params?.message ?? DEFAULT_REFINE_MESSAGE,
|
|
33
|
+
};
|
|
34
|
+
if (params?.path !== undefined) {
|
|
35
|
+
issue.path = params.path;
|
|
36
|
+
}
|
|
37
|
+
if (params?.fatal !== undefined) {
|
|
38
|
+
issue.fatal = params.fatal;
|
|
39
|
+
}
|
|
40
|
+
return issue;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Refine a schema that produces a SensitiveString, using the raw value.
|
|
44
|
+
*
|
|
45
|
+
* The predicate receives the revealed string. That read goes through
|
|
46
|
+
* `reveal()`, so an ambient `RevealContext` authorizes it and its
|
|
47
|
+
* `auditReveal` hook sees it with `operation: 'reveal'`. Pass
|
|
48
|
+
* `params.context` to audit the refinement under a context of its own.
|
|
49
|
+
*
|
|
50
|
+
* @typeParam C - The sensitivity class the schema produces
|
|
51
|
+
* @typeParam Input - The schema's input type
|
|
52
|
+
* @param schema - A schema whose output is a SensitiveString
|
|
53
|
+
* @param check - Predicate over the raw value; `false` produces an issue
|
|
54
|
+
* @param params - Message, path, and reveal context for the check
|
|
55
|
+
* @returns A schema producing the same value, refined
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```ts
|
|
59
|
+
* const password = refineSensitive(
|
|
60
|
+
* zCredential('password'),
|
|
61
|
+
* (raw) => /[A-Z]/.test(raw),
|
|
62
|
+
* { message: 'Password must contain an uppercase letter' }
|
|
63
|
+
* );
|
|
64
|
+
* ```
|
|
65
|
+
*
|
|
66
|
+
* @remarks
|
|
67
|
+
* An async check requires `parseAsync`. Zod throws `$ZodAsyncError` rather
|
|
68
|
+
* than skipping it if it is reached from a synchronous `parse()`.
|
|
69
|
+
*
|
|
70
|
+
* A reveal the policy refuses, or one denied by a lapsed deadline, throws out
|
|
71
|
+
* of the parse rather than becoming an issue, and that is deliberate. An issue
|
|
72
|
+
* says the input was wrong. A refused reveal says this process may not read a
|
|
73
|
+
* value it holds, which is a configuration or lifetime problem; reporting it as
|
|
74
|
+
* a validation failure would tell a client its payload was bad and hide the
|
|
75
|
+
* real cause. This is the opposite call from `zSensitiveDeserialize`, whose
|
|
76
|
+
* failures really are the payload's fault and so are reported as issues.
|
|
77
|
+
*/
|
|
78
|
+
function refineSensitive(schema, check, params) {
|
|
79
|
+
const refined = schema.superRefine((value, ctx) => {
|
|
80
|
+
const rawValue = value.reveal(params?.context);
|
|
81
|
+
const outcome = check(rawValue);
|
|
82
|
+
if (outcome instanceof Promise) {
|
|
83
|
+
return outcome.then((passed) => {
|
|
84
|
+
if (!passed) {
|
|
85
|
+
ctx.addIssue(refinementIssue(params));
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
if (!outcome) {
|
|
90
|
+
ctx.addIssue(refinementIssue(params));
|
|
91
|
+
}
|
|
92
|
+
return;
|
|
93
|
+
});
|
|
94
|
+
return withSensitiveMethods(refined);
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Attach `refineSensitive` to a schema this package builds.
|
|
98
|
+
*
|
|
99
|
+
* The method is non-enumerable so that it does not show up when a schema is
|
|
100
|
+
* spread, inspected, or serialized by tooling that walks own properties.
|
|
101
|
+
*
|
|
102
|
+
* @internal
|
|
103
|
+
*/
|
|
104
|
+
function withSensitiveMethods(schema) {
|
|
105
|
+
const refine = (check, params) => refineSensitive(schema, check, params);
|
|
106
|
+
Object.defineProperty(schema, 'refineSensitive', {
|
|
107
|
+
value: refine,
|
|
108
|
+
enumerable: false,
|
|
109
|
+
writable: false,
|
|
110
|
+
configurable: true,
|
|
111
|
+
});
|
|
112
|
+
return schema;
|
|
113
|
+
}
|
|
114
|
+
//# sourceMappingURL=refine.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"refine.js","sourceRoot":"","sources":["../src/refine.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;GAUG;;AAuFH,0CAwBC;AAUD,oDAcC;AA5HD;;;;GAIG;AACH,MAAM,sBAAsB,GAAG,eAAe,CAAC;AAE/C;;;;;;GAMG;AACH,SAAS,eAAe,CAAC,MAAyC;IAMhE,MAAM,KAAK,GAKP;QACF,IAAI,EAAE,QAAQ;QACd,OAAO,EAAE,MAAM,EAAE,OAAO,IAAI,sBAAsB;KACnD,CAAC;IAEF,IAAI,MAAM,EAAE,IAAI,KAAK,SAAS,EAAE,CAAC;QAC/B,KAAK,CAAC,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC;IAC3B,CAA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;QACjB,QAAQ,EAAE,KAAK;QACf,YAAY,EAAE,IAAI;KACnB,CAAC,CAAC;IAEH,OAAO,MAAmC,CAAC;AAC7C,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared types for the Zod integration.
|
|
3
|
+
*
|
|
4
|
+
* @packageDocumentation
|
|
5
|
+
*/
|
|
6
|
+
import type { z } from 'zod';
|
|
7
|
+
import type { RevealContext, SensitiveString, SensitivityClass } from '@platform-sec/sensitive-string-core';
|
|
8
|
+
/**
|
|
9
|
+
* A predicate run against the raw value of an already-wrapped SensitiveString.
|
|
10
|
+
*
|
|
11
|
+
* Returning a promise is supported, but only under `parseAsync`. Zod throws
|
|
12
|
+
* `$ZodAsyncError: Encountered Promise during synchronous parse. Use
|
|
13
|
+
* .parseAsync() instead.` if a promise-returning check is reached from
|
|
14
|
+
* `parse()`, so an async check can never be silently skipped.
|
|
15
|
+
*/
|
|
16
|
+
export type SensitiveCheck = (rawValue: string) => boolean | Promise<boolean>;
|
|
17
|
+
/**
|
|
18
|
+
* Options for `refineSensitive`.
|
|
19
|
+
*/
|
|
20
|
+
export interface RefineSensitiveParams {
|
|
21
|
+
/**
|
|
22
|
+
* The message attached to the issue when the check fails.
|
|
23
|
+
*
|
|
24
|
+
* It must describe the rule, never the value: this string ends up in a
|
|
25
|
+
* `ZodError` that callers routinely log and return to clients.
|
|
26
|
+
*/
|
|
27
|
+
message?: string;
|
|
28
|
+
/** Path appended to the issue, for refinements that report against a nested field. */
|
|
29
|
+
path?: (string | number)[];
|
|
30
|
+
/**
|
|
31
|
+
* Stop parsing after this issue rather than collecting further ones.
|
|
32
|
+
*
|
|
33
|
+
* Zod 4 spells this `continue: false` internally and keeps `fatal` as a
|
|
34
|
+
* documented v3 compatibility path. The name stays here because it is the
|
|
35
|
+
* public API of this package, and a test pins that it still aborts.
|
|
36
|
+
*/
|
|
37
|
+
fatal?: boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Reveal context used to read the raw value.
|
|
40
|
+
*
|
|
41
|
+
* Left unset, the check reveals under whatever context is ambient, exactly
|
|
42
|
+
* as a `reveal()` written by hand at the same point would. The refinement
|
|
43
|
+
* deliberately does not manufacture a context of its own: a policy that
|
|
44
|
+
* refuses a context-free reveal must keep refusing here too.
|
|
45
|
+
*/
|
|
46
|
+
context?: RevealContext;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* A Zod schema that produces a `SensitiveString` of a known class, carrying the
|
|
50
|
+
* refinement helper that reads the raw value under audit.
|
|
51
|
+
*
|
|
52
|
+
* The declared shape is `z.ZodType` rather than the concrete `z.ZodPipe` the
|
|
53
|
+
* factories build, because the intersection with `SensitiveSchemaMethods` needs
|
|
54
|
+
* a name that does not move when a factory changes how it composes. Naming the
|
|
55
|
+
* pipe would put each factory's internal shape in the public type.
|
|
56
|
+
*
|
|
57
|
+
* `refineSensitive` is attached to the schemas this package builds. Standard Zod
|
|
58
|
+
* combinators (`.optional()`, `.array()`, `.default()`) return plain Zod schemas
|
|
59
|
+
* and drop it; apply refinements before those, or call the standalone
|
|
60
|
+
* {@link refineSensitive} function.
|
|
61
|
+
*/
|
|
62
|
+
export type SensitiveSchema<C extends SensitivityClass, Input = string> = z.ZodType<SensitiveString<C>, Input> & SensitiveSchemaMethods<C, Input>;
|
|
63
|
+
/**
|
|
64
|
+
* Methods added to every schema this package builds.
|
|
65
|
+
*/
|
|
66
|
+
export interface SensitiveSchemaMethods<C extends SensitivityClass, Input> {
|
|
67
|
+
/**
|
|
68
|
+
* Validate the wrapped value against a predicate that receives the raw string.
|
|
69
|
+
*
|
|
70
|
+
* This is a reveal: the ambient `RevealContext` authorizes it and its
|
|
71
|
+
* `auditReveal` hook records it.
|
|
72
|
+
*/
|
|
73
|
+
refineSensitive(check: SensitiveCheck, params?: RefineSensitiveParams): SensitiveSchema<C, Input>;
|
|
74
|
+
}
|
|
75
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAC7B,OAAO,KAAK,EACV,aAAa,EACb,eAAe,EACf,gBAAgB,EACjB,MAAM,qCAAqC,CAAC;AAE7C;;;;;;;GAOG;AACH,MAAM,MAAM,cAAc,GAAG,CAAC,QAAQ,EAAE,MAAM,KAAK,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;AAE9E;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC;;;;;OAKG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,sFAAsF;IACtF,IAAI,CAAC,EAAE,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,CAAC;IAC3B;;;;;;OAMG;IACH,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB;;;;;;;OAOG;IACH,OAAO,CAAC,EAAE,aAAa,CAAC;CACzB;AAED;;;;;;;;;;;;;GAaG;AACH,MAAM,MAAM,eAAe,CAAC,CAAC,SAAS,gBAAgB,EAAE,KAAK,GAAG,MAAM,IAAI,CAAC,CAAC,OAAO,CACjF,eAAe,CAAC,CAAC,CAAC,EAClB,KAAK,CACN,GACC,sBAAsB,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC;AAEnC;;GAEG;AACH,MAAM,WAAW,sBAAsB,CAAC,CAAC,SAAS,gBAAgB,EAAE,KAAK;IACvE;;;;;OAKG;IACH,eAAe,CAAC,KAAK,EAAE,cAAc,EAAE,MAAM,CAAC,EAAE,qBAAqB,GAAG,eAAe,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC;CACnG"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";AAAA;;;;GAIG"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@platform-sec/sensitive-string-zod",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "Zod integration for SensitiveString - parse input directly into sensitive types",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -14,11 +14,6 @@
|
|
|
14
14
|
"files": [
|
|
15
15
|
"dist"
|
|
16
16
|
],
|
|
17
|
-
"scripts": {
|
|
18
|
-
"build": "tsc",
|
|
19
|
-
"test": "vitest run --root ../.. packages/sensitive-zod/",
|
|
20
|
-
"test:watch": "vitest --root ../.. packages/sensitive-zod/"
|
|
21
|
-
},
|
|
22
17
|
"keywords": [
|
|
23
18
|
"zod",
|
|
24
19
|
"validation",
|
|
@@ -40,10 +35,15 @@
|
|
|
40
35
|
"zod": "^4.0.0"
|
|
41
36
|
},
|
|
42
37
|
"devDependencies": {
|
|
43
|
-
"@
|
|
44
|
-
"@types/node": "^22.20.1",
|
|
38
|
+
"@types/node": "^22.20.5",
|
|
45
39
|
"typescript": "^5.9.3",
|
|
46
|
-
"vitest": "^4.1.
|
|
47
|
-
"zod": "^4.
|
|
40
|
+
"vitest": "^4.1.12",
|
|
41
|
+
"zod": "^4.6.7",
|
|
42
|
+
"@platform-sec/sensitive-string-core": "0.6.1"
|
|
43
|
+
},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"build": "tsc",
|
|
46
|
+
"test": "vitest run --root ../.. packages/sensitive-zod/",
|
|
47
|
+
"test:watch": "vitest --root ../.. packages/sensitive-zod/"
|
|
48
48
|
}
|
|
49
|
-
}
|
|
49
|
+
}
|