"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ajvValidator = void 0;
const ajv_1 = __importDefault(require("ajv"));
const ajv_formats_1 = __importDefault(require("ajv-formats"));
const shared_object_base_1 = require("@fluidframework/shared-object-base");
const runtime_utils_1 = require("@fluidframework/runtime-utils");
const test_runtime_utils_1 = require("@fluidframework/test-runtime-utils");
// See: https://github.com/sinclairzx81/typebox#ajv
const ajv = (0, ajv_formats_1.default)(new ajv_1.default({ strict: false, allErrors: true }), [
    "date-time",
    "time",
    "date",
    "email",
    "hostname",
    "ipv4",
    "ipv6",
    "uri",
    "uri-reference",
    "uuid",
    "uri-template",
    "json-pointer",
    "relative-json-pointer",
    "regex",
]);
class MockHandleContext {
    get IFluidHandleContext() {
        return this;
    }
    constructor(absolutePath = "", routeContext) {
        this.absolutePath = absolutePath;
        this.routeContext = routeContext;
        this.isAttached = false;
    }
    attachGraph() {
        throw new Error("Method not implemented.");
    }
    async resolveHandle(request) {
        return (0, runtime_utils_1.create404Response)(request);
    }
}
const serializer = new shared_object_base_1.FluidSerializer(new MockHandleContext(), () => { });
/**
 * A {@link JsonValidator} implementation which uses Ajv's JSON schema validator.
 *
 * This validator is useful for debugging issues with formats, as the error messages it produces
 * contain information about why the data is out of schema.
 */
exports.ajvValidator = {
    compile: (schema) => {
        const validate = ajv.compile(schema);
        return {
            check: (data) => {
                const valid = validate(data);
                if (!valid) {
                    throw new Error(`Invalid JSON.\n\nData: ${serializer.stringify(data, new test_runtime_utils_1.MockHandle(""))}\n\nErrors: ${JSON.stringify(validate.errors)}`);
                }
                return true;
            },
        };
    },
};
//# sourceMappingURL=ajvValidator.cjs.map