"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateJsonSchema = void 0;
const precompiledValidators = __importStar(require("../generated/precompiled-validators.js"));
/**
 * Validates the given payload using JSON schema keyed by the given schema name. Throws if the given payload fails validation.
 * @param schemaName the schema name use to look up the JSON schema to be used for schema validation.
 *                   The list of schema names can be found in compile-validators.js
 * @param payload javascript object to be validated
 */
function validateJsonSchema(schemaName, payload) {
    // const validateFn = validator.getSchema(schemaName);
    const validateFn = precompiledValidators[schemaName];
    if (!validateFn) {
        throw new Error(`schema for ${schemaName} not found.`);
    }
    validateFn(payload);
    if (!validateFn.errors) {
        return;
    }
    // AJV is configured by default to stop validating after the 1st error is encountered which means
    // there will only ever be one error;
    const [errorObj] = validateFn.errors;
    let { instancePath, message } = errorObj;
    if (!instancePath) {
        instancePath = schemaName;
    }
    throw new Error(`${instancePath}: ${message}`);
}
exports.validateJsonSchema = validateJsonSchema;
//# sourceMappingURL=schema-validator.js.map