@oxog/vld 2.1.0 → 2.2.7
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/CHANGELOG.md +113 -0
- package/README.md +413 -1235
- package/dist/chunks/errors-core-CerMg2rq.js +1 -0
- package/dist/chunks/index-CkbfQQ4n.js +1 -0
- package/dist/chunks/index-yjpbvFgw.js +1 -0
- package/dist/chunks/ip-validation-g5LWgy9t.js +1 -0
- package/dist/chunks/security-Dy24T8N7.js +1 -0
- package/dist/cjs/chunks/errors-core-DrydF46s.cjs +1 -0
- package/dist/cjs/chunks/index-BEV9nG0N.cjs +1 -0
- package/dist/cjs/chunks/index-ZjHAxYdA.cjs +1 -0
- package/dist/cjs/chunks/ip-validation-BZqPamPC.cjs +1 -0
- package/dist/cjs/chunks/security-aXZVI7qW.cjs +1 -0
- package/dist/cjs/cli/bin.cjs +1 -505
- package/dist/cjs/cli/index.cjs +1 -209
- package/dist/cjs/codecs/index.cjs +1 -45
- package/dist/cjs/coercion/index.cjs +1 -588
- package/dist/cjs/compat/emitter.cjs +1 -184
- package/dist/cjs/compat/result.cjs +1 -189
- package/dist/cjs/errors.cjs +1 -180
- package/dist/cjs/index.cjs +1 -1894
- package/dist/cjs/kernel.cjs +1 -415
- package/dist/cjs/locales/af.cjs +1 -108
- package/dist/cjs/locales/ar.cjs +1 -108
- package/dist/cjs/locales/bn.cjs +1 -108
- package/dist/cjs/locales/da.cjs +1 -108
- package/dist/cjs/locales/de.cjs +1 -108
- package/dist/cjs/locales/en.cjs +1 -108
- package/dist/cjs/locales/es-MX.cjs +1 -107
- package/dist/cjs/locales/es.cjs +1 -96
- package/dist/cjs/locales/fi.cjs +1 -108
- package/dist/cjs/locales/fr.cjs +1 -96
- package/dist/cjs/locales/hi.cjs +1 -108
- package/dist/cjs/locales/id.cjs +1 -108
- package/dist/cjs/locales/index.cjs +1 -43
- package/dist/cjs/locales/it.cjs +1 -108
- package/dist/cjs/locales/ja.cjs +1 -108
- package/dist/cjs/locales/ko.cjs +1 -108
- package/dist/cjs/locales/lazy.cjs +1 -188
- package/dist/cjs/locales/nl.cjs +1 -108
- package/dist/cjs/locales/no.cjs +1 -108
- package/dist/cjs/locales/pl.cjs +1 -108
- package/dist/cjs/locales/pt-BR.cjs +1 -108
- package/dist/cjs/locales/pt.cjs +1 -108
- package/dist/cjs/locales/ru.cjs +1 -108
- package/dist/cjs/locales/runtime.cjs +1 -51
- package/dist/cjs/locales/sv.cjs +1 -108
- package/dist/cjs/locales/sw.cjs +1 -108
- package/dist/cjs/locales/th.cjs +1 -108
- package/dist/cjs/locales/tr.cjs +1 -108
- package/dist/cjs/locales/vi.cjs +1 -108
- package/dist/cjs/locales/zh.cjs +1 -108
- package/dist/cjs/mini.cjs +1 -242
- package/dist/cjs/pigment.cjs +1 -207
- package/dist/cjs/plugins/index.cjs +1 -12
- package/dist/cjs/registry.cjs +1 -37
- package/dist/cjs/v3/index.cjs +1 -425
- package/dist/cjs/v4/core/index.cjs +1 -894
- package/dist/cjs/v4/index.cjs +1 -425
- package/dist/cjs/v4/locales/index.cjs +1 -130
- package/dist/cjs/v4/mini/index.cjs +1 -509
- package/dist/cjs/v4-mini/index.cjs +1 -532
- package/dist/cjs/validators/any.cjs +1 -40
- package/dist/cjs/validators/array.cjs +1 -283
- package/dist/cjs/validators/base.cjs +1 -887
- package/dist/cjs/validators/base64.cjs +1 -73
- package/dist/cjs/validators/bigint.cjs +1 -232
- package/dist/cjs/validators/boolean.cjs +1 -146
- package/dist/cjs/validators/codec.cjs +1 -205
- package/dist/cjs/validators/custom.cjs +1 -71
- package/dist/cjs/validators/date.cjs +1 -278
- package/dist/cjs/validators/discriminated-union.cjs +1 -130
- package/dist/cjs/validators/enum.cjs +1 -154
- package/dist/cjs/validators/file.cjs +1 -130
- package/dist/cjs/validators/function.cjs +1 -63
- package/dist/cjs/validators/hex.cjs +1 -70
- package/dist/cjs/validators/index.cjs +1 -123
- package/dist/cjs/validators/intersection.cjs +1 -120
- package/dist/cjs/validators/json.cjs +1 -70
- package/dist/cjs/validators/lazy.cjs +1 -53
- package/dist/cjs/validators/literal.cjs +1 -64
- package/dist/cjs/validators/map.cjs +1 -218
- package/dist/cjs/validators/nan.cjs +1 -41
- package/dist/cjs/validators/never.cjs +1 -36
- package/dist/cjs/validators/null.cjs +1 -43
- package/dist/cjs/validators/number.cjs +1 -427
- package/dist/cjs/validators/object.cjs +1 -868
- package/dist/cjs/validators/promise.cjs +1 -115
- package/dist/cjs/validators/record.cjs +1 -262
- package/dist/cjs/validators/set.cjs +1 -203
- package/dist/cjs/validators/string-bool.cjs +1 -124
- package/dist/cjs/validators/string-formats.cjs +1 -168
- package/dist/cjs/validators/string.cjs +1 -12
- package/dist/cjs/validators/symbol.cjs +1 -62
- package/dist/cjs/validators/template-literal.cjs +1 -108
- package/dist/cjs/validators/tuple.cjs +1 -187
- package/dist/cjs/validators/uint8array.cjs +1 -106
- package/dist/cjs/validators/undefined.cjs +1 -43
- package/dist/cjs/validators/union.cjs +1 -191
- package/dist/cjs/validators/unknown.cjs +1 -40
- package/dist/cjs/validators/void.cjs +1 -48
- package/dist/cjs/validators/xor.cjs +1 -69
- package/dist/cli/bin.js +1 -406
- package/dist/cli/index.js +1 -205
- package/dist/codecs/index.js +1 -11
- package/dist/coercion/index.js +1 -579
- package/dist/compat/emitter.js +1 -180
- package/dist/compat/result.js +1 -170
- package/dist/errors-core.d.ts +32 -2
- package/dist/errors.js +1 -171
- package/dist/index.d.ts +315 -361
- package/dist/index.js +1 -1534
- package/dist/kernel.js +1 -409
- package/dist/locales/af.js +1 -106
- package/dist/locales/ar.js +1 -106
- package/dist/locales/bn.js +1 -106
- package/dist/locales/da.js +1 -106
- package/dist/locales/de.js +1 -106
- package/dist/locales/en.js +1 -106
- package/dist/locales/es-MX.js +1 -105
- package/dist/locales/es.js +1 -94
- package/dist/locales/fi.js +1 -106
- package/dist/locales/fr.js +1 -94
- package/dist/locales/hi.js +1 -106
- package/dist/locales/id.js +1 -106
- package/dist/locales/index.js +1 -29
- package/dist/locales/it.js +1 -106
- package/dist/locales/ja.js +1 -106
- package/dist/locales/ko.js +1 -106
- package/dist/locales/lazy.js +1 -177
- package/dist/locales/nl.js +1 -106
- package/dist/locales/no.js +1 -106
- package/dist/locales/pl.js +1 -106
- package/dist/locales/pt-BR.js +1 -106
- package/dist/locales/pt.js +1 -106
- package/dist/locales/ru.js +1 -106
- package/dist/locales/runtime.js +1 -41
- package/dist/locales/sv.js +1 -106
- package/dist/locales/sw.js +1 -106
- package/dist/locales/th.js +1 -106
- package/dist/locales/tr.js +1 -106
- package/dist/locales/vi.js +1 -106
- package/dist/locales/zh.js +1 -106
- package/dist/mini.d.ts +3 -3
- package/dist/mini.js +1 -156
- package/dist/pigment.js +1 -180
- package/dist/plugins/index.js +1 -1
- package/dist/registry.js +1 -34
- package/dist/utils/json-schema.d.ts +10 -1
- package/dist/v3/index.js +1 -46
- package/dist/v4/core/index.d.ts +28 -16
- package/dist/v4/core/index.js +1 -277
- package/dist/v4/index.js +1 -50
- package/dist/v4/locales/index.d.ts +4 -0
- package/dist/v4/locales/index.js +1 -77
- package/dist/v4/mini/index.js +1 -47
- package/dist/v4-mini/index.d.ts +1 -1
- package/dist/v4-mini/index.js +1 -75
- package/dist/validators/any.js +1 -37
- package/dist/validators/array.d.ts +7 -0
- package/dist/validators/array.js +1 -279
- package/dist/validators/base.d.ts +210 -17
- package/dist/validators/base.js +1 -868
- package/dist/validators/base64.js +1 -68
- package/dist/validators/bigint.d.ts +7 -0
- package/dist/validators/bigint.js +1 -228
- package/dist/validators/boolean.js +1 -142
- package/dist/validators/codec.js +1 -200
- package/dist/validators/custom.d.ts +20 -10
- package/dist/validators/custom.js +1 -67
- package/dist/validators/date.d.ts +2 -0
- package/dist/validators/date.js +1 -274
- package/dist/validators/discriminated-union.d.ts +4 -2
- package/dist/validators/discriminated-union.js +1 -123
- package/dist/validators/enum.d.ts +8 -2
- package/dist/validators/enum.js +1 -150
- package/dist/validators/file.js +1 -125
- package/dist/validators/function.js +1 -58
- package/dist/validators/hex.js +1 -65
- package/dist/validators/index.d.ts +1 -1
- package/dist/validators/index.js +1 -35
- package/dist/validators/intersection.js +1 -116
- package/dist/validators/json.js +1 -66
- package/dist/validators/lazy.js +1 -50
- package/dist/validators/literal.d.ts +8 -3
- package/dist/validators/literal.js +1 -60
- package/dist/validators/map.d.ts +7 -0
- package/dist/validators/map.js +1 -214
- package/dist/validators/nan.js +1 -38
- package/dist/validators/never.js +1 -32
- package/dist/validators/null.js +1 -40
- package/dist/validators/number.d.ts +25 -1
- package/dist/validators/number.js +1 -423
- package/dist/validators/object.d.ts +18 -2
- package/dist/validators/object.js +1 -864
- package/dist/validators/promise.d.ts +6 -7
- package/dist/validators/promise.js +1 -111
- package/dist/validators/record.d.ts +13 -8
- package/dist/validators/record.js +1 -258
- package/dist/validators/set.d.ts +5 -0
- package/dist/validators/set.js +1 -199
- package/dist/validators/string-bool.js +1 -120
- package/dist/validators/string-formats.d.ts +58 -62
- package/dist/validators/string-formats.js +1 -135
- package/dist/validators/string.d.ts +49 -10
- package/dist/validators/string.js +1 -4
- package/dist/validators/symbol.js +1 -58
- package/dist/validators/template-literal.js +1 -103
- package/dist/validators/tuple.d.ts +17 -13
- package/dist/validators/tuple.js +1 -183
- package/dist/validators/uint8array.js +1 -101
- package/dist/validators/undefined.js +1 -40
- package/dist/validators/union.d.ts +1 -0
- package/dist/validators/union.js +1 -187
- package/dist/validators/unknown.js +1 -37
- package/dist/validators/void.js +1 -44
- package/dist/validators/xor.d.ts +2 -2
- package/dist/validators/xor.js +1 -66
- package/package.json +240 -238
- package/dist/chunks/errors-core-BeiFUtZM.js +0 -81
- package/dist/chunks/index-0R2ntSud.js +0 -508
- package/dist/chunks/index-BvH403Yy.js +0 -88
- package/dist/chunks/security-B0Pd_vSY.js +0 -35
- package/dist/chunks/string-BJRQod3t.js +0 -530
- package/dist/cjs/chunks/errors-core-DWxLFs2f.cjs +0 -83
- package/dist/cjs/chunks/index-BkI37108.cjs +0 -538
- package/dist/cjs/chunks/index-Cc3bjUMX.cjs +0 -91
- package/dist/cjs/chunks/security-Dc3Jh13T.cjs +0 -37
- package/dist/cjs/chunks/string-BKZrc4hs.cjs +0 -533
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Promise validator - validates Promise values
|
|
3
3
|
* Validates that the resolved value matches the inner schema
|
|
4
4
|
*/
|
|
5
|
-
import { type ParseResult, type StandardSchemaV1Props } from './base.js';
|
|
5
|
+
import { VldBase, type ParseResult, type StandardSchemaV1Props } from './base.js';
|
|
6
6
|
type PromiseInner<T> = {
|
|
7
7
|
parse(value: unknown): T;
|
|
8
8
|
safeParse(value: unknown): ParseResult<T>;
|
|
@@ -18,11 +18,10 @@ type PromiseInner<T> = {
|
|
|
18
18
|
*
|
|
19
19
|
* Use parseAsync() or safeParseAsync() for proper async handling.
|
|
20
20
|
*/
|
|
21
|
-
export declare class VldPromise<T> {
|
|
21
|
+
export declare class VldPromise<T> extends VldBase<unknown, Promise<T>> {
|
|
22
22
|
private readonly inner;
|
|
23
|
-
readonly validatorType: "promise";
|
|
24
23
|
constructor(inner: PromiseInner<T>);
|
|
25
|
-
get '~standard'(): StandardSchemaV1Props<unknown,
|
|
24
|
+
get '~standard'(): StandardSchemaV1Props<unknown, any>;
|
|
26
25
|
/**
|
|
27
26
|
* Check if value is thenable (Promise-like)
|
|
28
27
|
* Must be called BEFORE wrapping in Promise.resolve
|
|
@@ -36,14 +35,14 @@ export declare class VldPromise<T> {
|
|
|
36
35
|
* @throws {Error} If validation fails when Promise resolves
|
|
37
36
|
*/
|
|
38
37
|
parse(value: unknown): Promise<T>;
|
|
39
|
-
parseAsync(value: unknown): Promise<T
|
|
38
|
+
parseAsync(value: unknown): Promise<Promise<T>>;
|
|
40
39
|
/**
|
|
41
40
|
* Safely parse and validate a Promise value asynchronously
|
|
42
41
|
* @param value The Promise to validate
|
|
43
42
|
* @returns A Promise resolving to ParseResult containing the validated value
|
|
44
43
|
*/
|
|
45
|
-
safeParse(value: unknown):
|
|
46
|
-
safeParseAsync(value: unknown): Promise<ParseResult<T
|
|
44
|
+
safeParse(value: unknown): any;
|
|
45
|
+
safeParseAsync(value: unknown): Promise<ParseResult<Promise<T>>>;
|
|
47
46
|
}
|
|
48
47
|
/**
|
|
49
48
|
* Create a Promise validator
|
|
@@ -1,111 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Promise validator - validates Promise values
|
|
5
|
-
* Validates that the resolved value matches the inner schema
|
|
6
|
-
*/
|
|
7
|
-
/**
|
|
8
|
-
* Promise validator - validates Promise<T> where T matches inner schema
|
|
9
|
-
* Zod 4 API parity - allows async validation with proper type inference
|
|
10
|
-
*
|
|
11
|
-
* Validates that the input is a Promise, and validates the resolved value
|
|
12
|
-
* against the inner schema when parsed.
|
|
13
|
-
*
|
|
14
|
-
* Use parseAsync() or safeParseAsync() for proper async handling.
|
|
15
|
-
*/
|
|
16
|
-
class VldPromise {
|
|
17
|
-
constructor(inner) {
|
|
18
|
-
this.inner = inner;
|
|
19
|
-
this.validatorType = VLD_VALIDATOR_TYPES.PROMISE;
|
|
20
|
-
}
|
|
21
|
-
get '~standard'() {
|
|
22
|
-
return {
|
|
23
|
-
version: 1,
|
|
24
|
-
vendor: 'vld',
|
|
25
|
-
validate: async (value) => {
|
|
26
|
-
const result = await this.safeParseAsync(value);
|
|
27
|
-
if (result.success) {
|
|
28
|
-
return { value: result.data };
|
|
29
|
-
}
|
|
30
|
-
return { issues: [{ message: result.error.message }] };
|
|
31
|
-
},
|
|
32
|
-
types: undefined
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* Check if value is thenable (Promise-like)
|
|
37
|
-
* Must be called BEFORE wrapping in Promise.resolve
|
|
38
|
-
*/
|
|
39
|
-
_isThenable(value) {
|
|
40
|
-
return value !== null && typeof value.then === 'function';
|
|
41
|
-
}
|
|
42
|
-
unwrap() {
|
|
43
|
-
return this.inner;
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Parse and validate a Promise value asynchronously
|
|
47
|
-
* @param value The Promise to validate
|
|
48
|
-
* @returns The validated value
|
|
49
|
-
* @throws {Error} If validation fails when Promise resolves
|
|
50
|
-
*/
|
|
51
|
-
async parse(value) {
|
|
52
|
-
// Check if value is thenable BEFORE wrapping in Promise.resolve
|
|
53
|
-
if (!this._isThenable(value)) {
|
|
54
|
-
throw new Error('Expected a Promise value');
|
|
55
|
-
}
|
|
56
|
-
// Now we can safely await it
|
|
57
|
-
const resolved = await Promise.resolve(value);
|
|
58
|
-
return this.inner.parseAsync ? this.inner.parseAsync(resolved) : this.inner.parse(resolved);
|
|
59
|
-
}
|
|
60
|
-
parseAsync(value) {
|
|
61
|
-
return this.parse(value);
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Safely parse and validate a Promise value asynchronously
|
|
65
|
-
* @param value The Promise to validate
|
|
66
|
-
* @returns A Promise resolving to ParseResult containing the validated value
|
|
67
|
-
*/
|
|
68
|
-
async safeParse(value) {
|
|
69
|
-
// Check if value is thenable BEFORE wrapping in Promise.resolve
|
|
70
|
-
if (!this._isThenable(value)) {
|
|
71
|
-
return {
|
|
72
|
-
success: false,
|
|
73
|
-
error: new Error('Expected a Promise value')
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
try {
|
|
77
|
-
const resolved = await Promise.resolve(value);
|
|
78
|
-
if (this.inner.safeParseAsync) {
|
|
79
|
-
return await this.inner.safeParseAsync(resolved);
|
|
80
|
-
}
|
|
81
|
-
const innerResult = this.inner.safeParse(resolved);
|
|
82
|
-
if (!innerResult.success) {
|
|
83
|
-
return innerResult;
|
|
84
|
-
}
|
|
85
|
-
return { success: true, data: innerResult.data };
|
|
86
|
-
}
|
|
87
|
-
catch (err) {
|
|
88
|
-
return {
|
|
89
|
-
success: false,
|
|
90
|
-
error: err instanceof Error ? err : new Error(String(err))
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
safeParseAsync(value) {
|
|
95
|
-
return this.safeParse(value);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
/**
|
|
99
|
-
* Create a Promise validator
|
|
100
|
-
* @param inner The schema to validate the resolved value against
|
|
101
|
-
* @returns A new Promise validator
|
|
102
|
-
* @example
|
|
103
|
-
* const promiseSchema = v.promise(v.string());
|
|
104
|
-
* const result = await promiseSchema.parse(Promise.resolve("hello"));
|
|
105
|
-
* // result is string
|
|
106
|
-
*/
|
|
107
|
-
function promise(inner) {
|
|
108
|
-
return new VldPromise(inner);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
export { VldPromise, promise };
|
|
1
|
+
import{VldBase as e,VLD_VALIDATOR_TYPES as s,ensureVldError as r}from"./base.js";class VldPromise extends e{constructor(e){super(s.PROMISE),this.inner=e}get"~standard"(){return{version:1,vendor:"vld",validate:async e=>{const s=await this.safeParse(e);return s.success?{value:s.data}:{issues:[{message:s.error.message}]}},types:void 0}}_isThenable(e){return null!==e&&"function"==typeof e.then}unwrap(){return this.inner}async parse(e){if(!this._isThenable(e))throw new Error("Expected a Promise value");const s=await Promise.resolve(e);return this.inner.parseAsync?this.inner.parseAsync(s):this.inner.parse(s)}parseAsync(e){return this.parse(e)}safeParse(e){return this._isThenable(e)?(async()=>{try{const s=await Promise.resolve(e);if(this.inner.safeParseAsync)return await this.inner.safeParseAsync(s);const r=this.inner.safeParse(s);return r.success?{success:!0,data:r.data}:r}catch(e){return{success:!1,error:r(e)}}})():Promise.resolve({success:!1,error:r("Expected a Promise value")})}safeParseAsync(e){return this.safeParse(e)}}function n(e){return new VldPromise(e)}export{VldPromise,n as promise};
|
|
@@ -3,45 +3,50 @@ import { VldBase, ParseResult } from './base.js';
|
|
|
3
3
|
* Immutable record validator for key-value pairs
|
|
4
4
|
* BUG-NEW-018 FIX: Uses comprehensive dangerous key protection
|
|
5
5
|
*/
|
|
6
|
-
export declare class VldRecord<T> extends VldBase<unknown, Record<
|
|
6
|
+
export declare class VldRecord<T, K extends PropertyKey = string> extends VldBase<unknown, Record<K, T>> {
|
|
7
7
|
protected readonly valueValidator: VldBase<unknown, T>;
|
|
8
|
+
private readonly keyValidator?;
|
|
8
9
|
private readonly errorMessage?;
|
|
9
10
|
/**
|
|
10
11
|
* Private constructor to enforce immutability
|
|
11
12
|
*/
|
|
12
|
-
protected constructor(valueValidator: VldBase<unknown, T>, errorMessage?: string | undefined);
|
|
13
|
+
protected constructor(valueValidator: VldBase<unknown, T>, keyValidator?: VldBase<unknown, K> | undefined, errorMessage?: string | undefined);
|
|
13
14
|
private readonly _simpleValueMode;
|
|
14
15
|
private readonly _simpleValue;
|
|
15
16
|
get valueSchema(): VldBase<unknown, T>;
|
|
17
|
+
get keySchema(): VldBase<unknown, K> | undefined;
|
|
18
|
+
get valueType(): VldBase<unknown, T>;
|
|
19
|
+
get keyType(): VldBase<unknown, K> | undefined;
|
|
16
20
|
/**
|
|
17
21
|
* Create a new record validator
|
|
18
22
|
*/
|
|
19
|
-
static create<T>(valueValidator: VldBase<unknown, T>): VldRecord<T>;
|
|
23
|
+
static create<T, K extends PropertyKey = string>(valueValidator: VldBase<unknown, T>, keyValidator?: VldBase<unknown, K>): VldRecord<T, K>;
|
|
20
24
|
private getSimpleValueMode;
|
|
21
25
|
private getSimpleValueError;
|
|
22
26
|
/**
|
|
23
27
|
* Parse and validate a record value
|
|
24
28
|
* BUG-NEW-018 FIX: Use comprehensive dangerous key protection
|
|
25
29
|
*/
|
|
26
|
-
parse(value: unknown): Record<
|
|
30
|
+
parse(value: unknown): Record<K, T>;
|
|
27
31
|
/**
|
|
28
32
|
* Parse a value that has already passed the record object type guard.
|
|
29
33
|
* @internal Used by object validators to avoid duplicate hot-path checks.
|
|
30
34
|
*/
|
|
31
|
-
parseKnownRecord(obj: Record<string, unknown>): Record<
|
|
35
|
+
parseKnownRecord(obj: Record<string, unknown>): Record<K, T>;
|
|
36
|
+
private parseKeyedRecord;
|
|
32
37
|
/**
|
|
33
38
|
* Safely parse and validate a record value
|
|
34
39
|
*/
|
|
35
|
-
safeParse(value: unknown): ParseResult<Record<
|
|
40
|
+
safeParse(value: unknown): ParseResult<Record<K, T>>;
|
|
36
41
|
/**
|
|
37
42
|
* Create a partial record variant (all keys optional)
|
|
38
43
|
* Similar to v.object().partial() but for records
|
|
39
44
|
*/
|
|
40
|
-
partial(): VldRecord<T | undefined>;
|
|
45
|
+
partial(): VldRecord<T | undefined, K>;
|
|
41
46
|
/**
|
|
42
47
|
* Create a loose record variant (allows extra keys)
|
|
43
48
|
* Similar to v.object().passthrough() but for records
|
|
44
49
|
* Note: Records already allow any keys, so this mainly affects error handling
|
|
45
50
|
*/
|
|
46
|
-
loose(): VldBase<unknown, Record<
|
|
51
|
+
loose(): VldBase<unknown, Record<K, T>>;
|
|
47
52
|
}
|
|
@@ -1,258 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { getMessages } from '../locales/runtime.js';
|
|
3
|
-
import { i as isDangerousKey } from '../chunks/security-B0Pd_vSY.js';
|
|
4
|
-
import { V as VldError } from '../chunks/errors-core-BeiFUtZM.js';
|
|
5
|
-
|
|
6
|
-
function createRecordError(message) {
|
|
7
|
-
return new VldError([{ code: 'invalid_object', path: [], message }]);
|
|
8
|
-
}
|
|
9
|
-
/**
|
|
10
|
-
* Immutable record validator for key-value pairs
|
|
11
|
-
* BUG-NEW-018 FIX: Uses comprehensive dangerous key protection
|
|
12
|
-
*/
|
|
13
|
-
class VldRecord extends VldBase {
|
|
14
|
-
/**
|
|
15
|
-
* Private constructor to enforce immutability
|
|
16
|
-
*/
|
|
17
|
-
constructor(valueValidator, errorMessage) {
|
|
18
|
-
super(VLD_VALIDATOR_TYPES.RECORD);
|
|
19
|
-
this.valueValidator = valueValidator;
|
|
20
|
-
this.errorMessage = errorMessage;
|
|
21
|
-
this._simpleValueMode = this.getSimpleValueMode(valueValidator);
|
|
22
|
-
this._simpleValue = this._simpleValueMode === 'literal' ? valueValidator.literal : undefined;
|
|
23
|
-
}
|
|
24
|
-
get valueSchema() {
|
|
25
|
-
return this.valueValidator;
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Create a new record validator
|
|
29
|
-
*/
|
|
30
|
-
static create(valueValidator) {
|
|
31
|
-
return new VldRecord(valueValidator);
|
|
32
|
-
}
|
|
33
|
-
getSimpleValueMode(valueValidator) {
|
|
34
|
-
if (valueValidator.isSimple !== true) {
|
|
35
|
-
return undefined;
|
|
36
|
-
}
|
|
37
|
-
switch (valueValidator.validatorType) {
|
|
38
|
-
case VLD_VALIDATOR_TYPES.STRING:
|
|
39
|
-
return 'string';
|
|
40
|
-
case VLD_VALIDATOR_TYPES.NUMBER:
|
|
41
|
-
return 'number';
|
|
42
|
-
case VLD_VALIDATOR_TYPES.BOOLEAN:
|
|
43
|
-
return 'boolean';
|
|
44
|
-
case VLD_VALIDATOR_TYPES.BIGINT:
|
|
45
|
-
return 'bigint';
|
|
46
|
-
case VLD_VALIDATOR_TYPES.SYMBOL:
|
|
47
|
-
return 'symbol';
|
|
48
|
-
case VLD_VALIDATOR_TYPES.NULL:
|
|
49
|
-
return 'null';
|
|
50
|
-
case VLD_VALIDATOR_TYPES.UNDEFINED:
|
|
51
|
-
case VLD_VALIDATOR_TYPES.VOID:
|
|
52
|
-
return 'undefinedValue';
|
|
53
|
-
case VLD_VALIDATOR_TYPES.LITERAL:
|
|
54
|
-
return 'literal';
|
|
55
|
-
case VLD_VALIDATOR_TYPES.ANY:
|
|
56
|
-
case VLD_VALIDATOR_TYPES.UNKNOWN:
|
|
57
|
-
return 'passthrough';
|
|
58
|
-
default:
|
|
59
|
-
return undefined;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
getSimpleValueError(received) {
|
|
63
|
-
switch (this._simpleValueMode) {
|
|
64
|
-
case 'string':
|
|
65
|
-
return getMessages().invalidString;
|
|
66
|
-
case 'number':
|
|
67
|
-
return getMessages().invalidNumber;
|
|
68
|
-
case 'boolean':
|
|
69
|
-
return getMessages().invalidBoolean;
|
|
70
|
-
case 'bigint':
|
|
71
|
-
return getMessages().invalidBigint;
|
|
72
|
-
case 'symbol':
|
|
73
|
-
return getMessages().invalidSymbol;
|
|
74
|
-
case 'null':
|
|
75
|
-
return `Expected null, received ${typeof received}`;
|
|
76
|
-
case 'undefinedValue':
|
|
77
|
-
return getMessages().expectedUndefined;
|
|
78
|
-
case 'literal':
|
|
79
|
-
return getMessages().literalExpected(JSON.stringify(this._simpleValue), JSON.stringify(received));
|
|
80
|
-
default:
|
|
81
|
-
return getMessages().invalidRecord;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Parse and validate a record value
|
|
86
|
-
* BUG-NEW-018 FIX: Use comprehensive dangerous key protection
|
|
87
|
-
*/
|
|
88
|
-
parse(value) {
|
|
89
|
-
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
90
|
-
throw new Error(this.errorMessage || getMessages().invalidRecord);
|
|
91
|
-
}
|
|
92
|
-
return this.parseKnownRecord(value);
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* Parse a value that has already passed the record object type guard.
|
|
96
|
-
* @internal Used by object validators to avoid duplicate hot-path checks.
|
|
97
|
-
*/
|
|
98
|
-
parseKnownRecord(obj) {
|
|
99
|
-
const result = {};
|
|
100
|
-
const keys = Object.keys(obj);
|
|
101
|
-
const simpleMode = this._simpleValueMode;
|
|
102
|
-
for (let i = 0; i < keys.length; i++) {
|
|
103
|
-
const key = keys[i];
|
|
104
|
-
// Skip dangerous keys to prevent prototype pollution
|
|
105
|
-
// Now using comprehensive protection from shared utility
|
|
106
|
-
if (isDangerousKey(key)) {
|
|
107
|
-
continue;
|
|
108
|
-
}
|
|
109
|
-
const val = obj[key];
|
|
110
|
-
if (simpleMode !== undefined) {
|
|
111
|
-
switch (simpleMode) {
|
|
112
|
-
case 'string':
|
|
113
|
-
if (typeof val !== 'string') {
|
|
114
|
-
throw new Error(getMessages().objectField(key, getMessages().invalidString));
|
|
115
|
-
}
|
|
116
|
-
result[key] = val;
|
|
117
|
-
continue;
|
|
118
|
-
case 'number':
|
|
119
|
-
if (typeof val !== 'number' || isNaN(val)) {
|
|
120
|
-
throw new Error(getMessages().objectField(key, getMessages().invalidNumber));
|
|
121
|
-
}
|
|
122
|
-
result[key] = val;
|
|
123
|
-
continue;
|
|
124
|
-
case 'boolean':
|
|
125
|
-
if (typeof val !== 'boolean') {
|
|
126
|
-
throw new Error(getMessages().objectField(key, getMessages().invalidBoolean));
|
|
127
|
-
}
|
|
128
|
-
result[key] = val;
|
|
129
|
-
continue;
|
|
130
|
-
case 'bigint':
|
|
131
|
-
if (typeof val !== 'bigint') {
|
|
132
|
-
throw new Error(getMessages().objectField(key, getMessages().invalidBigint));
|
|
133
|
-
}
|
|
134
|
-
result[key] = val;
|
|
135
|
-
continue;
|
|
136
|
-
case 'symbol':
|
|
137
|
-
if (typeof val !== 'symbol') {
|
|
138
|
-
throw new Error(getMessages().objectField(key, getMessages().invalidSymbol));
|
|
139
|
-
}
|
|
140
|
-
result[key] = val;
|
|
141
|
-
continue;
|
|
142
|
-
case 'null':
|
|
143
|
-
if (val !== null) {
|
|
144
|
-
throw new Error(getMessages().objectField(key, this.getSimpleValueError(val)));
|
|
145
|
-
}
|
|
146
|
-
result[key] = null;
|
|
147
|
-
continue;
|
|
148
|
-
case 'undefinedValue':
|
|
149
|
-
if (val !== undefined) {
|
|
150
|
-
throw new Error(getMessages().objectField(key, getMessages().expectedUndefined));
|
|
151
|
-
}
|
|
152
|
-
result[key] = undefined;
|
|
153
|
-
continue;
|
|
154
|
-
case 'literal':
|
|
155
|
-
if (val !== this._simpleValue) {
|
|
156
|
-
throw new Error(getMessages().objectField(key, this.getSimpleValueError(val)));
|
|
157
|
-
}
|
|
158
|
-
result[key] = this._simpleValue;
|
|
159
|
-
continue;
|
|
160
|
-
case 'passthrough':
|
|
161
|
-
result[key] = val;
|
|
162
|
-
continue;
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
try {
|
|
166
|
-
result[key] = this.valueValidator.parse(val);
|
|
167
|
-
}
|
|
168
|
-
catch (error) {
|
|
169
|
-
throw new Error(getMessages().objectField(key, error.message));
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
return result;
|
|
173
|
-
}
|
|
174
|
-
/**
|
|
175
|
-
* Safely parse and validate a record value
|
|
176
|
-
*/
|
|
177
|
-
safeParse(value) {
|
|
178
|
-
try {
|
|
179
|
-
return { success: true, data: this.parse(value) };
|
|
180
|
-
}
|
|
181
|
-
catch (error) {
|
|
182
|
-
return { success: false, error: createRecordError(error.message) };
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
/**
|
|
186
|
-
* Create a partial record variant (all keys optional)
|
|
187
|
-
* Similar to v.object().partial() but for records
|
|
188
|
-
*/
|
|
189
|
-
partial() {
|
|
190
|
-
const optionalValidator = this.valueValidator.optional();
|
|
191
|
-
return new VldRecord(optionalValidator);
|
|
192
|
-
}
|
|
193
|
-
/**
|
|
194
|
-
* Create a loose record variant (allows extra keys)
|
|
195
|
-
* Similar to v.object().passthrough() but for records
|
|
196
|
-
* Note: Records already allow any keys, so this mainly affects error handling
|
|
197
|
-
*/
|
|
198
|
-
loose() {
|
|
199
|
-
// For records, "loose" means we don't throw errors for validation failures
|
|
200
|
-
// We return a modified version that catches validation errors
|
|
201
|
-
return new VldLooseRecord(this.valueValidator);
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
/**
|
|
205
|
-
* Loose record variant that allows validation failures
|
|
206
|
-
* Used internally by .loose() method
|
|
207
|
-
*/
|
|
208
|
-
class VldLooseRecord extends VldBase {
|
|
209
|
-
constructor(valueValidator) {
|
|
210
|
-
super(VLD_VALIDATOR_TYPES.RECORD);
|
|
211
|
-
this.valueValidator = valueValidator;
|
|
212
|
-
}
|
|
213
|
-
get valueSchema() {
|
|
214
|
-
return this.valueValidator;
|
|
215
|
-
}
|
|
216
|
-
parse(value) {
|
|
217
|
-
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
218
|
-
throw new Error(getMessages().invalidRecord);
|
|
219
|
-
}
|
|
220
|
-
const result = {};
|
|
221
|
-
const obj = value;
|
|
222
|
-
for (const [key, val] of Object.entries(obj)) {
|
|
223
|
-
// Skip dangerous keys
|
|
224
|
-
if (isDangerousKey(key)) {
|
|
225
|
-
continue;
|
|
226
|
-
}
|
|
227
|
-
// For loose records, skip invalid values instead of throwing
|
|
228
|
-
const parseResult = this.valueValidator.safeParse(val);
|
|
229
|
-
if (parseResult.success) {
|
|
230
|
-
result[key] = parseResult.data;
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
return result;
|
|
234
|
-
}
|
|
235
|
-
safeParse(value) {
|
|
236
|
-
try {
|
|
237
|
-
return { success: true, data: this.parse(value) };
|
|
238
|
-
}
|
|
239
|
-
catch (error) {
|
|
240
|
-
return { success: false, error: createRecordError(error.message) };
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
/**
|
|
244
|
-
* Create a partial variant (all keys optional)
|
|
245
|
-
*/
|
|
246
|
-
partial() {
|
|
247
|
-
const optionalValidator = this.valueValidator.optional();
|
|
248
|
-
return new VldLooseRecord(optionalValidator);
|
|
249
|
-
}
|
|
250
|
-
/**
|
|
251
|
-
* Return self (already loose)
|
|
252
|
-
*/
|
|
253
|
-
loose() {
|
|
254
|
-
return this;
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
export { VldRecord };
|
|
1
|
+
import{VldBase as e,VLD_VALIDATOR_TYPES as r}from"./base.js";import{getMessages as t}from"../locales/runtime.js";import{i}from"../chunks/security-Dy24T8N7.js";import{V as a}from"../chunks/errors-core-CerMg2rq.js";function o(e){return new a([{code:"invalid_object",path:[],message:e}])}class VldRecord extends e{constructor(e,t,i){super(r.RECORD),this.valueValidator=e,this.keyValidator=t,this.errorMessage=i,this._simpleValueMode=this.getSimpleValueMode(e),this._simpleValue="literal"===this._simpleValueMode?e.literal:void 0}get valueSchema(){return this.valueValidator}get keySchema(){return this.keyValidator}get valueType(){return this.valueValidator}get keyType(){return this.keyValidator}static create(e,r){return new VldRecord(e,r)}getSimpleValueMode(e){if(!0===e.isSimple)switch(e.validatorType){case r.STRING:return"string";case r.NUMBER:return"number";case r.BOOLEAN:return"boolean";case r.BIGINT:return"bigint";case r.SYMBOL:return"symbol";case r.NULL:return"null";case r.UNDEFINED:case r.VOID:return"undefinedValue";case r.LITERAL:return"literal";case r.ANY:case r.UNKNOWN:return"passthrough";default:return}}getSimpleValueError(e){switch(this._simpleValueMode){case"string":return t().invalidString;case"number":return t().invalidNumber;case"boolean":return t().invalidBoolean;case"bigint":return t().invalidBigint;case"symbol":return t().invalidSymbol;case"null":return"Expected null, received "+typeof e;case"undefinedValue":return t().expectedUndefined;case"literal":return t().literalExpected(JSON.stringify(this._simpleValue),JSON.stringify(e));default:return t().invalidRecord}}parse(e){if("object"!=typeof e||null===e||Array.isArray(e))throw new Error(this.errorMessage||t().invalidRecord);return this.parseKnownRecord(e)}parseKnownRecord(e){if(void 0!==this.keyValidator)return this.parseKeyedRecord(e);const r={},a=Object.keys(e),o=this._simpleValueMode;for(let s=0;s<a.length;s++){const n=a[s];if(i(n))continue;const l=e[n];if(void 0!==o)switch(o){case"string":if("string"!=typeof l)throw new Error(t().objectField(n,t().invalidString));r[n]=l;continue;case"number":if("number"!=typeof l||isNaN(l))throw new Error(t().objectField(n,t().invalidNumber));r[n]=l;continue;case"boolean":if("boolean"!=typeof l)throw new Error(t().objectField(n,t().invalidBoolean));r[n]=l;continue;case"bigint":if("bigint"!=typeof l)throw new Error(t().objectField(n,t().invalidBigint));r[n]=l;continue;case"symbol":if("symbol"!=typeof l)throw new Error(t().objectField(n,t().invalidSymbol));r[n]=l;continue;case"null":if(null!==l)throw new Error(t().objectField(n,this.getSimpleValueError(l)));r[n]=null;continue;case"undefinedValue":if(void 0!==l)throw new Error(t().objectField(n,t().expectedUndefined));r[n]=void 0;continue;case"literal":if(l!==this._simpleValue)throw new Error(t().objectField(n,this.getSimpleValueError(l)));r[n]=this._simpleValue;continue;case"passthrough":r[n]=l;continue}try{r[n]=this.valueValidator.parse(l)}catch(e){throw new Error(t().objectField(n,e.message))}}return r}parseKeyedRecord(e){const r={},o=Reflect.ownKeys(e);for(const s of o){if(!Object.prototype.propertyIsEnumerable.call(e,s))continue;if("string"==typeof s&&i(s))continue;const o=this.keyValidator.safeParse(s);if(!o.success)throw new a([{code:"invalid_key",path:[String(s)],message:o.error.message}]);if("string"!=typeof o.data&&"number"!=typeof o.data&&"symbol"!=typeof o.data)throw new a([{code:"invalid_key",path:[String(s)],message:"Record key transform must return a property key"}]);if("string"!=typeof o.data||!i(o.data))try{r[o.data]=this.valueValidator.parse(e[s])}catch(e){throw new Error(t().objectField(String(s),e.message))}}return r}safeParse(e){try{return{success:!0,data:this.parse(e)}}catch(e){return e instanceof a?{success:!1,error:e}:{success:!1,error:o(e.message)}}}partial(){const e=this.valueValidator.optional();return new VldRecord(e,this.keyValidator)}loose(){return new VldLooseRecord(this.valueValidator,this.keyValidator)}}class VldLooseRecord extends e{constructor(e,t){super(r.RECORD),this.valueValidator=e,this.keyValidator=t}get valueSchema(){return this.valueValidator}parse(e){if("object"!=typeof e||null===e||Array.isArray(e))throw new Error(t().invalidRecord);const r={},a=e;for(const e of Reflect.ownKeys(a)){if(!Object.prototype.propertyIsEnumerable.call(a,e))continue;if("string"==typeof e&&i(e))continue;const t=this.keyValidator?.safeParse(e);if(t&&!t.success)continue;const o=t?.data??e;if("string"!=typeof o&&"number"!=typeof o&&"symbol"!=typeof o)continue;if("string"==typeof o&&i(o))continue;const s=this.valueValidator.safeParse(a[e]);s.success&&(r[o]=s.data)}return r}safeParse(e){try{return{success:!0,data:this.parse(e)}}catch(e){return{success:!1,error:o(e.message)}}}partial(){const e=this.valueValidator.optional();return new VldLooseRecord(e,this.keyValidator)}loose(){return this}}export{VldRecord};
|
package/dist/validators/set.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ import { VldBase, ParseResult } from './base.js';
|
|
|
5
5
|
export declare class VldSet<T> extends VldBase<unknown, Set<T>> {
|
|
6
6
|
private readonly itemValidator;
|
|
7
7
|
private readonly errorMessage?;
|
|
8
|
+
private readonly _checks;
|
|
8
9
|
/**
|
|
9
10
|
* Private constructor to enforce immutability
|
|
10
11
|
*/
|
|
@@ -31,4 +32,8 @@ export declare class VldSet<T> extends VldBase<unknown, Set<T>> {
|
|
|
31
32
|
* Safely parse and validate a Set value
|
|
32
33
|
*/
|
|
33
34
|
safeParse(value: unknown): ParseResult<Set<T>>;
|
|
35
|
+
min(size: number, message?: string): VldSet<T>;
|
|
36
|
+
max(size: number, message?: string): VldSet<T>;
|
|
37
|
+
size(size: number, message?: string): VldSet<T>;
|
|
38
|
+
nonempty(message?: string): VldSet<T>;
|
|
34
39
|
}
|