Skip to content

Commit

Permalink
Use ajv
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastijankuzner committed Feb 29, 2024
1 parent 277b5bf commit 9ad996c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
11 changes: 6 additions & 5 deletions packages/api-common/source/rcp/processor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Hapi from "@hapi/hapi";
import { inject, injectable } from "@mainsail/container";
import { Contracts, Identifiers } from "@mainsail/contracts";
import Joi from "joi";

import { getRcpId, prepareRcpError } from "./utils";

Expand All @@ -14,6 +13,7 @@ export class Processor implements Contracts.Api.RPC.Processor {

public registerAction(action: Contracts.Api.RPC.Action): void {
this.#actions.set(action.name, action);
this.validator.addSchema(action.schema);
}

async process(request: Hapi.Request): Promise<Contracts.Api.RPC.Response | Contracts.Api.RPC.Error> {
Expand Down Expand Up @@ -51,11 +51,12 @@ export class Processor implements Contracts.Api.RPC.Processor {
}

#validateParams(parameters: any, action: Contracts.Api.RPC.Action): boolean {
return this.#validate(action.schema, parameters);
}
if (!action.schema.$id) {
return true;
}

const { error } = this.validator.validate(action.schema.$id, parameters);

#validate(schema: Joi.Schema, data: Contracts.Types.JsonObject): boolean {
const { error } = schema.validate(data);
return !error;
}
}
31 changes: 22 additions & 9 deletions packages/api-evm/source/actions/call.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
import { injectable } from "@mainsail/container";
import { Contracts } from "@mainsail/contracts";
import Joi from "joi";

@injectable()
export class CallAction implements Contracts.Api.RPC.Action {
public readonly name: string = "eth_call";

public readonly schema = Joi.array().items(
Joi.object({
data: Joi.string().required(),
from: Joi.string().required(),
to: Joi.string().required(),
}),
Joi.string().required(),
);
public readonly schema = {
$id: `jsonRpc_${this.name}`,

maxItems: 2,
minItems: 2,

prefixItems: [
{
additionalProperties: false,
properties: {
data: { type: "string" },
from: { type: "string" },
to: { type: "string" },
},
required: ["from", "to", "data"],
type: "object",
},
{ enum: ["latest", "earliest", "pending"], type: "string" },
],

type: "array",
};

public async handle(parameters: any): Promise<any> {
return `OK ${this.name}`;
Expand Down
4 changes: 2 additions & 2 deletions packages/contracts/source/contracts/api/rpc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Hapi from "@hapi/hapi";
import { Schema } from "joi";
import { SchemaObject } from "ajv";

export type Processor = {
registerAction(action: Action): void;
Expand Down Expand Up @@ -33,7 +33,7 @@ export type Error = {
export interface Action {
name: string;
handle: (parameters: any) => Promise<any>;
schema: Schema;
schema: SchemaObject;
}

export enum ErrorCode {
Expand Down

0 comments on commit 9ad996c

Please sign in to comment.