@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
116 lines (115 loc) • 4.75 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var ajv_1 = __importDefault(require("ajv"));
var generate_method_id_1 = require("../generate-method-id");
var parameter_validation_error_1 = __importDefault(require("./parameter-validation-error"));
var method_not_found_error_1 = __importDefault(require("./method-not-found-error"));
var helper_functions_1 = require("../helper-functions");
var method_ref_unexpected_error_1 = __importDefault(require("./method-ref-unexpected-error"));
/**
* A class to assist in validating method calls to an OpenRPC-based service. Generated Clients,
* Servers, and many others may want to expose the interface provided by an OpenRPC document.
* In doing so, use this class to easily create a re-useable validator for a particular method.
*/
var MethodCallValidator = /** @class */ (function () {
/**
* @param document The OpenRPC document containing the methods whose calls we want validated.
*
* @example
* ```typescript
*
* import { petstore } from "@open-rpc/examples";
* const petStoreMethodCallValidator = new MethodCallValidator(petstore);
* // Go on and use it!
* ```
*
*/
function MethodCallValidator(document) {
var _this = this;
this.document = document;
this.ajvValidator = new ajv_1.default();
// Validate that the methods are dereferenced
document.methods.forEach(function (method) {
if (method.$ref)
throw new method_ref_unexpected_error_1.default(method.$ref, document);
});
document.methods.forEach(function (method) {
var params = method.params;
params.forEach(function (param) {
if (param.schema === undefined) {
return;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
_this.ajvValidator.addSchema(param.schema, (0, generate_method_id_1.generateMethodParamId)(method, param));
});
});
}
/**
* Validates a particular method call against the OpenRPC definition for the method.
*
* @param methodName the name of the method in the OpenRPC Document.
* @param params the param values that you want validated.
*
* @returns an array of parameter validation errors, or if there are none, an empty array.
* if the method name is invalid, a [[MethodNotFoundError]] is returned.
*
* @example
* ```typescript
*
* import { petstore } from "@open-rpc/examples";
* const petStoreMethodCallValidator = new MethodCallValidator(petstore);
* const errors = petStoreMethodCallValidator.validate("list_pets", []);
* // errors.length === 0
* ```
*
*/
MethodCallValidator.prototype.validate = function (methodName,
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
params) {
var _this = this;
if (methodName === "rpc.discover") {
return [];
}
var method = (0, helper_functions_1.find)(this.document.methods, function (o) {
return o.name == methodName;
});
if (!method) {
return new method_not_found_error_1.default(methodName, this.document, params);
}
var paramMap = method.params;
return (0, helper_functions_1.compact)(paramMap.map(function (param, index) {
var id;
if (method.paramStructure === "by-position") {
id = index;
}
else if (method.paramStructure === "by-name") {
id = param.name;
}
else {
if (params[index] !== undefined) {
id = index;
}
else {
id = param.name;
}
}
var input = params[id];
if (input === undefined && !param.required) {
return;
}
if (param.schema !== undefined) {
var idForMethod = (0, generate_method_id_1.generateMethodParamId)(method, param);
var isValid = _this.ajvValidator.validate(idForMethod, input);
var errors = _this.ajvValidator.errors;
if (!isValid) {
return new parameter_validation_error_1.default(id, param.schema, input, errors);
}
}
}));
};
return MethodCallValidator;
}());
exports.default = MethodCallValidator;