@mojaloop/ml-schema-transformer-lib
Version:
Shared component for ML schemas translation
127 lines (123 loc) • 4.6 kB
JavaScript
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
}) : x)(function(x) {
if (typeof require !== "undefined") return require.apply(this, arguments);
throw Error('Dynamic require of "' + x + '" is not supported');
});
// src/lib/validation/index.ts
import path from "path";
import NodeCache from "node-cache";
// src/types/index.ts
var logLevelsMap = {
error: "error",
warn: "warn",
info: "info",
verbose: "verbose",
debug: "debug",
silly: "silly",
audit: "audit",
trace: "trace",
perf: "perf"
};
var logLevelValues = Object.values(logLevelsMap);
// src/lib/validation/validator.ts
import SwaggerParser from "@apidevtools/swagger-parser";
import { OpenAPIValidator } from "openapi-backend";
import stringify from "fast-safe-stringify";
var Validator = class {
apiDoc;
apiSpec;
apiValidator;
ajvOpts = { allErrors: true, coerceTypes: true, strict: false };
/**
* @param apiDoc - The path to the OpenAPI document or the document itself
*/
constructor(apiDoc) {
this.apiDoc = apiDoc;
this.apiSpec = {};
this.apiValidator = {};
}
async initialize() {
this.apiSpec = await SwaggerParser.validate(this.apiDoc);
this.apiValidator = new OpenAPIValidator({ definition: this.apiSpec, ajvOpts: this.ajvOpts });
}
validateRequest(request, returnErrors = false) {
const validation = this.apiValidator.validateRequest(request);
if (validation.errors && validation.errors.length > 0) {
if (returnErrors) return validation.errors;
throw new Error(`Validation errors: ${stringify(validation.errors)}`);
}
return true;
}
validateBody(partialRequest, returnErrors = false) {
const { body, path: path2, method } = partialRequest;
const validation = this.apiValidator.validateRequest({ path: path2, method, body, headers: {} });
if (validation.errors) {
const bodyErrors = validation.errors.filter((error) => error.instancePath === "/requestBody");
if (bodyErrors && bodyErrors.length > 0) {
if (returnErrors) return bodyErrors;
throw new Error(`Validation errors: ${stringify(bodyErrors)}`);
}
}
return true;
}
validateHeaders(partialRequest, returnErrors = false) {
const { headers, path: path2, method } = partialRequest;
const validation = this.apiValidator.validateRequest({ path: path2, method, headers, body: {} });
if (validation.errors) {
const headersErrors = validation.errors.filter((error) => error.instancePath === "/header");
if (headersErrors && headersErrors.length > 0) {
if (returnErrors) return headersErrors;
throw new Error(`Validation errors: ${stringify(headersErrors)}`);
}
}
return true;
}
};
// src/lib/validation/index.ts
var validatorsCache = new NodeCache();
var getApiSpecPath = (apiName, version) => {
let specFile;
switch (apiName) {
case "fspiop" /* FSPIOP */:
specFile = `../docs/fspiop-rest-v${version}-openapi3-snippets.yaml`;
break;
case "iso" /* ISO20022 */:
specFile = `../docs/fspiop-rest-v${version}-ISO20022-openapi3-snippets.yaml`;
break;
default:
throw new Error(`Invalid API name: ${apiName} in getApiSpecPath`);
}
const specPath = path.join(path.dirname(__require.resolve("@mojaloop/api-snippets")), specFile);
return specPath;
};
var validateBody = async (body, spec) => {
const apiSpecPath = getApiSpecPath(spec.name, spec.version);
let validator = validatorsCache.get(apiSpecPath);
if (!validator) {
validator = new Validator(apiSpecPath);
await validator.initialize();
validatorsCache.set(apiSpecPath, validator);
}
return validator.validateBody({ body, path: spec.path, method: spec.method });
};
var applyTargetValidation = async (params) => {
const { target, options, logger } = params;
if (!options.validateTarget) {
logger.debug("Skipping target validation", { target, options });
return target;
}
const targetSpec = options.applyTargetValidation?.targetSpec;
if (!targetSpec || !targetSpec.name || !targetSpec.path || !targetSpec.method) {
logger.error("Missing or invalid targetSpec in applyTargetValidation options", { target, targetSpec });
throw new Error("Missing or invalid targetSpec in applyTargetValidation options");
}
await validateBody(target.body, targetSpec);
return target;
};
export {
applyTargetValidation,
getApiSpecPath,
validateBody
};
//# sourceMappingURL=index.mjs.map