JSON Schema Generator for MongoEngine Documents
📖 About the project
What is this and why?
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
- Python 3.10
- MongoEngine 0.27.0
but should work on Python >= 3.7 and MongoEngine >= 0.20.0 without any problems.
🛠 Installation
pip install mongoengine-jsonschema
💻 Getting started
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
}
Example
Check out example.md for a more extensive example.
Features
- Inheritance is supported. Make sure you add mixin to parent class.
additionalPropertiesis set toFalseforDynamicDocumentandDynamicEmbeddedDocumentclasses.requiredkeyword can be removed by settingstrictargument toFalse(.json_schema(strict=False)). This is useful for partial validation when updating documents using HTTP PATCH method.- Constraints for special
StringFieldtypes such asEmailField,URLField,UUIDField,DateTimeFieldetc. are applied to schema usingformatand/orpatternkeywords. - Fields derived from
GeoJsonBaseFieldcan be validated for both array and object types as supported by MongoEngine. - Field arguments/constraints
required,min_length,max_length,min_value,max_value,default,choices,regexandurl_regex(forURLField) are supported and reflected to schema. - Excluding a field from schema is possible with setting field argument
exclude_from_schematoTrue. Example:name = me.StringField(exclude_from_schema=True)
- Auto-generates human-friendly (first-letter capitalized, separate words)
titlefrom both document (PascalCase) and field names (snake_case). Keeps uppercase acronyms as is, e.g.page_URL->Page URL. - For
ListFieldtypes,required=Truemeans it cannot be empty, therefore, schema defines this constraint withminItemskeyword. - Custom schemas can be defined directly in model class with
_JSONSCHEMAclass attribute. Setting a_JSONSCHEMAattribute will bypass JSON schema generation.
Limitations
FileField,ImageFieldfields are not supportedPolygonFieldandMultiPolygonFieldmust start and end at the same point, but this is not enforced by generated schemaschemesargument is ignored forURLFielddomain_whitelist,allow_utf8_user,allow_ip_domainarguments are ignored forEmailField- The following fields are defined in schema as strings and may require field specific conversion before assigning to a document's attribute:
ObjectIdFieldBinaryFieldDateTimeFieldComplexDateTimeFieldDateFieldReferenceFieldLazyReferenceFieldCachedReferenceFieldGenericReferenceFieldGenericLazyReferenceField
👥 Contact
- Email: myusuferoglu@gmail.com
- GitHub: symphonicityy
- Project Link: (https://github.com/symphonicityy/mongoengine-jsonschema)
🤝 Contributing
Contributions, issues, and feature requests are welcome!
Metadata
Release files for mongoengine-jsonschema 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| mongoengine-jsonschema-0.1.3.tar.gz | 13.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| mongoengine_jsonschema-0.1.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 20.7 kB
Release files / mongoengine-jsonschema-0.1.3.tar.gz
| Download URL | mongoengine-jsonschema-0.1.3.tar.gz |
|---|---|
| Size | 13.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b1709517df8b57ae8acb7aa963331e007a94c76167e6eb305a64a29b479dbc0d
|
|
BLAKE2b-256 checksum How to use checksums |
cdb776cd5a4a5cbecaceb2f58c5bebf6159b7ada53ec7f2144d644fc7e99ba26
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.17
|
Release files / mongoengine_jsonschema-0.1.3-py3-none-any.whl
| Download URL | mongoengine_jsonschema-0.1.3-py3-none-any.whl |
|---|---|
| Size | 7.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1661e92854aedb902b35c4ed1cd509b766c12f960b63f8996b26ea4704fd364c
|
|
BLAKE2b-256 checksum How to use checksums |
6f2e21016824ac3ddaf8292706ac5ec7cb15d190cb93191536890e3c41a4b78c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.17
|