@open-rpc/schema-utils-js
Version:
<center> <span> <img alt="CircleCI branch" src="https://img.shields.io/circleci/project/github/open-rpc/schema-utils-js/master.svg"> <img src="https://codecov.io/gh/open-rpc/schema-utils-js/branch/master/graph/badge.svg" /> <img alt="npm" sr
80 lines (79 loc) • 2.61 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OpenRPCDocumentValidationError = void 0;
var ajv_1 = __importDefault(require("ajv"));
var meta_schema_1 = __importDefault(require("@json-schema-tools/meta-schema"));
var apply_extension_spec_1 = __importDefault(require("./apply-extension-spec"));
var get_extended_metaschema_1 = __importDefault(require("./get-extended-metaschema"));
/**
* @ignore
*/
/**
* Provides an error interface for OpenRPC Document validation
*
* @category Errors
*
*/
var OpenRPCDocumentValidationError = /** @class */ (function () {
/**
* @param errors The errors received by ajv.errors.
*/
function OpenRPCDocumentValidationError(errors) {
this.name = "OpenRPCDocumentDereferencingError";
this.message = [
"Error validating OpenRPC Document against @open-rpc/meta-schema.",
"The errors found are as follows:",
JSON.stringify(errors, undefined, " "),
].join("\n");
}
return OpenRPCDocumentValidationError;
}());
exports.OpenRPCDocumentValidationError = OpenRPCDocumentValidationError;
/**
* Returns any JSON Schema validation errors that are found with the OpenRPC document passed in.
*
* @param document OpenRPC Document to validate.
*
* @returns Either true if everything checks out, or a well formatted error.
*
* @example
* ```typescript
*
* import { validateOpenRPCDocument } from "@open-rpc/schema-utils-js";
* const badOpenRPCDocument = {} as any;
*
* const result = validateOpenRPCDocument(badOpenRPCDocument);
* if (result !== true) {
* console.error(result);
* }
* ```
*
*/
function validateOpenRPCDocument(document) {
var ajv = new ajv_1.default();
ajv.addSchema(meta_schema_1.default, "https://meta.json-schema.tools");
var extMetaSchema = (0, get_extended_metaschema_1.default)();
try {
extMetaSchema = (0, apply_extension_spec_1.default)(document, extMetaSchema);
ajv.validate(extMetaSchema, document);
}
catch (e) {
throw new Error([
"schema-utils-js: Internal Error",
"-----",
e,
"-----",
"If you see this report it: https://github.com/open-rpc/schema-utils-js/issues",
].join("\n"));
}
if (ajv.errors) {
return new OpenRPCDocumentValidationError(ajv.errors);
}
else {
return true;
}
}
exports.default = validateOpenRPCDocument;
;