in lib/liveValidation/liveValidator.ts [188:322]
public async initialize(): Promise<void> {
const startTime = Date.now();
// Clone github repository if required
if (this.options.git.shouldClone && this.options.git.url) {
const cloneStartTime = Date.now();
utils.gitClone(this.options.directory, this.options.git.url, this.options.git.branch);
this.logging(
`Clone spec repository ${this.options.git.url}, branch:${this.options.git.branch} in livevalidator.initialize`
);
this.logging(
`Clone spec repository ${this.options.git.url}, branch:${this.options.git.branch}`,
LiveValidatorLoggingLevels.info,
LiveValidatorLoggingTypes.perfTrace,
"Oav.liveValidator.initialize.gitclone",
Date.now() - cloneStartTime
);
}
// Construct array of swagger paths to be used for building a cache
this.logging("Get swagger path.");
const swaggerPaths = await this.getSwaggerPaths();
const container = inversifyGetContainer();
this.loader = inversifyGetInstance(LiveValidatorLoader, {
container,
fileRoot: this.options.directory,
...this.options,
loadSuppression: this.options.loadSuppression ?? Object.keys(apiValidationErrors),
});
this.loader.logging = this.logging;
// re-set the transform context after set the logging function
this.loader.setTransformContext();
const schemaValidator = container.get(TYPES.schemaValidator) as SchemaValidator;
this.validateRequestResponsePair = await schemaValidator.compileAsync(
requestResponseDefinition
);
const allSpecs: SwaggerSpec[] = [];
while (swaggerPaths.length > 0) {
const swaggerPath = swaggerPaths.shift()!;
this.swaggerList.push(swaggerPath);
const spec = await this.getSwaggerInitializer(this.loader!, swaggerPath);
if (spec !== undefined) {
allSpecs.push(spec);
}
}
this.logging("Apply global transforms for all specs");
try {
this.loader.transformLoadedSpecs();
} catch (e) {
// keeps building validator if it fails to tranform specs coz global transformers catches the exceptions and continue other schema transformings;
// this error will be reported in validator building or validation runtime.
const errMsg = `Failed to transform loaded specs, detail error message:${
(e as any)?.message
}.\nError stack:${(e as any)?.stack}`;
this.logging(
errMsg,
LiveValidatorLoggingLevels.error,
LiveValidatorLoggingTypes.specTrace,
"Oav.liveValidator.initialize.transformLoadedSpecs"
);
}
if (this.options.loadValidatorInInitialize) {
this.logging("Building validator in initialization time...");
let spec;
while (allSpecs.length > 0) {
try {
spec = allSpecs.shift()!;
const loadStart = Date.now();
await this.loader.buildAjvValidator(spec);
const durationInMs = Date.now() - loadStart;
this.logging(
`Complete building validator for ${spec._filePath} in initialization time`,
LiveValidatorLoggingLevels.info,
LiveValidatorLoggingTypes.perfTrace,
"Oav.liveValidator.initialize.loader.buildAjvValidator",
durationInMs
);
this.logging(
`Complete building validator for spec ${spec._filePath}`,
LiveValidatorLoggingLevels.info,
LiveValidatorLoggingTypes.specTrace,
"Oav.liveValidator.initialize.loader.buildAjvValidator",
durationInMs,
{
providerNamespace: spec._providerNamespace ?? "unknown",
apiVersion: spec.info.version,
specName: spec._filePath,
}
);
} catch (e) {
this.logging(
`Failed to build validator for spec ${spec?._filePath}.\nErrorMessage:${
(e as any)?.message
}.\nErrorStack:${(e as any)?.stack}`,
LiveValidatorLoggingLevels.error,
LiveValidatorLoggingTypes.specTrace,
"Oav.liveValidator.initialize.loader.buildAjvValidator",
undefined,
{
providerNamespace: spec?._providerNamespace ?? "unknown",
apiVersion: spec?.info.version ?? "unknown",
specName: spec?._filePath,
}
);
}
}
this.loader = undefined;
}
this.logging("Cache initialization complete.");
const elapsedTime = Date.now() - startTime;
this.logging(
`Cache complete initialization with DurationInMs:${elapsedTime}`,
LiveValidatorLoggingLevels.info,
LiveValidatorLoggingTypes.trace,
"Oav.liveValidator.initialize"
);
this.logging(
`Cache complete initialization`,
LiveValidatorLoggingLevels.info,
LiveValidatorLoggingTypes.perfTrace,
"Oav.liveValidator.initialize",
elapsedTime
);
if (this.options.loadValidatorInBackground) {
this.logging("Building validator in background...");
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this.loadAllSpecValidatorInBackground(allSpecs);
}
}