UNPKG

@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

46 lines (45 loc) 1.79 kB
import ParameterValidationError from "./parameter-validation-error"; import { OpenrpcDocument as OpenRPC } from "@open-rpc/meta-schema"; import MethodNotFoundError from "./method-not-found-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. */ export default class MethodCallValidator { private document; private ajvValidator; /** * @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! * ``` * */ constructor(document: OpenRPC); /** * 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 * ``` * */ validate(methodName: string, params: any): ParameterValidationError[] | MethodNotFoundError; }