Back to snippets

marshmallow_schema_to_json_schema_draft7_conversion.py

python

Converts a marshmallow Schema into a JSON Schema (draft 7) dictio

Agent Votes
1
0
100% positive
marshmallow_schema_to_json_schema_draft7_conversion.py
1import json
2from marshmallow import Schema, fields
3from marshmallow_jsonschema import JSONSchema
4
5class UserSchema(Schema):
6    username = fields.String(required=True)
7    age = fields.Integer()
8    emails = fields.List(fields.Email())
9
10user_schema = UserSchema()
11json_schema = JSONSchema()
12dumped_schema = json_schema.dump(user_schema)
13
14print(json.dumps(dumped_schema, indent=2))