From 6efc17269b57b0cd31756c751154988c2af7826a Mon Sep 17 00:00:00 2001 From: Beau Date: Wed, 28 Aug 2024 11:19:15 +1000 Subject: [PATCH] Move request-schema.json to a .py file (#284) --- jsonrpcserver/main.py | 9 +++-- ...{request-schema.json => request_schema.py} | 35 ++++++++----------- 2 files changed, 18 insertions(+), 26 deletions(-) rename jsonrpcserver/{request-schema.json => request_schema.py} (51%) diff --git a/jsonrpcserver/main.py b/jsonrpcserver/main.py index 19070b2..70fdd18 100644 --- a/jsonrpcserver/main.py +++ b/jsonrpcserver/main.py @@ -11,7 +11,6 @@ """ import json -from importlib.resources import read_text from typing import Any, Callable, Dict, List, Union, cast from jsonschema.validators import validator_for # type: ignore @@ -23,6 +22,7 @@ validate_args, ) from .methods import Methods, global_methods +from .request_schema import REQUEST_SCHEMA from .response import Response, to_dict from .sentinels import NOCONTEXT from .utils import identity @@ -32,10 +32,9 @@ # Prepare the jsonschema validator. This is global so it loads only once, not every time # dispatch is called. -schema = json.loads(read_text(__package__, "request-schema.json")) -klass = validator_for(schema) -klass.check_schema(schema) -default_jsonrpc_validator = klass(schema).validate +klass = validator_for(REQUEST_SCHEMA) +klass.check_schema(REQUEST_SCHEMA) +default_jsonrpc_validator = klass(REQUEST_SCHEMA).validate def dispatch_to_response( diff --git a/jsonrpcserver/request-schema.json b/jsonrpcserver/request_schema.py similarity index 51% rename from jsonrpcserver/request-schema.json rename to jsonrpcserver/request_schema.py index 52bb147..fc163e4 100644 --- a/jsonrpcserver/request-schema.json +++ b/jsonrpcserver/request_schema.py @@ -1,39 +1,32 @@ -{ +REQUEST_SCHEMA = { "$schema": "http://json-schema.org/draft-04/schema#", "description": "A JSON RPC 2.0 request", "oneOf": [ - { - "description": "An individual request", - "$ref": "#/definitions/request" - }, + {"description": "An individual request", "$ref": "#/definitions/request"}, { "description": "An array of requests", "type": "array", - "items": { "$ref": "#/definitions/request" }, - "minItems": 1 - } + "items": {"$ref": "#/definitions/request"}, + "minItems": 1, + }, ], "definitions": { "request": { "type": "object", - "required": [ "jsonrpc", "method" ], + "required": ["jsonrpc", "method"], "properties": { - "jsonrpc": { "enum": [ "2.0" ] }, - "method": { - "type": "string" - }, + "jsonrpc": {"enum": ["2.0"]}, + "method": {"type": "string"}, "id": { - "type": [ "string", "number", "null" ], + "type": ["string", "number", "null"], "note": [ "While allowed, null should be avoided: http://www.jsonrpc.org/specification#id1", - "While allowed, a number with a fractional part should be avoided: http://www.jsonrpc.org/specification#id2" - ] + "While allowed, a number with a fractional part should be avoided: http://www.jsonrpc.org/specification#id2", + ], }, - "params": { - "type": [ "array", "object" ] - } + "params": {"type": ["array", "object"]}, }, - "additionalProperties": false + "additionalProperties": False, } - } + }, }