MongoEngine JSON Schema Generator
MIT License
This package provides a mixin class that extends a MongoEngine document's functionality by adding a .json_schema()
method and allows generating a JSON schema directly from the document. Generated schema then can be used in API documentation or form validation and automatic form generation on a web application frontend, etc.
Generated schema should be compatible with JSON schema specification version Draft-7 and newer.
Tested on
but should work on Python >= 3.7
and MongoEngine >= 0.20.0
without any problems.
pip install mongoengine-jsonschema
Add JsonSchemaMixin
to your document class as parent. Resolution order matters, so always place MongoEngine document first.
import mongoengine as me
from mongoengine_jsonschema import JsonSchemaMixin
class Person(me.Document, JsonSchemaMixin):
name = me.StringField(required=True, min_length=1, max_length=32)
age = me.IntField(min_value=0)
Then you can generate JSON schema by calling .json_schema()
method.
Person.json_schema()
which returns the schema as a Python dictionary
{
'$id': '/schemas/Person',
'title': 'Person',
'type': 'object',
'properties': {
'age': {
'type': 'integer',
'title': 'Age',
'minimum': 0
},
'name': {
'type': 'string',
'title': 'Name',
'minLength': 1,
'maxLength': 32
}
},
'required': ['name'],
'additionalProperties': False
}
Check out example.md for a more extensive example.
additionalProperties
is set to False
for DynamicDocument
and DynamicEmbeddedDocument
classes.required
keyword can be removed by setting strict
argument to False
(.json_schema(strict=False)
). This is useful for partial validation when updating documents using HTTP PATCH method.StringField
types such as EmailField
, URLField
, UUIDField
, DateTimeField
etc. are applied to schema using format
and/or pattern
keywords.GeoJsonBaseField
can be validated for both array and object types as supported by MongoEngine.required
, min_length
, max_length
, min_value
, max_value
, default
, choices
, regex
and url_regex
(for URLField
) are supported and reflected to schema.exclude_from_schema
to True
. Example:
name = me.StringField(exclude_from_schema=True)
title
from both document (PascalCase) and field names (snake_case). Keeps uppercase acronyms as is, e.g. page_URL
-> Page URL
.ListField
types, required=True
means it cannot be empty, therefore, schema defines this constraint with minItems
keyword._JSONSCHEMA
class attribute. Setting a _JSONSCHEMA
attribute will bypass JSON schema generation.FileField
, ImageField
fields are not supportedPolygonField
and MultiPolygonField
must start and end at the same point, but this is not enforced by generated schemaschemes
argument is ignored for URLField
domain_whitelist
, allow_utf8_user
, allow_ip_domain
arguments are ignored for EmailField
ObjectIdField
BinaryField
DateTimeField
ComplexDateTimeField
DateField
ReferenceField
LazyReferenceField
CachedReferenceField
GenericReferenceField
GenericLazyReferenceField
Contributions, issues, and feature requests are welcome!