Skip to content

Commit

Permalink
fix: add docs to methodCallValidator
Browse files Browse the repository at this point in the history
  • Loading branch information
BelfordZ committed Apr 12, 2019
1 parent 3d7e619 commit d5a9f30
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
41 changes: 38 additions & 3 deletions src/method-call-validator/method-call-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,30 @@ import { generateMethodParamId } from "../generate-method-id";
import { types } from "@open-rpc/meta-schema";
import ParameterValidationError from "./parameter-validation-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 ajvValidator: Ajv.Ajv;

constructor(private schema: types.OpenRPC) {
/**
* @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(private document: types.OpenRPC) {
this.ajvValidator = new Ajv();

schema.methods.forEach((method: types.MethodObject) => {
document.methods.forEach((method: types.MethodObject) => {
const params = method.params as types.ContentDescriptorObject[];
if (method.params === undefined) { return; }

Expand All @@ -22,8 +39,26 @@ export default class MethodCallValidator {
});
}

/**
* 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.
*
* @example
* ```typescript
*
* import { petstore } from "@open-rpc/examples";
* const petStoreMethodCallValidator = new MethodCallValidator(petstore);
* const errors = petStoreMethodCallValidator.validate("list_pets", []);
* // errors.length === 0
* ```
*
*/
public validate(methodName: string, params: any[]): ParameterValidationError[] {
const method = _.find(this.schema.methods, { name: methodName }) as types.MethodObject;
const method = _.find(this.document.methods, { name: methodName }) as types.MethodObject;

if (method.params === undefined) {
return [];
Expand Down
20 changes: 18 additions & 2 deletions src/method-call-validator/parameter-validation-error.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
import { ErrorObject } from "ajv";
import { types } from "@open-rpc/meta-schema";

export default class ParameterValidationError extends Error {
constructor(paramIndex: number, expectedSchema: types.Schema, receievedParam: any, public errors: ErrorObject[]) {
/**
* Provides an error interface for handling when a function call has invalid parameters.
*/
export default class MethodCallParameterValidationError extends Error {

/**
* @param paramIndex The index of the param that for this error (index
* of the param in MethodObject.params).
* @param expectedSchema The expected JSON Schema for the passed in value.
* @param receievedParam The value of the param passed to the method call.
* @param errors The errors recieved by ajv
*/
constructor(
public paramIndex: number,
public expectedSchema: types.Schema,
public receievedParam: any,
private errors: ErrorObject[],
) {
super([
"Expected param in position ",
paramIndex,
Expand Down

0 comments on commit d5a9f30

Please # to comment.