@message-in-the-middle/core
Version:
Framework-agnostic middleware pattern for message queue processing. Core package with all middlewares.
26 lines • 906 B
JavaScript
import { ValidateInboundMiddleware } from '../middlewares/validation.middleware';
export function createZodValidator(schema, options = {}) {
const validator = async (data) => {
try {
let processedSchema = schema;
if (options.strip) {
processedSchema = processedSchema.strip();
}
if (options.allowUnknown) {
processedSchema = processedSchema.passthrough();
}
const result = await processedSchema.parseAsync(data);
return result;
}
catch (error) {
if (options.errorFormatter && error.errors) {
throw options.errorFormatter(error);
}
throw error;
}
};
return new ValidateInboundMiddleware(validator, {
throwOnError: !options.passthrough,
});
}
//# sourceMappingURL=zod.validator.js.map