"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ManifestValidator = void 0;
const tslib_1 = require("tslib");
const ajv_formats_1 = tslib_1.__importDefault(require("ajv-formats"));
const _2020_1 = tslib_1.__importDefault(require("ajv/dist/2020"));
const json_schema_draft_07_json_1 = tslib_1.__importDefault(require("ajv/dist/refs/json-schema-draft-07.json"));
const errors_1 = require("../types/errors");
const lightdashMetadata_json_1 = tslib_1.__importDefault(require("./schemas/lightdashMetadata.json"));
const lightdashV7_json_1 = tslib_1.__importDefault(require("./schemas/lightdashV7.json"));
const lightdashV8_json_1 = tslib_1.__importDefault(require("./schemas/lightdashV8.json"));
const lightdashV9_json_1 = tslib_1.__importDefault(require("./schemas/lightdashV9.json"));
const lightdashV10_json_1 = tslib_1.__importDefault(require("./schemas/lightdashV10.json"));
const lightdashV11_json_1 = tslib_1.__importDefault(require("./schemas/lightdashV11.json"));
const lightdashV12_json_1 = tslib_1.__importDefault(require("./schemas/lightdashV12.json"));
const lightdashV20_json_1 = tslib_1.__importDefault(require("./schemas/lightdashV20.json"));
const manifestV7_json_1 = tslib_1.__importDefault(require("./schemas/manifestV7.json"));
const manifestV8_json_1 = tslib_1.__importDefault(require("./schemas/manifestV8.json"));
const manifestV9_json_1 = tslib_1.__importDefault(require("./schemas/manifestV9.json"));
const manifestV10_json_1 = tslib_1.__importDefault(require("./schemas/manifestV10.json"));
const manifestV11_json_1 = tslib_1.__importDefault(require("./schemas/manifestV11.json"));
const manifestV12_json_1 = tslib_1.__importDefault(require("./schemas/manifestV12.json"));
const manifestV20_json_1 = tslib_1.__importDefault(require("./schemas/manifestV20.json"));
const ajv = new _2020_1.default();
ajv.addMetaSchema(json_schema_draft_07_json_1.default); // add backward compatibility with draft-07
ajv.addSchema([
    manifestV7_json_1.default,
    manifestV8_json_1.default,
    manifestV9_json_1.default,
    manifestV10_json_1.default,
    manifestV11_json_1.default,
    manifestV12_json_1.default,
    manifestV20_json_1.default,
    lightdashMetadata_json_1.default,
    lightdashV7_json_1.default,
    lightdashV8_json_1.default,
    lightdashV9_json_1.default,
    lightdashV10_json_1.default,
    lightdashV11_json_1.default,
    lightdashV12_json_1.default,
    lightdashV20_json_1.default,
]);
(0, ajv_formats_1.default)(ajv);
class ManifestValidator {
    constructor(manifestVersion) {
        this.isModelValid = (model) => {
            // Seeds are only used as join targets and don't need full model
            // schema validation (which enforces resource_type: "model").
            // They just need valid Lightdash metadata and columns.
            if (model.resource_type === 'seed') {
                return [true, undefined];
            }
            const validator = ManifestValidator.getValidator(`${this.lightdashSchemaId}#/definitions/LightdashCompiledModelNode`);
            return ManifestValidator.isValid(validator, model);
        };
        this.isDbtMetricValid = (metric) => {
            const validator = ManifestValidator.getValidator(`${this.dbtSchemaId}#/definitions/Metric`);
            return ManifestValidator.isValid(validator, metric);
        };
        this.lightdashSchemaId = `https://schemas.lightdash.com/lightdash/${manifestVersion}.json`;
        this.dbtSchemaId = `https://schemas.getdbt.com/dbt/manifest/${manifestVersion}.json`;
    }
}
exports.ManifestValidator = ManifestValidator;
ManifestValidator.isValid = (validator, data) => {
    const isValid = validator(data);
    if (!isValid) {
        return [false, ManifestValidator.formatAjvErrors(validator)];
    }
    return [true, undefined];
};
ManifestValidator.formatAjvErrors = (validator) => (validator.errors || [])
    .map((err) => `Field at "${err.instancePath}" ${err.message}`)
    .join('\n');
ManifestValidator.getValidator = (schemaRef) => {
    const validator = ajv.getSchema(schemaRef);
    if (validator === undefined) {
        throw new errors_1.ParseError(`Could not find schema with reference: ${schemaRef}`);
    }
    return validator;
};
//# sourceMappingURL=validation.js.map