@zudojs/config 1.0.0 → 1.0.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.
|
@@ -124,8 +124,26 @@ function appendCustomValidationResult(result, path, issues) {
|
|
|
124
124
|
}
|
|
125
125
|
issues.push(result);
|
|
126
126
|
}
|
|
127
|
+
/**
|
|
128
|
+
* Compiles a string schema pattern for a one-shot `test`.
|
|
129
|
+
*
|
|
130
|
+
* A RegExp carrying the `g` or `y` flag keeps `lastIndex` between calls, so
|
|
131
|
+
* the same schema object alternated between accepting and rejecting an
|
|
132
|
+
* identical value on successive validations. Both flags are dropped; they
|
|
133
|
+
* have no meaning for a whole-value match.
|
|
134
|
+
*/
|
|
135
|
+
function compilePattern(pattern) {
|
|
136
|
+
if (typeof pattern === "string") {
|
|
137
|
+
return new RegExp(pattern);
|
|
138
|
+
}
|
|
139
|
+
const flags = pattern.flags.replace(/[gy]/g, "");
|
|
140
|
+
return flags === pattern.flags ? pattern : new RegExp(pattern.source, flags);
|
|
141
|
+
}
|
|
127
142
|
/**
|
|
128
143
|
* Built-in schema validation rules.
|
|
144
|
+
*
|
|
145
|
+
* Returns a replacement value when a rule rewrote it (an array whose item
|
|
146
|
+
* schema applied `transform` or `default`), otherwise undefined.
|
|
129
147
|
*/
|
|
130
148
|
function validateBuiltInRules(value, schema, context, issues) {
|
|
131
149
|
if (typeof value === "string") {
|
|
@@ -139,9 +157,7 @@ function validateBuiltInRules(value, schema, context, issues) {
|
|
|
139
157
|
issues.push(createConfigValidationIssue(context.path, `Value must contain at most ${stringSchema.maxLength} characters.`, "MAX_LENGTH"));
|
|
140
158
|
}
|
|
141
159
|
if (stringSchema.pattern) {
|
|
142
|
-
const pattern =
|
|
143
|
-
? new RegExp(stringSchema.pattern)
|
|
144
|
-
: stringSchema.pattern;
|
|
160
|
+
const pattern = compilePattern(stringSchema.pattern);
|
|
145
161
|
if (!pattern.test(value)) {
|
|
146
162
|
issues.push(createConfigValidationIssue(context.path, "Value does not match the required pattern.", "PATTERN"));
|
|
147
163
|
}
|
|
@@ -176,6 +192,11 @@ function validateBuiltInRules(value, schema, context, issues) {
|
|
|
176
192
|
issues.push(createConfigValidationIssue(context.path, `Array must contain at most ${arraySchema.maxItems} items.`, "MAX_ITEMS"));
|
|
177
193
|
}
|
|
178
194
|
if (arraySchema.items) {
|
|
195
|
+
// Item results used to be consulted for issues only, so an item
|
|
196
|
+
// schema's `transform` (or `default`) ran and its output was thrown
|
|
197
|
+
// away: the caller got the untransformed array back as "valid".
|
|
198
|
+
const rebuilt = [];
|
|
199
|
+
let allValid = true;
|
|
179
200
|
value.forEach((item, index) => {
|
|
180
201
|
const result = validateConfigValue(item, arraySchema.items, {
|
|
181
202
|
path: `${context.path}[${index}]`,
|
|
@@ -184,9 +205,15 @@ function validateBuiltInRules(value, schema, context, issues) {
|
|
|
184
205
|
key: index,
|
|
185
206
|
});
|
|
186
207
|
issues.push(...result.issues);
|
|
208
|
+
if (!result.valid) {
|
|
209
|
+
allValid = false;
|
|
210
|
+
}
|
|
211
|
+
rebuilt.push(result.value === undefined ? item : result.value);
|
|
187
212
|
});
|
|
213
|
+
return allValid ? rebuilt : undefined;
|
|
188
214
|
}
|
|
189
215
|
}
|
|
216
|
+
return undefined;
|
|
190
217
|
}
|
|
191
218
|
/**
|
|
192
219
|
* Validates a value against a schema.
|
|
@@ -239,7 +266,7 @@ export function validateConfigValue(value, schema, context) {
|
|
|
239
266
|
issues,
|
|
240
267
|
};
|
|
241
268
|
}
|
|
242
|
-
validateBuiltInRules(value, schema, validationContext, issues);
|
|
269
|
+
const rewritten = validateBuiltInRules(value, schema, validationContext, issues);
|
|
243
270
|
// An object schema carries `properties` / `additionalProperties`.
|
|
244
271
|
// Those were declared on ConfigObjectSchema but read nowhere in this
|
|
245
272
|
// function, so every nested object schema — including the ones used
|
|
@@ -247,7 +274,7 @@ export function validateConfigValue(value, schema, context) {
|
|
|
247
274
|
// validation unconditionally. Delegate to validateConfigObject so
|
|
248
275
|
// nested constraints are actually enforced.
|
|
249
276
|
const objectSchema = schema;
|
|
250
|
-
let base = value;
|
|
277
|
+
let base = rewritten ?? value;
|
|
251
278
|
if (matchesConfigType(value, ConfigValueType.OBJECT) &&
|
|
252
279
|
(objectSchema.properties !== undefined ||
|
|
253
280
|
objectSchema.additionalProperties !== undefined)) {
|
package/package.json
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zudojs/config",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Layered configuration management with multiple sources, validation, and environment-specific overrides.",
|
|
5
5
|
"license": "MIT",
|
|
6
|
+
"author": {
|
|
7
|
+
"name": "Oluwayemi Oyinlola",
|
|
8
|
+
"url": "https://github.com/oyinlola-tech"
|
|
9
|
+
},
|
|
6
10
|
"type": "module",
|
|
7
11
|
"main": "./dist/index.js",
|
|
8
12
|
"module": "./dist/index.js",
|
|
@@ -24,7 +28,7 @@
|
|
|
24
28
|
"node": ">=24.0.0"
|
|
25
29
|
},
|
|
26
30
|
"dependencies": {
|
|
27
|
-
"@zudojs/errors": "1.0.
|
|
31
|
+
"@zudojs/errors": "1.0.1"
|
|
28
32
|
},
|
|
29
33
|
"devDependencies": {
|
|
30
34
|
"typescript": "7.0.2",
|