*Remove use of anyOf and set Avj allowUnionTypes

*Compile schema to validateRpcRequest
*Use errorsText to generate errors for validation
This commit is contained in:
Derrick Hammer 2022-08-28 02:48:53 -04:00
parent 44af1cf765
commit 16e8eab836
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 6 additions and 12 deletions

View File

@ -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);

View File

@ -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",