Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#1786] Address jsonschema format_checker deprecation #1974

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions connexion/validators/form_data.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import typing as t

from jsonschema import ValidationError, draft4_format_checker
from jsonschema import Draft4Validator, ValidationError
from starlette.datastructures import Headers, UploadFile
from starlette.formparsers import FormParser, MultiPartParser
from starlette.types import Scope
Expand Down Expand Up @@ -39,7 +39,7 @@ def __init__(
@property
def _validator(self):
return Draft4RequestValidator(
self._schema, format_checker=draft4_format_checker
self._schema, format_checker=Draft4Validator.FORMAT_CHECKER
)

@property
Expand Down
10 changes: 6 additions & 4 deletions connexion/validators/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import typing as t

import jsonschema
from jsonschema import Draft4Validator, ValidationError, draft4_format_checker
from jsonschema import Draft4Validator, ValidationError
from starlette.types import Scope

from connexion.exceptions import BadRequestProblem, NonConformingResponseBody
Expand Down Expand Up @@ -44,7 +44,7 @@ def __init__(
@property
def _validator(self):
return Draft4RequestValidator(
self._schema, format_checker=draft4_format_checker
self._schema, format_checker=Draft4Validator.FORMAT_CHECKER
)

async def _parse(
Expand Down Expand Up @@ -86,7 +86,9 @@ class DefaultsJSONRequestBodyValidator(JSONRequestBodyValidator):
@property
def _validator(self):
validator_cls = self.extend_with_set_default(Draft4RequestValidator)
return validator_cls(self._schema, format_checker=draft4_format_checker)
return validator_cls(
self._schema, format_checker=Draft4Validator.FORMAT_CHECKER
)

# via https://python-jsonschema.readthedocs.io/
@staticmethod
Expand All @@ -111,7 +113,7 @@ class JSONResponseBodyValidator(AbstractResponseBodyValidator):
@property
def validator(self) -> Draft4Validator:
return Draft4ResponseValidator(
self._schema, format_checker=draft4_format_checker
self._schema, format_checker=Draft4Validator.FORMAT_CHECKER
)

def _parse(self, stream: t.Generator[bytes, None, None]) -> t.Any:
Expand Down
6 changes: 3 additions & 3 deletions docs/validation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -325,18 +325,18 @@ Custom type formats
-------------------

It is possible to define custom type formats for validation without adding a custom validator, by
leveraging the ``jsonschema.draft4_format_checker.checks`` decorator.
leveraging the ``jsonschema.Draft4Validator.FORMAT_CHECKER.checks`` decorator.

We can for instance create a custom `money` format.

.. code-block:: python

import re
from jsonschema import draft4_format_checker
from jsonschema import Draft4Validator

MONEY_RE = re.compile('^\$\s*\d+(\.\d\d)?')

@draft4_format_checker.checks('money')
@Draft4Validator.FORMAT_CHECKER.checks('money')
def is_money(val):
if not isinstance(val, str):
return True
Expand Down