Skip to content

Commit

Permalink
Move request-schema.json to a .py file (#284)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcb authored Aug 28, 2024
1 parent 1d9153e commit 6efc172
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 26 deletions.
9 changes: 4 additions & 5 deletions jsonrpcserver/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
}
}
},
}

0 comments on commit 6efc172

Please sign in to comment.