*Remove use of anyOf and set Avj allowUnionTypes
*Compile schema to validateRpcRequest *Use errorsText to generate errors for validation
This commit is contained in:
parent
44af1cf765
commit
16e8eab836
|
@ -15,8 +15,8 @@ import stringify from "json-stable-stringify";
|
||||||
import Ajv from "ajv";
|
import Ajv from "ajv";
|
||||||
import RPCConnection from "./connection.js";
|
import RPCConnection from "./connection.js";
|
||||||
|
|
||||||
const ajv = new Ajv();
|
const ajv = new Ajv({ allowUnionTypes: true });
|
||||||
ajv.addSchema(RPC_REQUEST_SCHEMA, "rpc_request");
|
const validateRpcRequest = ajv.compile(RPC_REQUEST_SCHEMA);
|
||||||
|
|
||||||
let server: RPCServer;
|
let server: RPCServer;
|
||||||
|
|
||||||
|
@ -193,9 +193,10 @@ export class RPCServer {
|
||||||
}
|
}
|
||||||
|
|
||||||
private verifyRequest(request: RPCRequest) {
|
private verifyRequest(request: RPCRequest) {
|
||||||
let valid: any = ajv.getSchema("rpc_request")?.(request);
|
let valid: boolean | Error | RPCMethod = validateRpcRequest(request);
|
||||||
|
|
||||||
if (!valid) {
|
if (!valid) {
|
||||||
return new Error("Invalid request");
|
return new Error(ajv.errorsText(validateRpcRequest.errors));
|
||||||
}
|
}
|
||||||
|
|
||||||
valid = this.getMethodByRequest(request);
|
valid = this.getMethodByRequest(request);
|
||||||
|
|
|
@ -34,14 +34,7 @@ export const RPC_REQUEST_SCHEMA: JSONSchemaType<RPCRequest> = {
|
||||||
type: "string",
|
type: "string",
|
||||||
},
|
},
|
||||||
data: {
|
data: {
|
||||||
type: "string",
|
type: ["number", "string", "boolean", "object", "array"],
|
||||||
anyOf: [
|
|
||||||
{ type: "string" },
|
|
||||||
{ type: "number" },
|
|
||||||
{ type: "integer" },
|
|
||||||
{ type: "object" },
|
|
||||||
{ type: "array" },
|
|
||||||
],
|
|
||||||
},
|
},
|
||||||
bypassCache: {
|
bypassCache: {
|
||||||
type: "boolean",
|
type: "boolean",
|
||||||
|
|
Loading…
Reference in New Issue