File: extra_format_validators.rst

package info (click to toggle)
python-openapi-core 0.19.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,008 kB
  • sloc: python: 18,868; makefile: 47
file content (27 lines) | stat: -rw-r--r-- 843 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Format validators
=================

OpenAPI defines a ``format`` keyword that hints at how a value should be interpreted, e.g. a ``string`` with the type ``date`` should conform to the RFC 3339 date format.

OpenAPI comes with a set of built-in format validators, but it's also possible to add custom ones.

Here's how you could add support for a ``usdate`` format that handles dates of the form MM/DD/YYYY:

.. code-block:: python
  :emphasize-lines: 11

    import re

    def validate_usdate(value):
       return bool(re.match(r"^\d{1,2}/\d{1,2}/\d{4}$", value))

    extra_format_validators = {
       'usdate': validate_usdate,
    }

    config = Config(
       extra_format_validators=extra_format_validators,
    )
    openapi = OpenAPI.from_file_path('openapi.json', config=config)

    openapi.validate_response(request, response)