@cofondateurauchomage/libs 1.1.163 → 1.1.164
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/build/api.validate.js +40 -10
- package/package.json +1 -1
package/build/api.validate.js
CHANGED
|
@@ -256,6 +256,24 @@ const schemasForAllRoutes = {
|
|
|
256
256
|
}),
|
|
257
257
|
webhook: zod_1.z.object({}),
|
|
258
258
|
};
|
|
259
|
+
/**
|
|
260
|
+
* Keys that accept `undefined` on the route schema (`.optional()` or `.partial()`).
|
|
261
|
+
* Invalid values for these keys are stripped instead of failing the whole request.
|
|
262
|
+
*/
|
|
263
|
+
function getLenientOptionalKeys(schema) {
|
|
264
|
+
const keys = new Set();
|
|
265
|
+
for (const [key, fieldSchema] of Object.entries(schema.shape)) {
|
|
266
|
+
const field = fieldSchema;
|
|
267
|
+
if (field.safeParse(undefined).success) {
|
|
268
|
+
keys.add(key);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
return keys;
|
|
272
|
+
}
|
|
273
|
+
const lenientKeysByRoute = Object.fromEntries(Object.keys(schemasForAllRoutes).map((route) => [
|
|
274
|
+
route,
|
|
275
|
+
getLenientOptionalKeys(schemasForAllRoutes[route]),
|
|
276
|
+
]));
|
|
259
277
|
/**
|
|
260
278
|
* Validate the request body depending on the route.
|
|
261
279
|
* @param route - Route to know which validators to use.
|
|
@@ -267,19 +285,31 @@ function validateBodyForO(route, body) {
|
|
|
267
285
|
throw new ValidationError(400, "Le payload est requis");
|
|
268
286
|
}
|
|
269
287
|
const schema = schemasForAllRoutes[route];
|
|
270
|
-
const
|
|
271
|
-
if (!parsed.success) {
|
|
272
|
-
throwFromZodError(parsed.error);
|
|
273
|
-
}
|
|
288
|
+
const lenientKeys = lenientKeysByRoute[route];
|
|
274
289
|
const input = body;
|
|
275
|
-
const
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
if (
|
|
279
|
-
|
|
290
|
+
const candidate = { ...input };
|
|
291
|
+
while (true) {
|
|
292
|
+
const parsed = schema.safeParse(candidate);
|
|
293
|
+
if (parsed.success) {
|
|
294
|
+
const data = parsed.data;
|
|
295
|
+
const result = {};
|
|
296
|
+
for (const key of Object.keys(data)) {
|
|
297
|
+
if (input[key] !== undefined) {
|
|
298
|
+
result[key] = data[key];
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
return result;
|
|
280
302
|
}
|
|
303
|
+
const issue = parsed.error.issues[0];
|
|
304
|
+
const key = issue.path.find((p) => typeof p === "string");
|
|
305
|
+
if (key !== undefined &&
|
|
306
|
+
lenientKeys.has(key) &&
|
|
307
|
+
Object.prototype.hasOwnProperty.call(candidate, key)) {
|
|
308
|
+
delete candidate[key];
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
throwFromZodError(parsed.error);
|
|
281
312
|
}
|
|
282
|
-
return result;
|
|
283
313
|
}
|
|
284
314
|
/**
|
|
285
315
|
* Custom error class to handle validation errors.
|