Skip to content

Commit

Permalink
Remove cookbook docs and sort index better (#1765)
Browse files Browse the repository at this point in the history
Contributes to #1531
  • Loading branch information
RobbeSneyders committed Oct 30, 2023
1 parent 99c7a13 commit 17aa31a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 153 deletions.
151 changes: 0 additions & 151 deletions docs/cookbook.rst

This file was deleted.

3 changes: 1 addition & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ Documentation
:maxdepth: 2

quickstart
middleware
routing
swagger_ui
request
Expand All @@ -72,8 +71,8 @@ Documentation
security
context
lifespan
cookbook
exceptions
middleware
cli
v3

Expand Down
47 changes: 47 additions & 0 deletions docs/validation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -321,5 +321,52 @@ Which you then pass into your application or API as mentioned above.

See our `enforce defaults`_ example for a full example.

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.

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

.. code-block:: python
import re
from jsonschema import draft4_format_checker
MONEY_RE = re.compile('^\$\s*\d+(\.\d\d)?')
@draft4_format_checker.checks('money')
def is_money(val):
if not isinstance(val, str):
return True
return MONEY_RE.match(val)
Which you can then use in your openAPI specification:

.. code-block:: yaml
type: object
properties:
title:
type: string
price_label:
type: string
format: money
The format checker function is expected to return ``True`` when the
value matches the expected format and return ``False`` when it
doesn't. Also is important to verify if the type of the value you are
trying to validate is compatible with the format. In our example we
check if the ``val`` is of type "string" before performing any further
checking.

.. note::

Keep in mind that the format checkers should be defined and registered before you run your
application server.

.. _enforce defaults: https://github.com/spec-first/connexion/tree/main/examples/enforcedefaults
.. _jsonschema: https://github.com/python-jsonschema/jsonschema

0 comments on commit 17aa31a

Please sign in to comment.