var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
define(["require", "exports", "ajv"], function (require, exports, ajv_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    ajv_1 = __importDefault(ajv_1);
    /**
     * Base class for all JSON readers.
     */
    class MetadataJSONReader {
        /**
         * @param schema The JSON schema with which to validate the metadata.
         */
        constructor(schema) {
            this.schema = schema;
        }
        /**
         * A validator that uses the schema set for this reader.
         */
        get validator() {
            if (this._validator === undefined) {
                const ajv = new ajv_1.default();
                this._validator = ajv.compile(this.schema);
            }
            return this._validator;
        }
        read(object) {
            this.validate(object);
            return this.convert(object);
        }
        /**
         * Validate the object against the schema that was set for this reader.
         *
         * @param object The object to validate.
         */
        validate(object) {
            const validator = this.validator;
            const valid = validator(object);
            if (!valid) {
                if (validator.errors === undefined) {
                    throw new Error("metadata JSON invalid but no errors!");
                }
                const error = new Error("failed to validate");
                // Yes, we cheat. This is not meant to be a full-fledged diagnosis
                // mechanism.
                // tslint:disable-next-line:no-any
                error.jsonErrors = validator.errors;
                throw error;
            }
        }
    }
    exports.MetadataJSONReader = MetadataJSONReader;
});
//  LocalWords:  MPL
//# sourceMappingURL=metadata-json-reader.js.map