@oxog/vld 1.1.1 → 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +157 -0
- package/README.md +1128 -840
- package/dist/codecs/index.d.ts +89 -0
- package/dist/codecs/index.d.ts.map +1 -0
- package/dist/codecs/index.js +256 -0
- package/dist/codecs/index.js.map +1 -0
- package/dist/coercion/boolean.d.ts.map +1 -1
- package/dist/coercion/boolean.js +6 -2
- package/dist/coercion/boolean.js.map +1 -1
- package/dist/coercion/string.d.ts.map +1 -1
- package/dist/coercion/string.js +146 -10
- package/dist/coercion/string.js.map +1 -1
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +27 -1
- package/dist/index.js.map +1 -1
- package/dist/locales/af.d.ts.map +1 -1
- package/dist/locales/af.js +15 -1
- package/dist/locales/af.js.map +1 -1
- package/dist/locales/ar.d.ts.map +1 -1
- package/dist/locales/ar.js +15 -1
- package/dist/locales/ar.js.map +1 -1
- package/dist/locales/bn.d.ts.map +1 -1
- package/dist/locales/bn.js +15 -1
- package/dist/locales/bn.js.map +1 -1
- package/dist/locales/da.d.ts.map +1 -1
- package/dist/locales/da.js +15 -1
- package/dist/locales/da.js.map +1 -1
- package/dist/locales/de.d.ts.map +1 -1
- package/dist/locales/de.js +15 -1
- package/dist/locales/de.js.map +1 -1
- package/dist/locales/en.d.ts.map +1 -1
- package/dist/locales/en.js +15 -1
- package/dist/locales/en.js.map +1 -1
- package/dist/locales/es-MX.d.ts.map +1 -1
- package/dist/locales/es-MX.js +15 -1
- package/dist/locales/es-MX.js.map +1 -1
- package/dist/locales/es.d.ts.map +1 -1
- package/dist/locales/es.js +15 -1
- package/dist/locales/es.js.map +1 -1
- package/dist/locales/fi.d.ts.map +1 -1
- package/dist/locales/fi.js +15 -1
- package/dist/locales/fi.js.map +1 -1
- package/dist/locales/fr.d.ts.map +1 -1
- package/dist/locales/fr.js +15 -1
- package/dist/locales/fr.js.map +1 -1
- package/dist/locales/hi.d.ts.map +1 -1
- package/dist/locales/hi.js +15 -1
- package/dist/locales/hi.js.map +1 -1
- package/dist/locales/id.d.ts.map +1 -1
- package/dist/locales/id.js +15 -1
- package/dist/locales/id.js.map +1 -1
- package/dist/locales/it.d.ts.map +1 -1
- package/dist/locales/it.js +15 -1
- package/dist/locales/it.js.map +1 -1
- package/dist/locales/ja.d.ts.map +1 -1
- package/dist/locales/ja.js +15 -1
- package/dist/locales/ja.js.map +1 -1
- package/dist/locales/ko.d.ts.map +1 -1
- package/dist/locales/ko.js +15 -1
- package/dist/locales/ko.js.map +1 -1
- package/dist/locales/nl.d.ts.map +1 -1
- package/dist/locales/nl.js +15 -1
- package/dist/locales/nl.js.map +1 -1
- package/dist/locales/no.d.ts.map +1 -1
- package/dist/locales/no.js +15 -1
- package/dist/locales/no.js.map +1 -1
- package/dist/locales/pl.d.ts.map +1 -1
- package/dist/locales/pl.js +15 -1
- package/dist/locales/pl.js.map +1 -1
- package/dist/locales/pt-BR.d.ts.map +1 -1
- package/dist/locales/pt-BR.js +15 -1
- package/dist/locales/pt-BR.js.map +1 -1
- package/dist/locales/pt.d.ts.map +1 -1
- package/dist/locales/pt.js +15 -1
- package/dist/locales/pt.js.map +1 -1
- package/dist/locales/ru.d.ts.map +1 -1
- package/dist/locales/ru.js +15 -1
- package/dist/locales/ru.js.map +1 -1
- package/dist/locales/sv.d.ts.map +1 -1
- package/dist/locales/sv.js +15 -1
- package/dist/locales/sv.js.map +1 -1
- package/dist/locales/sw.d.ts.map +1 -1
- package/dist/locales/sw.js +15 -1
- package/dist/locales/sw.js.map +1 -1
- package/dist/locales/th.d.ts.map +1 -1
- package/dist/locales/th.js +15 -1
- package/dist/locales/th.js.map +1 -1
- package/dist/locales/tr.d.ts.map +1 -1
- package/dist/locales/tr.js +15 -1
- package/dist/locales/tr.js.map +1 -1
- package/dist/locales/types.d.ts +10 -0
- package/dist/locales/types.d.ts.map +1 -1
- package/dist/locales/types.js.map +1 -1
- package/dist/locales/vi.d.ts.map +1 -1
- package/dist/locales/vi.js +15 -1
- package/dist/locales/vi.js.map +1 -1
- package/dist/locales/zh.d.ts.map +1 -1
- package/dist/locales/zh.js +15 -1
- package/dist/locales/zh.js.map +1 -1
- package/dist/utils/codec-utils.d.ts +44 -0
- package/dist/utils/codec-utils.d.ts.map +1 -0
- package/dist/utils/codec-utils.js +205 -0
- package/dist/utils/codec-utils.js.map +1 -0
- package/dist/utils/deep-merge.d.ts +1 -0
- package/dist/utils/deep-merge.d.ts.map +1 -1
- package/dist/utils/deep-merge.js +33 -8
- package/dist/utils/deep-merge.js.map +1 -1
- package/dist/validators/array.d.ts +5 -0
- package/dist/validators/array.d.ts.map +1 -1
- package/dist/validators/array.js +30 -1
- package/dist/validators/array.js.map +1 -1
- package/dist/validators/base64.d.ts +18 -0
- package/dist/validators/base64.d.ts.map +1 -0
- package/dist/validators/base64.js +65 -0
- package/dist/validators/base64.js.map +1 -0
- package/dist/validators/codec.d.ts +55 -0
- package/dist/validators/codec.d.ts.map +1 -0
- package/dist/validators/codec.js +182 -0
- package/dist/validators/codec.js.map +1 -0
- package/dist/validators/date.d.ts +5 -1
- package/dist/validators/date.d.ts.map +1 -1
- package/dist/validators/date.js +53 -29
- package/dist/validators/date.js.map +1 -1
- package/dist/validators/hex.d.ts +17 -0
- package/dist/validators/hex.d.ts.map +1 -0
- package/dist/validators/hex.js +61 -0
- package/dist/validators/hex.js.map +1 -0
- package/dist/validators/number.d.ts.map +1 -1
- package/dist/validators/number.js +16 -3
- package/dist/validators/number.js.map +1 -1
- package/dist/validators/object.d.ts +5 -0
- package/dist/validators/object.d.ts.map +1 -1
- package/dist/validators/object.js +58 -6
- package/dist/validators/object.js.map +1 -1
- package/dist/validators/record.d.ts.map +1 -1
- package/dist/validators/record.js +6 -0
- package/dist/validators/record.js.map +1 -1
- package/dist/validators/string.d.ts.map +1 -1
- package/dist/validators/string.js +86 -4
- package/dist/validators/string.js.map +1 -1
- package/dist/validators/uint8array.d.ts +25 -0
- package/dist/validators/uint8array.d.ts.map +1 -0
- package/dist/validators/uint8array.js +87 -0
- package/dist/validators/uint8array.js.map +1 -0
- package/dist/validators/union.d.ts +1 -0
- package/dist/validators/union.d.ts.map +1 -1
- package/dist/validators/union.js +31 -20
- package/dist/validators/union.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility functions for codec transformations
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Convert a base64 string to Uint8Array
|
|
6
|
+
*/
|
|
7
|
+
export function base64ToUint8Array(base64) {
|
|
8
|
+
// Handle URL-safe base64
|
|
9
|
+
const base64Clean = base64.replace(/-/g, '+').replace(/_/g, '/');
|
|
10
|
+
// Add padding if necessary
|
|
11
|
+
const padded = base64Clean.padEnd(base64Clean.length + (4 - base64Clean.length % 4) % 4, '=');
|
|
12
|
+
// Decode base64 to binary string
|
|
13
|
+
const binaryString = safeAtob(padded);
|
|
14
|
+
// Convert binary string to Uint8Array
|
|
15
|
+
const bytes = new Uint8Array(binaryString.length);
|
|
16
|
+
for (let i = 0; i < binaryString.length; i++) {
|
|
17
|
+
bytes[i] = binaryString.charCodeAt(i);
|
|
18
|
+
}
|
|
19
|
+
return bytes;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Convert Uint8Array to base64 string
|
|
23
|
+
*/
|
|
24
|
+
export function uint8ArrayToBase64(bytes) {
|
|
25
|
+
// Convert Uint8Array to binary string
|
|
26
|
+
let binaryString = '';
|
|
27
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
28
|
+
binaryString += String.fromCharCode(bytes[i]);
|
|
29
|
+
}
|
|
30
|
+
// Encode to base64
|
|
31
|
+
return safeBtoa(binaryString);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Convert Uint8Array to URL-safe base64 string
|
|
35
|
+
*/
|
|
36
|
+
export function uint8ArrayToBase64Url(bytes) {
|
|
37
|
+
const base64 = uint8ArrayToBase64(bytes);
|
|
38
|
+
// Make URL-safe and remove padding
|
|
39
|
+
return base64
|
|
40
|
+
.replace(/\+/g, '-')
|
|
41
|
+
.replace(/\//g, '_')
|
|
42
|
+
.replace(/=/g, '');
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Convert hex string to Uint8Array
|
|
46
|
+
*/
|
|
47
|
+
export function hexToUint8Array(hex) {
|
|
48
|
+
// Remove any 0x prefix if present
|
|
49
|
+
const cleanHex = hex.startsWith('0x') ? hex.slice(2) : hex;
|
|
50
|
+
// Ensure even length
|
|
51
|
+
const paddedHex = cleanHex.length % 2 === 0 ? cleanHex : '0' + cleanHex;
|
|
52
|
+
const bytes = new Uint8Array(paddedHex.length / 2);
|
|
53
|
+
for (let i = 0; i < paddedHex.length; i += 2) {
|
|
54
|
+
bytes[i / 2] = parseInt(paddedHex.substring(i, i + 2), 16);
|
|
55
|
+
}
|
|
56
|
+
return bytes;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Convert Uint8Array to hex string
|
|
60
|
+
*/
|
|
61
|
+
export function uint8ArrayToHex(bytes) {
|
|
62
|
+
let hex = '';
|
|
63
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
64
|
+
const byte = bytes[i].toString(16);
|
|
65
|
+
hex += byte.length === 1 ? '0' + byte : byte;
|
|
66
|
+
}
|
|
67
|
+
return hex;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Convert string to Uint8Array using UTF-8 encoding
|
|
71
|
+
*/
|
|
72
|
+
export function stringToUint8Array(str) {
|
|
73
|
+
const encoder = new TextEncoder();
|
|
74
|
+
return encoder.encode(str);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Convert Uint8Array to string using UTF-8 decoding
|
|
78
|
+
*/
|
|
79
|
+
export function uint8ArrayToString(bytes) {
|
|
80
|
+
const decoder = new TextDecoder();
|
|
81
|
+
return decoder.decode(bytes);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Check if running in Node.js environment
|
|
85
|
+
*/
|
|
86
|
+
export function isNodeEnvironment() {
|
|
87
|
+
return typeof globalThis !== 'undefined' &&
|
|
88
|
+
typeof globalThis.process !== 'undefined' &&
|
|
89
|
+
globalThis.process.versions != null &&
|
|
90
|
+
globalThis.process.versions.node != null;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Validate and sanitize base64 input to prevent prototype pollution
|
|
94
|
+
*/
|
|
95
|
+
function validateBase64Input(encoded) {
|
|
96
|
+
// Check for null/undefined
|
|
97
|
+
if (encoded == null) {
|
|
98
|
+
throw new Error('Base64 input cannot be null or undefined');
|
|
99
|
+
}
|
|
100
|
+
// Ensure it's a string
|
|
101
|
+
if (typeof encoded !== 'string') {
|
|
102
|
+
throw new Error('Base64 input must be a string');
|
|
103
|
+
}
|
|
104
|
+
// Length validation - base64 strings should be reasonable length
|
|
105
|
+
if (encoded.length > 10000000) { // 10MB limit
|
|
106
|
+
throw new Error('Base64 input is too large');
|
|
107
|
+
}
|
|
108
|
+
// Base64 character validation - only allow valid base64 characters
|
|
109
|
+
const base64Regex = /^[A-Za-z0-9+/]*={0,2}$/;
|
|
110
|
+
if (!base64Regex.test(encoded)) {
|
|
111
|
+
throw new Error('Invalid base64 format');
|
|
112
|
+
}
|
|
113
|
+
// Check for padding issues
|
|
114
|
+
const paddingIndex = encoded.indexOf('=');
|
|
115
|
+
if (paddingIndex !== -1) {
|
|
116
|
+
const padding = encoded.substring(paddingIndex);
|
|
117
|
+
// Only allow '=', '==', or '===' as padding
|
|
118
|
+
if (!/^={1,2}$/.test(padding) && padding !== '===') {
|
|
119
|
+
throw new Error('Invalid base64 padding');
|
|
120
|
+
}
|
|
121
|
+
// Padding should only be at the end
|
|
122
|
+
if (paddingIndex < encoded.length - 3) {
|
|
123
|
+
throw new Error('Base64 padding must be at the end');
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
// Additional security check: prevent potential encoded JavaScript
|
|
127
|
+
// by looking for suspicious patterns that might indicate malicious content
|
|
128
|
+
const suspiciousPatterns = [
|
|
129
|
+
/eval/i,
|
|
130
|
+
/function/i,
|
|
131
|
+
/javascript:/i,
|
|
132
|
+
/<script/i,
|
|
133
|
+
/on\w+\s*=/i,
|
|
134
|
+
/__proto__/i,
|
|
135
|
+
/constructor/i,
|
|
136
|
+
/prototype/i
|
|
137
|
+
];
|
|
138
|
+
// Decode a small portion to check for suspicious content
|
|
139
|
+
try {
|
|
140
|
+
const sampleSize = Math.min(100, encoded.length);
|
|
141
|
+
const sample = encoded.substring(0, sampleSize);
|
|
142
|
+
const sampleDecoded = typeof atob !== 'undefined'
|
|
143
|
+
? atob(sample)
|
|
144
|
+
: Buffer.from(sample, 'base64').toString('utf8', 0, 50); // Only decode first 50 chars
|
|
145
|
+
for (const pattern of suspiciousPatterns) {
|
|
146
|
+
if (pattern.test(sampleDecoded)) {
|
|
147
|
+
throw new Error('Suspicious content detected in base64 input');
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
catch (error) {
|
|
152
|
+
if (error instanceof Error && error.message.includes('Suspicious content')) {
|
|
153
|
+
throw error;
|
|
154
|
+
}
|
|
155
|
+
// If decoding fails, continue - it might be invalid base64 which will be caught later
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Polyfill for atob in Node.js with proper input validation
|
|
160
|
+
*/
|
|
161
|
+
export function safeAtob(encoded) {
|
|
162
|
+
// Input validation and sanitization
|
|
163
|
+
validateBase64Input(encoded);
|
|
164
|
+
if (typeof atob !== 'undefined') {
|
|
165
|
+
try {
|
|
166
|
+
return atob(encoded);
|
|
167
|
+
}
|
|
168
|
+
catch (error) {
|
|
169
|
+
throw new Error('Failed to decode base64 data');
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
// Node.js fallback with additional validation
|
|
173
|
+
if (isNodeEnvironment()) {
|
|
174
|
+
try {
|
|
175
|
+
// Use Buffer with explicit error handling
|
|
176
|
+
const buffer = Buffer.from(encoded, 'base64');
|
|
177
|
+
// Additional validation: ensure the decoded data is reasonable size
|
|
178
|
+
if (buffer.length > 5000000) { // 5MB limit for decoded data
|
|
179
|
+
throw new Error('Decoded base64 data is too large');
|
|
180
|
+
}
|
|
181
|
+
return buffer.toString('binary');
|
|
182
|
+
}
|
|
183
|
+
catch (error) {
|
|
184
|
+
if (error instanceof Error && error.message.includes('Invalid base64')) {
|
|
185
|
+
throw new Error('Invalid base64 format');
|
|
186
|
+
}
|
|
187
|
+
throw new Error('Failed to decode base64 data in Node.js environment');
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
throw new Error('Base64 decoding not supported in this environment');
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Polyfill for btoa in Node.js
|
|
194
|
+
*/
|
|
195
|
+
export function safeBtoa(str) {
|
|
196
|
+
if (typeof btoa !== 'undefined') {
|
|
197
|
+
return btoa(str);
|
|
198
|
+
}
|
|
199
|
+
// Node.js fallback
|
|
200
|
+
if (isNodeEnvironment()) {
|
|
201
|
+
return Buffer.from(str, 'binary').toString('base64');
|
|
202
|
+
}
|
|
203
|
+
throw new Error('Base64 encoding not supported in this environment');
|
|
204
|
+
}
|
|
205
|
+
//# sourceMappingURL=codec-utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"codec-utils.js","sourceRoot":"","sources":["../../src/utils/codec-utils.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;GAEG;AACH,MAAM,UAAU,kBAAkB,CAAC,MAAc;IAC/C,yBAAyB;IACzB,MAAM,WAAW,GAAG,MAAM,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;IAEjE,2BAA2B;IAC3B,MAAM,MAAM,GAAG,WAAW,CAAC,MAAM,CAAC,WAAW,CAAC,MAAM,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,GAAG,CAAC,CAAC;IACnB,CAAC;IAED,mBAAmB;IACnB,IAAI,iBAAiB,EAAE,EAAE,CAAC;QACxB,OAAO,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;IACvD,CAAC;IAED,MAAM,IAAI,KAAK,CAAC,mDAAmD,CAAC,CAAC;AACvE,CAAC","sourcesContent":["/**\r\n * Utility functions for codec transformations\r\n */\r\n\r\n/**\r\n * Convert a base64 string to Uint8Array\r\n */\r\nexport function base64ToUint8Array(base64: string): Uint8Array {\r\n // Handle URL-safe base64\r\n const base64Clean = base64.replace(/-/g, '+').replace(/_/g, '/');\r\n \r\n // Add padding if necessary\r\n const padded = base64Clean.padEnd(base64Clean.length + (4 - base64Clean.length % 4) % 4, '=');\r\n \r\n // Decode base64 to binary string\r\n const binaryString = safeAtob(padded);\r\n \r\n // Convert binary string to Uint8Array\r\n const bytes = new Uint8Array(binaryString.length);\r\n for (let i = 0; i < binaryString.length; i++) {\r\n bytes[i] = binaryString.charCodeAt(i);\r\n }\r\n \r\n return bytes;\r\n}\r\n\r\n/**\r\n * Convert Uint8Array to base64 string\r\n */\r\nexport function uint8ArrayToBase64(bytes: Uint8Array): string {\r\n // Convert Uint8Array to binary string\r\n let binaryString = '';\r\n for (let i = 0; i < bytes.length; i++) {\r\n binaryString += String.fromCharCode(bytes[i]);\r\n }\r\n \r\n // Encode to base64\r\n return safeBtoa(binaryString);\r\n}\r\n\r\n/**\r\n * Convert Uint8Array to URL-safe base64 string\r\n */\r\nexport function uint8ArrayToBase64Url(bytes: Uint8Array): string {\r\n const base64 = uint8ArrayToBase64(bytes);\r\n // Make URL-safe and remove padding\r\n return base64\r\n .replace(/\\+/g, '-')\r\n .replace(/\\//g, '_')\r\n .replace(/=/g, '');\r\n}\r\n\r\n/**\r\n * Convert hex string to Uint8Array\r\n */\r\nexport function hexToUint8Array(hex: string): Uint8Array {\r\n // Remove any 0x prefix if present\r\n const cleanHex = hex.startsWith('0x') ? hex.slice(2) : hex;\r\n \r\n // Ensure even length\r\n const paddedHex = cleanHex.length % 2 === 0 ? cleanHex : '0' + cleanHex;\r\n \r\n const bytes = new Uint8Array(paddedHex.length / 2);\r\n for (let i = 0; i < paddedHex.length; i += 2) {\r\n bytes[i / 2] = parseInt(paddedHex.substring(i, i + 2), 16);\r\n }\r\n \r\n return bytes;\r\n}\r\n\r\n/**\r\n * Convert Uint8Array to hex string\r\n */\r\nexport function uint8ArrayToHex(bytes: Uint8Array): string {\r\n let hex = '';\r\n for (let i = 0; i < bytes.length; i++) {\r\n const byte = bytes[i].toString(16);\r\n hex += byte.length === 1 ? '0' + byte : byte;\r\n }\r\n return hex;\r\n}\r\n\r\n/**\r\n * Convert string to Uint8Array using UTF-8 encoding\r\n */\r\nexport function stringToUint8Array(str: string): Uint8Array {\r\n const encoder = new TextEncoder();\r\n return encoder.encode(str);\r\n}\r\n\r\n/**\r\n * Convert Uint8Array to string using UTF-8 decoding\r\n */\r\nexport function uint8ArrayToString(bytes: Uint8Array): string {\r\n const decoder = new TextDecoder();\r\n return decoder.decode(bytes);\r\n}\r\n\r\n/**\r\n * Check if running in Node.js environment\r\n */\r\nexport function isNodeEnvironment(): boolean {\r\n return typeof globalThis !== 'undefined' && \r\n typeof globalThis.process !== 'undefined' &&\r\n globalThis.process.versions != null &&\r\n globalThis.process.versions.node != null;\r\n}\r\n\r\n/**\r\n * Validate and sanitize base64 input to prevent prototype pollution\r\n */\r\nfunction validateBase64Input(encoded: string): void {\r\n // Check for null/undefined\r\n if (encoded == null) {\r\n throw new Error('Base64 input cannot be null or undefined');\r\n }\r\n\r\n // Ensure it's a string\r\n if (typeof encoded !== 'string') {\r\n throw new Error('Base64 input must be a string');\r\n }\r\n\r\n // Length validation - base64 strings should be reasonable length\r\n if (encoded.length > 10000000) { // 10MB limit\r\n throw new Error('Base64 input is too large');\r\n }\r\n\r\n // Base64 character validation - only allow valid base64 characters\r\n const base64Regex = /^[A-Za-z0-9+/]*={0,2}$/;\r\n if (!base64Regex.test(encoded)) {\r\n throw new Error('Invalid base64 format');\r\n }\r\n\r\n // Check for padding issues\r\n const paddingIndex = encoded.indexOf('=');\r\n if (paddingIndex !== -1) {\r\n const padding = encoded.substring(paddingIndex);\r\n // Only allow '=', '==', or '===' as padding\r\n if (!/^={1,2}$/.test(padding) && padding !== '===') {\r\n throw new Error('Invalid base64 padding');\r\n }\r\n // Padding should only be at the end\r\n if (paddingIndex < encoded.length - 3) {\r\n throw new Error('Base64 padding must be at the end');\r\n }\r\n }\r\n\r\n // Additional security check: prevent potential encoded JavaScript\r\n // by looking for suspicious patterns that might indicate malicious content\r\n const suspiciousPatterns = [\r\n /eval/i,\r\n /function/i,\r\n /javascript:/i,\r\n /<script/i,\r\n /on\\w+\\s*=/i,\r\n /__proto__/i,\r\n /constructor/i,\r\n /prototype/i\r\n ];\r\n\r\n // Decode a small portion to check for suspicious content\r\n try {\r\n const sampleSize = Math.min(100, encoded.length);\r\n const sample = encoded.substring(0, sampleSize);\r\n const sampleDecoded = typeof atob !== 'undefined'\r\n ? atob(sample)\r\n : Buffer.from(sample, 'base64').toString('utf8', 0, 50); // Only decode first 50 chars\r\n\r\n for (const pattern of suspiciousPatterns) {\r\n if (pattern.test(sampleDecoded)) {\r\n throw new Error('Suspicious content detected in base64 input');\r\n }\r\n }\r\n } catch (error) {\r\n if (error instanceof Error && error.message.includes('Suspicious content')) {\r\n throw error;\r\n }\r\n // If decoding fails, continue - it might be invalid base64 which will be caught later\r\n }\r\n}\r\n\r\n/**\r\n * Polyfill for atob in Node.js with proper input validation\r\n */\r\nexport function safeAtob(encoded: string): string {\r\n // Input validation and sanitization\r\n validateBase64Input(encoded);\r\n\r\n if (typeof atob !== 'undefined') {\r\n try {\r\n return atob(encoded);\r\n } catch (error) {\r\n throw new Error('Failed to decode base64 data');\r\n }\r\n }\r\n\r\n // Node.js fallback with additional validation\r\n if (isNodeEnvironment()) {\r\n try {\r\n // Use Buffer with explicit error handling\r\n const buffer = Buffer.from(encoded, 'base64');\r\n\r\n // Additional validation: ensure the decoded data is reasonable size\r\n if (buffer.length > 5000000) { // 5MB limit for decoded data\r\n throw new Error('Decoded base64 data is too large');\r\n }\r\n\r\n return buffer.toString('binary');\r\n } catch (error) {\r\n if (error instanceof Error && error.message.includes('Invalid base64')) {\r\n throw new Error('Invalid base64 format');\r\n }\r\n throw new Error('Failed to decode base64 data in Node.js environment');\r\n }\r\n }\r\n\r\n throw new Error('Base64 decoding not supported in this environment');\r\n}\r\n\r\n/**\r\n * Polyfill for btoa in Node.js\r\n */\r\nexport function safeBtoa(str: string): string {\r\n if (typeof btoa !== 'undefined') {\r\n return btoa(str);\r\n }\r\n \r\n // Node.js fallback\r\n if (isNodeEnvironment()) {\r\n return Buffer.from(str, 'binary').toString('base64');\r\n }\r\n \r\n throw new Error('Base64 encoding not supported in this environment');\r\n}"]}
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
export declare function isPlainObject(obj: any): obj is Record<string, any>;
|
|
5
5
|
/**
|
|
6
6
|
* Safely deep merge two objects without prototype pollution vulnerability
|
|
7
|
+
* Optimized for performance with minimal object allocations
|
|
7
8
|
* @param target The target object
|
|
8
9
|
* @param source The source object to merge from
|
|
9
10
|
* @returns A new merged object
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"deep-merge.d.ts","sourceRoot":"","sources":["../../src/utils/deep-merge.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,GAAG,GAAG,GAAG,IAAI,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAOlE;
|
|
1
|
+
{"version":3,"file":"deep-merge.d.ts","sourceRoot":"","sources":["../../src/utils/deep-merge.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,GAAG,GAAG,GAAG,IAAI,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAOlE;AA6BD;;;;;;GAMG;AACH,wBAAgB,SAAS,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EACrD,MAAM,EAAE,CAAC,EACT,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,GACjB,CAAC,CAiCH;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAuBjD"}
|
package/dist/utils/deep-merge.js
CHANGED
|
@@ -7,23 +7,48 @@ export function isPlainObject(obj) {
|
|
|
7
7
|
obj.constructor === Object &&
|
|
8
8
|
Object.prototype.toString.call(obj) === '[object Object]');
|
|
9
9
|
}
|
|
10
|
+
/**
|
|
11
|
+
* Check if a key could be dangerous for prototype pollution
|
|
12
|
+
*/
|
|
13
|
+
function isDangerousKey(key) {
|
|
14
|
+
// Direct dangerous keys
|
|
15
|
+
const directDangerousKeys = ['__proto__', 'constructor', 'prototype'];
|
|
16
|
+
if (directDangerousKeys.includes(key)) {
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
// Nested prototype manipulation vectors
|
|
20
|
+
const nestedPatterns = [
|
|
21
|
+
'constructor.prototype',
|
|
22
|
+
'__proto__.toString',
|
|
23
|
+
'prototype.constructor'
|
|
24
|
+
];
|
|
25
|
+
// Check for nested patterns
|
|
26
|
+
for (const pattern of nestedPatterns) {
|
|
27
|
+
if (key.includes(pattern)) {
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
10
33
|
/**
|
|
11
34
|
* Safely deep merge two objects without prototype pollution vulnerability
|
|
35
|
+
* Optimized for performance with minimal object allocations
|
|
12
36
|
* @param target The target object
|
|
13
37
|
* @param source The source object to merge from
|
|
14
38
|
* @returns A new merged object
|
|
15
39
|
*/
|
|
16
40
|
export function deepMerge(target, source) {
|
|
17
|
-
//
|
|
18
|
-
const DANGEROUS_KEYS = ['__proto__', 'constructor', 'prototype'];
|
|
19
|
-
// Create a new object to avoid mutations
|
|
41
|
+
// Create a new object to avoid mutations (only once)
|
|
20
42
|
const result = { ...target };
|
|
21
|
-
|
|
43
|
+
// Get source keys directly for better performance
|
|
44
|
+
const sourceKeys = Object.keys(source);
|
|
45
|
+
for (let i = 0; i < sourceKeys.length; i++) {
|
|
46
|
+
const key = sourceKeys[i];
|
|
22
47
|
// Skip dangerous keys that could lead to prototype pollution
|
|
23
|
-
if (
|
|
48
|
+
if (isDangerousKey(key)) {
|
|
24
49
|
continue;
|
|
25
50
|
}
|
|
26
|
-
// Only process own properties
|
|
51
|
+
// Only process own properties (redundant with Object.keys but safe)
|
|
27
52
|
if (!Object.prototype.hasOwnProperty.call(source, key)) {
|
|
28
53
|
continue;
|
|
29
54
|
}
|
|
@@ -56,8 +81,8 @@ export function deepFreeze(obj) {
|
|
|
56
81
|
}
|
|
57
82
|
// Freeze the object/function itself
|
|
58
83
|
Object.freeze(obj);
|
|
59
|
-
// Recursively freeze all properties
|
|
60
|
-
|
|
84
|
+
// Recursively freeze all properties (including Symbol properties)
|
|
85
|
+
Reflect.ownKeys(obj).forEach(prop => {
|
|
61
86
|
const value = obj[prop];
|
|
62
87
|
if (value !== null && (typeof value === 'object' || typeof value === 'function')) {
|
|
63
88
|
deepFreeze(value);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"deep-merge.js","sourceRoot":"","sources":["../../src/utils/deep-merge.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,UAAU,aAAa,CAAC,GAAQ;IACpC,OAAO,CACL,GAAG,KAAK,IAAI;QACZ,OAAO,GAAG,KAAK,QAAQ;QACvB,GAAG,CAAC,WAAW,KAAK,MAAM;QAC1B,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,iBAAiB,CAC1D,CAAC;AACJ,CAAC;AAED
|
|
1
|
+
{"version":3,"file":"deep-merge.js","sourceRoot":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AO,KAAK,KAAK,UAAU,CAAC,EAAE,CAAC;YACjF,UAAU,CAAC,KAAK,CAAC,CAAC;QACpB,CAAC;IACH,CAAC,CAAC,CAAC;IAEH,OAAO,GAAG,CAAC;AACb,CAAC","sourcesContent":["/**\r\n * Type guard to check if a value is a plain object\r\n */\r\nexport function isPlainObject(obj: any): obj is Record<string, any> {\r\n return (\r\n obj !== null &&\r\n typeof obj === 'object' &&\r\n obj.constructor === Object &&\r\n Object.prototype.toString.call(obj) === '[object Object]'\r\n );\r\n}\r\n\r\n/**\r\n * Check if a key could be dangerous for prototype pollution\r\n */\r\nfunction isDangerousKey(key: string): boolean {\r\n // Direct dangerous keys\r\n const directDangerousKeys = ['__proto__', 'constructor', 'prototype'];\r\n if (directDangerousKeys.includes(key)) {\r\n return true;\r\n }\r\n\r\n // Nested prototype manipulation vectors\r\n const nestedPatterns = [\r\n 'constructor.prototype',\r\n '__proto__.toString',\r\n 'prototype.constructor'\r\n ];\r\n\r\n // Check for nested patterns\r\n for (const pattern of nestedPatterns) {\r\n if (key.includes(pattern)) {\r\n return true;\r\n }\r\n }\r\n\r\n return false;\r\n}\r\n\r\n/**\r\n * Safely deep merge two objects without prototype pollution vulnerability\r\n * Optimized for performance with minimal object allocations\r\n * @param target The target object\r\n * @param source The source object to merge from\r\n * @returns A new merged object\r\n */\r\nexport function deepMerge<T extends Record<string, any>>(\r\n target: T,\r\n source: Partial<T>\r\n): T {\r\n // Create a new object to avoid mutations (only once)\r\n const result = { ...target } as T;\r\n\r\n // Get source keys directly for better performance\r\n const sourceKeys = Object.keys(source);\r\n\r\n for (let i = 0; i < sourceKeys.length; i++) {\r\n const key = sourceKeys[i];\r\n\r\n // Skip dangerous keys that could lead to prototype pollution\r\n if (isDangerousKey(key)) {\r\n continue;\r\n }\r\n\r\n // Only process own properties (redundant with Object.keys but safe)\r\n if (!Object.prototype.hasOwnProperty.call(source, key)) {\r\n continue;\r\n }\r\n\r\n const sourceValue = source[key];\r\n const targetValue = target[key];\r\n\r\n // If both values are plain objects, merge them recursively\r\n if (isPlainObject(sourceValue) && isPlainObject(targetValue)) {\r\n (result as any)[key] = deepMerge(targetValue as any, sourceValue as any);\r\n } else {\r\n // Otherwise, use the source value (including undefined)\r\n (result as any)[key] = sourceValue;\r\n }\r\n }\r\n\r\n return result;\r\n}\r\n\r\n/**\r\n * Freeze an object deeply to prevent any mutations\r\n * @param obj Object to freeze\r\n * @returns The frozen object\r\n */\r\nexport function deepFreeze<T>(obj: T): Readonly<T> {\r\n // Primitive values don't need processing\r\n if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {\r\n return obj;\r\n }\r\n \r\n // Check if already frozen to prevent infinite recursion\r\n if (Object.isFrozen(obj)) {\r\n return obj;\r\n }\r\n \r\n // Freeze the object/function itself\r\n Object.freeze(obj);\r\n\r\n // Recursively freeze all properties (including Symbol properties)\r\n Reflect.ownKeys(obj).forEach(prop => {\r\n const value = (obj as any)[prop];\r\n if (value !== null && (typeof value === 'object' || typeof value === 'function')) {\r\n deepFreeze(value);\r\n }\r\n });\r\n \r\n return obj;\r\n}"]}
|
|
@@ -20,6 +20,11 @@ export declare class VldArray<T> extends VldBase<unknown[], T[]> {
|
|
|
20
20
|
* Safely parse and validate an array value
|
|
21
21
|
*/
|
|
22
22
|
safeParse(value: unknown): ParseResult<T[]>;
|
|
23
|
+
/**
|
|
24
|
+
* Create a stable string representation of an object for hashing
|
|
25
|
+
* Handles circular references gracefully
|
|
26
|
+
*/
|
|
27
|
+
private stableStringify;
|
|
23
28
|
/**
|
|
24
29
|
* Create a new validator with minimum length constraint
|
|
25
30
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"array.d.ts","sourceRoot":"","sources":["../../src/validators/array.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAC;AAe9C;;GAEG;AACH,qBAAa,QAAQ,CAAC,CAAC,CAAE,SAAQ,OAAO,CAAC,OAA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const item of result) {
|
|
51
|
-
const key = typeof item === 'object' ?
|
|
51
|
+
const key = typeof item === 'object' ? this.stableStringify(item) : item;
|
|
52
52
|
if (seen.has(key)) {
|
|
53
53
|
throw new Error('Array must contain unique items');
|
|
54
54
|
}
|
|
@@ -68,6 +68,35 @@ export class VldArray extends VldBase {
|
|
|
68
68
|
return { success: false, error: error };
|
|
69
69
|
}
|
|
70
70
|
}
|
|
71
|
+
/**
|
|
72
|
+
* Create a stable string representation of an object for hashing
|
|
73
|
+
* Handles circular references gracefully
|
|
74
|
+
*/
|
|
75
|
+
stableStringify(obj) {
|
|
76
|
+
const seen = new WeakSet();
|
|
77
|
+
const replacer = (_key, value) => {
|
|
78
|
+
if (typeof value === 'object' && value !== null) {
|
|
79
|
+
if (seen.has(value)) {
|
|
80
|
+
return '[Circular]';
|
|
81
|
+
}
|
|
82
|
+
seen.add(value);
|
|
83
|
+
}
|
|
84
|
+
return value;
|
|
85
|
+
};
|
|
86
|
+
try {
|
|
87
|
+
const allKeys = [];
|
|
88
|
+
JSON.stringify(obj, (key, value) => {
|
|
89
|
+
allKeys.push(key);
|
|
90
|
+
return replacer(key, value);
|
|
91
|
+
});
|
|
92
|
+
allKeys.sort();
|
|
93
|
+
return JSON.stringify(obj, (_key, value) => replacer(_key, value));
|
|
94
|
+
}
|
|
95
|
+
catch (error) {
|
|
96
|
+
// Fallback to safe representation if stringify fails
|
|
97
|
+
return String(obj);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
71
100
|
/**
|
|
72
101
|
* Create a new validator with minimum length constraint
|
|
73
102
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"array.js","sourceRoot":"","sources":["../../src/validators/array.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAe,MAAM,QAAQ,CAAC;AAC9C,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAczC;;GAEG;AACH,MAAM,OAAO,QAAY,SAAQ,OAAuB;IAGtD;;OAE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,IAAI,QAAQ,CAAC;YAClB,GAAG,IAAI,CAAC,MAAM;YACd,MAAM,EAAE,IAAI;YACZ,YAAY,EAAE,OAAO,IAAI,iCAAiC;SAC3D,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACH,OAAO,CAAC,GAAW,EAAE,GAAW,EAAE,OAAgB;QAChD,OAAO,IAAI,QAAQ,CAAC;YAClB,GAAG,IAAI,CAAC,MAAM;YACd,SAAS,EAAE,GAAG;YACd,SAAS,EAAE,GAAG;YACd,YAAY,EAAE,OAAO,IAAI,gCAAgC,GAAG,QAAQ,GAAG,EAAE;SAC1E,CAAC,CAAC;IACL,CAAC;CACF","sourcesContent":["import { VldBase, ParseResult } from './base';\nimport { getMessages } from '../locales';\n\n/**\n * Configuration for array validator\n */\ninterface ArrayValidatorConfig<T> {\n readonly itemValidator: VldBase<unknown, T>;\n readonly minLength?: number;\n readonly maxLength?: number;\n readonly exactLength?: number;\n readonly unique?: boolean;\n readonly errorMessage?: string;\n}\n\n/**\n * Immutable array validator with chainable methods\n */\nexport class VldArray<T> extends VldBase<unknown[], T[]> {\n private readonly config: ArrayValidatorConfig<T>;\n \n /**\n * Private constructor to enforce immutability\n */\n private constructor(config: ArrayValidatorConfig<T>) {\n super();\n this.config = config;\n }\n \n /**\n * Create a new array validator\n */\n static create<T>(itemValidator: VldBase<unknown, T>): VldArray<T> {\n return new VldArray({ itemValidator });\n }\n \n /**\n * Parse and validate an array value\n */\n parse(value: unknown): T[] {\n if (!Array.isArray(value)) {\n throw new Error(this.config.errorMessage || getMessages().invalidArray);\n }\n \n // Validate length constraints\n if (this.config.exactLength !== undefined && value.length !== this.config.exactLength) {\n throw new Error(this.config.errorMessage || getMessages().arrayLength(this.config.exactLength));\n }\n \n if (this.config.minLength !== undefined && value.length < this.config.minLength) {\n throw new Error(this.config.errorMessage || getMessages().arrayMin(this.config.minLength));\n }\n \n if (this.config.maxLength !== undefined && value.length > this.config.maxLength) {\n throw new Error(this.config.errorMessage || getMessages().arrayMax(this.config.maxLength));\n }\n \n // Validate each item with optimized loop and safeParse\n const result: T[] = [];\n const length = value.length;\n \n for (let i = 0; i < length; i++) {\n const parseResult = this.config.itemValidator.safeParse(value[i]);\n if (!parseResult.success) {\n throw new Error(getMessages().arrayItem(i, parseResult.error.message));\n }\n result[i] = parseResult.data; // Direct assignment is faster than push\n }\n \n // Check uniqueness if required\n if (this.config.unique) {\n const seen = new Set<any>();\n for (const item of result) {\n const key = typeof item === 'object' ? JSON.stringify(item) : item;\n if (seen.has(key)) {\n throw new Error('Array must contain unique items');\n }\n seen.add(key);\n }\n }\n \n return result;\n }\n \n /**\n * Safely parse and validate an array value\n */\n safeParse(value: unknown): ParseResult<T[]> {\n try {\n return { success: true, data: this.parse(value) };\n } catch (error) {\n return { success: false, error: error as Error };\n }\n }\n \n /**\n * Create a new validator with minimum length constraint\n */\n min(length: number, message?: string): VldArray<T> {\n return new VldArray({\n ...this.config,\n minLength: length,\n errorMessage: message || getMessages().arrayMin(length)\n });\n }\n \n /**\n * Create a new validator with maximum length constraint\n */\n max(length: number, message?: string): VldArray<T> {\n return new VldArray({\n ...this.config,\n maxLength: length,\n errorMessage: message || getMessages().arrayMax(length)\n });\n }\n \n /**\n * Create a new validator with exact length constraint\n */\n length(length: number, message?: string): VldArray<T> {\n return new VldArray({\n ...this.config,\n exactLength: length,\n minLength: undefined,\n maxLength: undefined,\n errorMessage: message || getMessages().arrayLength(length)\n });\n }\n \n /**\n * Create a new validator that ensures array is not empty\n */\n nonempty(message?: string): VldArray<T> {\n return new VldArray({\n ...this.config,\n minLength: 1,\n errorMessage: message || getMessages().arrayEmpty\n });\n }\n \n /**\n * Create a new validator that ensures array contains unique items\n */\n unique(message?: string): VldArray<T> {\n return new VldArray({\n ...this.config,\n unique: true,\n errorMessage: message || 'Array must contain unique items'\n });\n }\n \n /**\n * Create a new validator with a range constraint for length\n */\n between(min: number, max: number, message?: string): VldArray<T> {\n return new VldArray({\n ...this.config,\n minLength: min,\n maxLength: max,\n errorMessage: message || `Array length must be between ${min} and ${max}`\n });\n }\n}"]}
|
|
1
|
+
{"version":3,"file":"array.js","sourceRoot":"","sources":["../../src/validators/array.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAe,MAAM,QAAQ,CAAC;AAC9C,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAczC;;GAEG;AACH,MAAM,OAAO,QAAY,SAAQ,OAAuB;IAGtD;;OAEG;IACH,YAAoB,MAA+B;QACjD,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAED;;OAEG;IACH,MAAM,CAAC,MAAM,CAAI,aAAkC;QACjD,OAAO,IAAI,QAAQ,CAAC,EAAE,aAAa,EAAE,CAAC,CAAC;IACzC,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,KAAc;QAClB,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;YAC1B,MAAM,IAAI,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,YAAY,IAAI,WAAW,EAAE,CAAC,YAAY,CAAC,CAAC;QAC1E,CAAC;QAED,8BAA8B;QAC9B,IAAI,IAAI,CAAC,MAAM,CAAC,WAAW,KAAK,SAAS,IAAI,KAAK,CAAC,MAAM,KAAK,IAAI,CAAC,MAAM,CAAC,WAAW,EAAE,CAAC;YACtF,MAAM,IAAI,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,YAAY,IAAI,WAAW,EAAE,CAAC,WAAW,CAAC,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,CAAC;QAClG,CAAC;QAED,IAAI,IAAI,CAAC,MAAM,CAAC,SAAS,KAAK,SAAS,IAAI,KAAK,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,CAAC;YAChF,MAAM,IAAI,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,YAAY,IAAI,WAAW,EAAE,CAAC,QAAQ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AAM,CAAC,OAAgB;QACrB,OAAO,IAAI,QAAQ,CAAC;YAClB,GAAG,IAAI,CAAC,MAAM;YACd,MAAM,EAAE,IAAI;YACZ,YAAY,EAAE,OAAO,IAAI,iCAAiC;SAC3D,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACH,OAAO,CAAC,GAAW,EAAE,GAAW,EAAE,OAAgB;QAChD,OAAO,IAAI,QAAQ,CAAC;YAClB,GAAG,IAAI,CAAC,MAAM;YACd,SAAS,EAAE,GAAG;YACd,SAAS,EAAE,GAAG;YACd,YAAY,EAAE,OAAO,IAAI,gCAAgC,GAAG,QAAQ,GAAG,EAAE;SAC1E,CAAC,CAAC;IACL,CAAC;CACF","sourcesContent":["import { VldBase, ParseResult } from './base';\r\nimport { getMessages } from '../locales';\r\n\r\n/**\r\n * Configuration for array validator\r\n */\r\ninterface ArrayValidatorConfig<T> {\r\n readonly itemValidator: VldBase<unknown, T>;\r\n readonly minLength?: number;\r\n readonly maxLength?: number;\r\n readonly exactLength?: number;\r\n readonly unique?: boolean;\r\n readonly errorMessage?: string;\r\n}\r\n\r\n/**\r\n * Immutable array validator with chainable methods\r\n */\r\nexport class VldArray<T> extends VldBase<unknown[], T[]> {\r\n private readonly config: ArrayValidatorConfig<T>;\r\n \r\n /**\r\n * Private constructor to enforce immutability\r\n */\r\n private constructor(config: ArrayValidatorConfig<T>) {\r\n super();\r\n this.config = config;\r\n }\r\n \r\n /**\r\n * Create a new array validator\r\n */\r\n static create<T>(itemValidator: VldBase<unknown, T>): VldArray<T> {\r\n return new VldArray({ itemValidator });\r\n }\r\n \r\n /**\r\n * Parse and validate an array value\r\n */\r\n parse(value: unknown): T[] {\r\n if (!Array.isArray(value)) {\r\n throw new Error(this.config.errorMessage || getMessages().invalidArray);\r\n }\r\n \r\n // Validate length constraints\r\n if (this.config.exactLength !== undefined && value.length !== this.config.exactLength) {\r\n throw new Error(this.config.errorMessage || getMessages().arrayLength(this.config.exactLength));\r\n }\r\n \r\n if (this.config.minLength !== undefined && value.length < this.config.minLength) {\r\n throw new Error(this.config.errorMessage || getMessages().arrayMin(this.config.minLength));\r\n }\r\n \r\n if (this.config.maxLength !== undefined && value.length > this.config.maxLength) {\r\n throw new Error(this.config.errorMessage || getMessages().arrayMax(this.config.maxLength));\r\n }\r\n \r\n // Validate each item with optimized loop and safeParse\r\n const result: T[] = [];\r\n const length = value.length;\r\n \r\n for (let i = 0; i < length; i++) {\r\n const parseResult = this.config.itemValidator.safeParse(value[i]);\r\n if (!parseResult.success) {\r\n throw new Error(getMessages().arrayItem(i, parseResult.error.message));\r\n }\r\n result[i] = parseResult.data; // Direct assignment is faster than push\r\n }\r\n \r\n // Check uniqueness if required\r\n if (this.config.unique) {\r\n const seen = new Set<any>();\r\n for (const item of result) {\r\n const key = typeof item === 'object' ? this.stableStringify(item) : item;\r\n if (seen.has(key)) {\r\n throw new Error('Array must contain unique items');\r\n }\r\n seen.add(key);\r\n }\r\n }\r\n \r\n return result;\r\n }\r\n \r\n /**\r\n * Safely parse and validate an array value\r\n */\r\n safeParse(value: unknown): ParseResult<T[]> {\r\n try {\r\n return { success: true, data: this.parse(value) };\r\n } catch (error) {\r\n return { success: false, error: error as Error };\r\n }\r\n }\r\n\r\n /**\r\n * Create a stable string representation of an object for hashing\r\n * Handles circular references gracefully\r\n */\r\n private stableStringify(obj: any): string {\r\n const seen = new WeakSet();\r\n\r\n const replacer = (_key: string, value: any): any => {\r\n if (typeof value === 'object' && value !== null) {\r\n if (seen.has(value)) {\r\n return '[Circular]';\r\n }\r\n seen.add(value);\r\n }\r\n return value;\r\n };\r\n\r\n try {\r\n const allKeys: string[] = [];\r\n JSON.stringify(obj, (key, value) => {\r\n allKeys.push(key);\r\n return replacer(key, value);\r\n });\r\n allKeys.sort();\r\n return JSON.stringify(obj, (_key, value) => replacer(_key, value));\r\n } catch (error) {\r\n // Fallback to safe representation if stringify fails\r\n return String(obj);\r\n }\r\n }\r\n \r\n /**\r\n * Create a new validator with minimum length constraint\r\n */\r\n min(length: number, message?: string): VldArray<T> {\r\n return new VldArray({\r\n ...this.config,\r\n minLength: length,\r\n errorMessage: message || getMessages().arrayMin(length)\r\n });\r\n }\r\n \r\n /**\r\n * Create a new validator with maximum length constraint\r\n */\r\n max(length: number, message?: string): VldArray<T> {\r\n return new VldArray({\r\n ...this.config,\r\n maxLength: length,\r\n errorMessage: message || getMessages().arrayMax(length)\r\n });\r\n }\r\n \r\n /**\r\n * Create a new validator with exact length constraint\r\n */\r\n length(length: number, message?: string): VldArray<T> {\r\n return new VldArray({\r\n ...this.config,\r\n exactLength: length,\r\n minLength: undefined,\r\n maxLength: undefined,\r\n errorMessage: message || getMessages().arrayLength(length)\r\n });\r\n }\r\n \r\n /**\r\n * Create a new validator that ensures array is not empty\r\n */\r\n nonempty(message?: string): VldArray<T> {\r\n return new VldArray({\r\n ...this.config,\r\n minLength: 1,\r\n errorMessage: message || getMessages().arrayEmpty\r\n });\r\n }\r\n \r\n /**\r\n * Create a new validator that ensures array contains unique items\r\n */\r\n unique(message?: string): VldArray<T> {\r\n return new VldArray({\r\n ...this.config,\r\n unique: true,\r\n errorMessage: message || 'Array must contain unique items'\r\n });\r\n }\r\n \r\n /**\r\n * Create a new validator with a range constraint for length\r\n */\r\n between(min: number, max: number, message?: string): VldArray<T> {\r\n return new VldArray({\r\n ...this.config,\r\n minLength: min,\r\n maxLength: max,\r\n errorMessage: message || `Array length must be between ${min} and ${max}`\r\n });\r\n }\r\n}"]}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { VldBase, ParseResult } from './base';
|
|
2
|
+
/**
|
|
3
|
+
* Base64 string validator
|
|
4
|
+
*/
|
|
5
|
+
export declare class VldBase64 extends VldBase<string, string> {
|
|
6
|
+
private static readonly BASE64_REGEX;
|
|
7
|
+
private static readonly BASE64_URL_REGEX;
|
|
8
|
+
private readonly urlSafe;
|
|
9
|
+
constructor(urlSafe?: boolean);
|
|
10
|
+
parse(value: unknown): string;
|
|
11
|
+
safeParse(value: unknown): ParseResult<string>;
|
|
12
|
+
/**
|
|
13
|
+
* Create a URL-safe base64 validator
|
|
14
|
+
*/
|
|
15
|
+
urlSafeMode(): VldBase64;
|
|
16
|
+
static create(urlSafe?: boolean): VldBase64;
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=base64.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base64.d.ts","sourceRoot":"","sources":["../../src/validators/base64.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAC;AAI9C;;GAEG;AACH,qBAAa,SAAU,SAAQ,OAAO,CAAC,MAAM,EAAE,MAAM,CAAC;IACpD,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,YAAY,CAA4B;IAEhE,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,gBAAgB,CAA4B;IAEpE,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAU;gBAEtB,OAAO,UAAQ;IAK3B,KAAK,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM;IAQ7B,SAAS,CAAC,KAAK,EAAE,OAAO,GAAG,WAAW,CAAC,MAAM,CAAC;IAsC9C;;OAEG;IACH,WAAW,IAAI,SAAS;IAIxB,MAAM,CAAC,MAAM,CAAC,OAAO,UAAQ,GAAG,SAAS;CAG1C"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { VldBase } from './base.js';
|
|
2
|
+
import { VldError, createIssue } from '../errors.js';
|
|
3
|
+
import { getMessages } from '../locales.js';
|
|
4
|
+
/**
|
|
5
|
+
* Base64 string validator
|
|
6
|
+
*/
|
|
7
|
+
export class VldBase64 extends VldBase {
|
|
8
|
+
constructor(urlSafe = false) {
|
|
9
|
+
super();
|
|
10
|
+
this.urlSafe = urlSafe;
|
|
11
|
+
}
|
|
12
|
+
parse(value) {
|
|
13
|
+
const result = this.safeParse(value);
|
|
14
|
+
if (!result.success) {
|
|
15
|
+
throw result.error;
|
|
16
|
+
}
|
|
17
|
+
return result.data;
|
|
18
|
+
}
|
|
19
|
+
safeParse(value) {
|
|
20
|
+
if (typeof value !== 'string') {
|
|
21
|
+
return {
|
|
22
|
+
success: false,
|
|
23
|
+
error: new VldError([
|
|
24
|
+
createIssue('invalid_type', [], getMessages().expectedString)
|
|
25
|
+
])
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
// Check if it's a valid base64 string
|
|
29
|
+
const regex = this.urlSafe ? VldBase64.BASE64_URL_REGEX : VldBase64.BASE64_REGEX;
|
|
30
|
+
// Check basic format
|
|
31
|
+
if (!regex.test(value)) {
|
|
32
|
+
return {
|
|
33
|
+
success: false,
|
|
34
|
+
error: new VldError([
|
|
35
|
+
createIssue('custom', [], getMessages().invalidBase64)
|
|
36
|
+
])
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
// For standard base64, check length is multiple of 4 (after padding)
|
|
40
|
+
// Empty strings are valid (decode to empty buffer)
|
|
41
|
+
// For URL-safe base64, padding is not required
|
|
42
|
+
if (!this.urlSafe && value.length > 0 && value.length % 4 !== 0) {
|
|
43
|
+
return {
|
|
44
|
+
success: false,
|
|
45
|
+
error: new VldError([
|
|
46
|
+
createIssue('custom', [], getMessages().invalidBase64)
|
|
47
|
+
])
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
return { success: true, data: value };
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Create a URL-safe base64 validator
|
|
54
|
+
*/
|
|
55
|
+
urlSafeMode() {
|
|
56
|
+
return new VldBase64(true);
|
|
57
|
+
}
|
|
58
|
+
static create(urlSafe = false) {
|
|
59
|
+
return new VldBase64(urlSafe);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
VldBase64.BASE64_REGEX = /^[A-Za-z0-9+/]*={0,2}$/;
|
|
63
|
+
// Note: RFC 4648 specifies URL-safe base64 should omit padding, but we allow it for compatibility
|
|
64
|
+
VldBase64.BASE64_URL_REGEX = /^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AAK,EAAE,IAAI,QAAQ,CAAC;oBAClB,WAAW,CAAC,QAAQ,EAAE,EAAE,EAAE,WAAW,EAAE,CAAC,aAAa,CAAC;iBACvD,CAAC;aACH,CAAC;QACJ,CAAC;QAED,qEAAqE;QACrE,mDAAmD;QACnD,+CAA+C;QAC/C,IAAI,CAAC,IAAI,CAAC,OAAO,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC;YAChE,OAAO;gBACL,OAAO,EAAE,KAAK;gBACd,KAAK,EAAE,IAAI,QAAQ,CAAC;oBAClB,WAAW,CAAC,QAAQ,EAAE,EAAE,EAAE,WAAW,EAAE,CAAC,aAAa,CAAC;iBACvD,CAAC;aACH,CAAC;QACJ,CAAC;QAED,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC;IACxC,CAAC;IAED;;OAEG;IACH,WAAW;QACT,OAAO,IAAI,SAAS,CAAC,IAAI,CAAC,CAAC;IAC7B,CAAC;IAED,MAAM,CAAC,MAAM,CAAC,OAAO,GAAG,KAAK;QAC3B,OAAO,IAAI,SAAS,CAAC,OAAO,CAAC,CAAC;IAChC,CAAC;;AAlEuB,sBAAY,GAAG,wBAAwB,CAAC;AAChE,kGAAkG;AAC1E,0BAAgB,GAAG,wBAAwB,CAAC","sourcesContent":["import { VldBase, ParseResult } from './base';\r\nimport { VldError, createIssue } from '../errors';\r\nimport { getMessages } from '../locales';\r\n\r\n/**\r\n * Base64 string validator\r\n */\r\nexport class VldBase64 extends VldBase<string, string> {\r\n private static readonly BASE64_REGEX = /^[A-Za-z0-9+/]*={0,2}$/;\r\n // Note: RFC 4648 specifies URL-safe base64 should omit padding, but we allow it for compatibility\r\n private static readonly BASE64_URL_REGEX = /^[A-Za-z0-9_-]*={0,2}$/;\r\n \r\n private readonly urlSafe: boolean;\r\n \r\n constructor(urlSafe = false) {\r\n super();\r\n this.urlSafe = urlSafe;\r\n }\r\n \r\n parse(value: unknown): string {\r\n const result = this.safeParse(value);\r\n if (!result.success) {\r\n throw result.error;\r\n }\r\n return result.data;\r\n }\r\n \r\n safeParse(value: unknown): ParseResult<string> {\r\n if (typeof value !== 'string') {\r\n return {\r\n success: false,\r\n error: new VldError([\r\n createIssue('invalid_type', [], getMessages().expectedString)\r\n ])\r\n };\r\n }\r\n \r\n // Check if it's a valid base64 string\r\n const regex = this.urlSafe ? VldBase64.BASE64_URL_REGEX : VldBase64.BASE64_REGEX;\r\n\r\n // Check basic format\r\n if (!regex.test(value)) {\r\n return {\r\n success: false,\r\n error: new VldError([\r\n createIssue('custom', [], getMessages().invalidBase64)\r\n ])\r\n };\r\n }\r\n\r\n // For standard base64, check length is multiple of 4 (after padding)\r\n // Empty strings are valid (decode to empty buffer)\r\n // For URL-safe base64, padding is not required\r\n if (!this.urlSafe && value.length > 0 && value.length % 4 !== 0) {\r\n return {\r\n success: false,\r\n error: new VldError([\r\n createIssue('custom', [], getMessages().invalidBase64)\r\n ])\r\n };\r\n }\r\n \r\n return { success: true, data: value };\r\n }\r\n \r\n /**\r\n * Create a URL-safe base64 validator\r\n */\r\n urlSafeMode(): VldBase64 {\r\n return new VldBase64(true);\r\n }\r\n \r\n static create(urlSafe = false): VldBase64 {\r\n return new VldBase64(urlSafe);\r\n }\r\n}"]}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { VldBase, ParseResult } from './base';
|
|
2
|
+
/**
|
|
3
|
+
* Codec transform functions for bidirectional transformations
|
|
4
|
+
*/
|
|
5
|
+
export interface CodecTransform<TInput, TOutput> {
|
|
6
|
+
decode: (value: TInput) => TOutput | Promise<TOutput>;
|
|
7
|
+
encode: (value: TOutput) => TInput | Promise<TInput>;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Codec validator for bidirectional transformations
|
|
11
|
+
* Enables converting between different data representations
|
|
12
|
+
*/
|
|
13
|
+
export declare class VldCodec<TInput, TOutput> extends VldBase<TInput, TOutput> {
|
|
14
|
+
private readonly inputValidator;
|
|
15
|
+
private readonly outputValidator;
|
|
16
|
+
private readonly codecTransform;
|
|
17
|
+
constructor(inputValidator: VldBase<unknown, TInput>, outputValidator: VldBase<unknown, TOutput>, codecTransform: CodecTransform<TInput, TOutput>);
|
|
18
|
+
/**
|
|
19
|
+
* Parse (decode) a value from input to output format
|
|
20
|
+
*/
|
|
21
|
+
parse(value: unknown): TOutput;
|
|
22
|
+
/**
|
|
23
|
+
* Safely parse (decode) a value from input to output format
|
|
24
|
+
*/
|
|
25
|
+
safeParse(value: unknown): ParseResult<TOutput>;
|
|
26
|
+
/**
|
|
27
|
+
* Encode a value from output back to input format
|
|
28
|
+
*/
|
|
29
|
+
encode(value: unknown): TInput;
|
|
30
|
+
/**
|
|
31
|
+
* Safely encode a value from output back to input format
|
|
32
|
+
*/
|
|
33
|
+
safeEncode(value: unknown): ParseResult<TInput>;
|
|
34
|
+
/**
|
|
35
|
+
* Async version of parse (decode)
|
|
36
|
+
*/
|
|
37
|
+
parseAsync(value: unknown): Promise<TOutput>;
|
|
38
|
+
/**
|
|
39
|
+
* Async version of safeParse (decode)
|
|
40
|
+
*/
|
|
41
|
+
safeParseAsync(value: unknown): Promise<ParseResult<TOutput>>;
|
|
42
|
+
/**
|
|
43
|
+
* Async version of encode
|
|
44
|
+
*/
|
|
45
|
+
encodeAsync(value: unknown): Promise<TInput>;
|
|
46
|
+
/**
|
|
47
|
+
* Async version of safeEncode
|
|
48
|
+
*/
|
|
49
|
+
safeEncodeAsync(value: unknown): Promise<ParseResult<TInput>>;
|
|
50
|
+
/**
|
|
51
|
+
* Create a codec validator
|
|
52
|
+
*/
|
|
53
|
+
static create<TInput, TOutput>(inputValidator: VldBase<unknown, TInput>, outputValidator: VldBase<unknown, TOutput>, codecTransform: CodecTransform<TInput, TOutput>): VldCodec<TInput, TOutput>;
|
|
54
|
+
}
|
|
55
|
+
//# sourceMappingURL=codec.d.ts.map
|
|
@@ -0,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