Skip to content

Commit 6efc172

Browse files
authored
Move request-schema.json to a .py file (#284)
1 parent 1d9153e commit 6efc172

File tree

2 files changed

+18
-26
lines changed

2 files changed

+18
-26
lines changed

jsonrpcserver/main.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
"""
1212

1313
import json
14-
from importlib.resources import read_text
1514
from typing import Any, Callable, Dict, List, Union, cast
1615

1716
from jsonschema.validators import validator_for # type: ignore
@@ -23,6 +22,7 @@
2322
validate_args,
2423
)
2524
from .methods import Methods, global_methods
25+
from .request_schema import REQUEST_SCHEMA
2626
from .response import Response, to_dict
2727
from .sentinels import NOCONTEXT
2828
from .utils import identity
@@ -32,10 +32,9 @@
3232

3333
# Prepare the jsonschema validator. This is global so it loads only once, not every time
3434
# dispatch is called.
35-
schema = json.loads(read_text(__package__, "request-schema.json"))
36-
klass = validator_for(schema)
37-
klass.check_schema(schema)
38-
default_jsonrpc_validator = klass(schema).validate
35+
klass = validator_for(REQUEST_SCHEMA)
36+
klass.check_schema(REQUEST_SCHEMA)
37+
default_jsonrpc_validator = klass(REQUEST_SCHEMA).validate
3938

4039

4140
def dispatch_to_response(
Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,32 @@
1-
{
1+
REQUEST_SCHEMA = {
22
"$schema": "http://json-schema.org/draft-04/schema#",
33
"description": "A JSON RPC 2.0 request",
44
"oneOf": [
5-
{
6-
"description": "An individual request",
7-
"$ref": "#/definitions/request"
8-
},
5+
{"description": "An individual request", "$ref": "#/definitions/request"},
96
{
107
"description": "An array of requests",
118
"type": "array",
12-
"items": { "$ref": "#/definitions/request" },
13-
"minItems": 1
14-
}
9+
"items": {"$ref": "#/definitions/request"},
10+
"minItems": 1,
11+
},
1512
],
1613
"definitions": {
1714
"request": {
1815
"type": "object",
19-
"required": [ "jsonrpc", "method" ],
16+
"required": ["jsonrpc", "method"],
2017
"properties": {
21-
"jsonrpc": { "enum": [ "2.0" ] },
22-
"method": {
23-
"type": "string"
24-
},
18+
"jsonrpc": {"enum": ["2.0"]},
19+
"method": {"type": "string"},
2520
"id": {
26-
"type": [ "string", "number", "null" ],
21+
"type": ["string", "number", "null"],
2722
"note": [
2823
"While allowed, null should be avoided: http://www.jsonrpc.org/specification#id1",
29-
"While allowed, a number with a fractional part should be avoided: http://www.jsonrpc.org/specification#id2"
30-
]
24+
"While allowed, a number with a fractional part should be avoided: http://www.jsonrpc.org/specification#id2",
25+
],
3126
},
32-
"params": {
33-
"type": [ "array", "object" ]
34-
}
27+
"params": {"type": ["array", "object"]},
3528
},
36-
"additionalProperties": false
29+
"additionalProperties": False,
3730
}
38-
}
31+
},
3932
}

0 commit comments

Comments
 (0)