turtle-express
Version:
`turtle-express` is kinda a framework or a library based on `express.js` with an opinionated express router with type safety and schema validation with zod. Also many [more features](https://github.com/mm-ninja-turtles/turtle-express/discussions/7) planni
32 lines • 1.18 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.requestValidation = void 0;
function requestValidation(req, schemas) {
const context = {
params: null,
query: null,
body: null,
};
if (schemas.params) {
const params = req.params;
const paramsSchema = schemas.params;
context.params = paramsSchema.safeParse(params);
}
if (schemas.query) {
const query = req.query;
const querySchema = schemas.query;
context.query = querySchema.safeParse(query);
}
if (schemas.body) {
const body = req.body;
const bodySchema = schemas.body;
context.body = bodySchema.safeParse(body);
}
const ifSomeSchemasExists = () => [schemas.params, schemas.query, schemas.body].some((schema) => schema !== undefined);
const ifOneValidationFailed = () => [context.params, context.query, context.body].some((validate) => {
return validate?.success === false;
});
return { context, ifSomeSchemasExists, ifOneValidationFailed };
}
exports.requestValidation = requestValidation;
//# sourceMappingURL=request-validation.js.map