How to support x-nullable for swagger in python's jsonschema library

Introduction

swagger is not fully compatible with json schema. I use a library with the same name as jsonschema for validation with jsonschema in python.

As I wrote earlier, swagger does not allow null types. However, there are times when you want to return null, so a note on how to deal with it.

x-nullable support

Some tools for swagger add the attribute x-nullable to the support for nullable. In terms of swagger, attributes starting with x- are allowed to be attached freely.

The method to deal with this is as follows.

from jsonschema.validators import Draft4Validator, extend
from jsonschema._validators import type_draft4


def type_custom(validator, types, instance, schema, nullable_attr="x-nullable"):
    if schema.get(nullable_attr, False):
        if not isinstance(types, (list, tuple)):
            types = [types]
        types.append("null")
    yield from type_draft4(validator, types, instance, schema)


CustomValidator = extend(Draft4Validator, {
    "type": type_custom,
})

You can actually use it like this.


schema = {
    "type": ["string", "null"]
}

validate = CustomValidator(schema).validate
print(validate(None, schema))
print(validate(None, {"type": "string", "x-nullable": True}))

Recommended Posts

How to support x-nullable for swagger in python's jsonschema library
[For beginners] How to register a library created in Python in PyPI
How to use the C library in Python
How to use Python Image Library in python3 series
[For beginners] How to use say command in python!
How to run python in virtual space (for MacOS)
How to debug the Python standard library in Visual Studio
How to import Python library set up in EFS to Lambda
[python] How to use the library Matplotlib for drawing graphs
How to define multiple variables in a python for statement
How to specify Cache-Control for blob storage in Azure Storage in Python
How to implement Python EXE for Windows in Docker container
For beginners, how to deal with common errors in keras
How to develop in Python
Notes on how to use marshmallow in the schema library
How to set the output resolution for each keyframe in Blender
[For beginners] How to implement O'reilly sample code in Google Colab
How to configure CORS settings for Azure storage service in Python
[Introduction to Python] How to use the in operator in a for statement?
[Beginner memo] How to specify the library reading path in Python
How to handle session in SQLAlchemy
[For non-programmers] How to walk Kaggle
How to use classes in Theano
How to write soberly in pandas
How to collect images in Python
How to update Spyder in Anaconda
How to use Requests (Python Library)
How to use SQLite in Python
How to use Python's logging module
How to convert 0.5 to 1056964608 in one shot
[Python] How to import the library
How to reflect CSS in Django
How to kill processes in bulk
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
How to run TensorFlow 1.0 code in 2.0
How to handle Japanese in Python
How to log in to Docker + NGINX
How to use Python's Context Manager
How to call PyTorch in Julia
How to make a model for object detection using YOLO in 3 hours
How to run a quickstart for Google Cloud Text-to-Speech .NET in Unity
How to use calculated columns in CASTable
[Introduction to Python] How to use class in Python?
How to suppress display error in matplotlib
How to access environment variables in Python
How to dynamically define variables in Python
How to do R chartr () in Python
How to create * .spec files for pyinstaller.
How to convert csv to tsv in CLI
How to delete expired sessions in Django
[Itertools.permutations] How to put permutations in Python
How to use Google Test in C
How to end Python's infinite scroll scraping
How to implement nested serializer in drf-flex-fields
How to work with BigQuery in Python
How to execute commands in jupyter notebook
How to do'git fetch --tags' in GitPython
How to get a stacktrace in python