Skip to content

Commit

Permalink
fix: constrain typing more
Browse files Browse the repository at this point in the history
  • Loading branch information
BelfordZ committed Apr 12, 2019
1 parent 3f80428 commit af5ce00
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/get-open-rpc-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ type TGetOpenRPCDocument = (schema: string) => Promise<types.OpenRPC>;
const fetchUrlSchemaFile: TGetOpenRPCDocument = async (schema) => {
try {
const response = await fetch(schema);
return await response.json();
return await response.json() as types.OpenRPC;
} catch (e) {
throw new Error(`Unable to download openrpc.json file located at the url: ${schema}`);
}
};

const readSchemaFromFile = async (schema: string) => {
const readSchemaFromFile: TGetOpenRPCDocument = async (schema) => {
try {
return await readJson(schema);
return await readJson(schema) as types.OpenRPC;
} catch (e) {
if (e.message.includes("SyntaxError")) {
throw new Error(`Failed to parse json in file ${schema}`);
Expand Down
12 changes: 6 additions & 6 deletions src/method-call-validator/method-call-validator.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import Ajv, { ErrorObject } from "ajv";
import Ajv, { ErrorObject, Ajv as IAjv } from "ajv";
import * as _ from "lodash";
import { generateMethodParamId } from "../generate-method-id";
import { types } from "@open-rpc/meta-schema";
import ParameterValidationError from "./parameter-validation-error";
import MethodCallParameterValidationError 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;
private ajvValidator: IAjv;

/**
* @param document The OpenRPC document containing the methods whose calls we want validated.
Expand Down Expand Up @@ -57,23 +57,23 @@ export default class MethodCallValidator {
* ```
*
*/
public validate(methodName: string, params: any[]): ParameterValidationError[] {
public validate(methodName: string, params: any[]): MethodCallParameterValidationError[] {
const method = _.find(this.document.methods, { name: methodName }) as types.MethodObject;

if (method.params === undefined) {
return [];
}

return _.chain(method.params as types.ContentDescriptorObject[])
.map((param: types.ContentDescriptorObject, index: number): ParameterValidationError | undefined => {
.map((param: types.ContentDescriptorObject, index: number): MethodCallParameterValidationError | undefined => {
if (param.schema === undefined) { return; }

const idForMethod = generateMethodParamId(method, param);
const isValid = this.ajvValidator.validate(idForMethod, params[index]);
const errors = this.ajvValidator.errors as ErrorObject[];

if (!isValid) {
return new ParameterValidationError(index, param.schema, params[index], errors);
return new MethodCallParameterValidationError(index, param.schema, params[index], errors);
}
})
.compact()
Expand Down
4 changes: 2 additions & 2 deletions src/validate-open-rpc-document.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import metaSchema, { types } from "@open-rpc/meta-schema";
import JsonSchemaDraft07 from "../lib/json-schema-draft-07.json";
import Ajv from "ajv";
import Ajv, { ErrorObject } from "ajv";

/**
* @ignore
Expand All @@ -16,7 +16,7 @@ export class OpenRPCDocumentValidationError extends Error {
/**
* @param errors The errors received by ajv.errors.
*/
constructor(private errors: Ajv.ErrorObject[]) {
constructor(private errors: ErrorObject[]) {
super([
"Error validating OpenRPC Document against @open-rpc/meta-schema.",
"The errors found are as follows:",
Expand Down

0 comments on commit af5ce00

Please # to comment.