@nahkies/typescript-express-runtime
Version:
Runtime package for code generated by @nahkies/openapi-code-generator using the typescript-express template
47 lines • 1.64 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseRequestInput = parseRequestInput;
exports.responseValidationFactory = responseValidationFactory;
const errors_1 = require("./errors");
function parseRequestInput(schema, input, type) {
try {
if (!schema) {
return undefined;
}
const result = schema.validate(input, { stripUnknown: true });
if (result.error) {
throw result.error;
}
return result.value;
}
catch (err) {
throw errors_1.ExpressRuntimeError.RequestError(err, type);
}
}
function responseValidationFactory(possibleResponses, defaultResponse) {
// Exploit the natural ordering matching the desired specificity of eg: 404 vs 4xx
possibleResponses.sort((x, y) => (x[0] < y[0] ? -1 : 1));
return (status, value) => {
for (const [match, schema] of possibleResponses) {
const isMatch = (/^\d+$/.test(match) && String(status) === match) ||
(/^\d[xX]{2}$/.test(match) && String(status)[0] === match[0]);
if (isMatch) {
const result = schema.validate(value);
if (result.error) {
throw result.error;
}
return result.value;
}
}
// TODO: wrap thrown error.
if (defaultResponse) {
const result = defaultResponse.validate(value);
if (result.error) {
throw result.error;
}
return result.value;
}
return value;
};
}
//# sourceMappingURL=joi.js.map
;