Nested schemas land in definitions and are referenced via $ref:
from marshmallow import Schema, fields
from marshmallow_jsonschema import JSONSchema
class AddressSchema(Schema):
street = fields.String()
city = fields.String()
class UserSchema(Schema):
name = fields.String()
address = fields.Nested(AddressSchema)
JSONSchema().dump(UserSchema())
A complete runnable Flask example lives at
example/example.py. It exposes a marshmallow
schema as JSON Schema and renders it as a form using
json-editor — pure JS,
no build pipeline, drop in via CDN:
pip install -r example/requirements.txt
python example/example.py
# open http://127.0.0.1:5000/
minimum / maximum (or exclusiveMinimum / exclusiveMaximum)
validate.OneOf(choices, labels=)
enum (and the non-standard enumNames for compatibility with react-jsonschema-form)
validate.Equal(value)
enum: [value]
validate.Regexp(pattern)
pattern
validate.ContainsOnly(choices, labels=)
items.anyOf: [{const}, ...] + uniqueItems: true
from marshmallow import Schema, fields, validate
from marshmallow_jsonschema import JSONSchema
class UserSchema(Schema):
age = fields.Integer(validate=validate.Range(min=0, max=150))
name = fields.String(validate=validate.Length(min=1, max=100))
role = fields.String(validate=validate.OneOf(["admin", "user"]))
For a custom validator that's a subclass of one of the above, set
_jsonschema_base_validator_class = validate.<Base> on it so the
translation still fires.
Enums
marshmallow.fields.Enum (added in marshmallow 3.18) is supported
out of the box and emits the enum-member names. The third-party
marshmallow-enumEnumField is also supported when installed; native Enum is preferred
when both are present.
by_value=True enums are supported when every member's value is a
string — the common pattern for serialising enums as their string
value in the wire format:
class Status(str, Enum):
ACTIVE = "active"
INACTIVE = "inactive"
class S(Schema):
status = fields.Enum(Status, by_value=True)
# {"status": {"enum": ["active", "inactive"], "type": "string"}}
Mixed-type or non-string enum values still raise
NotImplementedError — use the class MyEnum(str, Enum) pattern, or
load by name instead.
Advanced usage
Schema-level title and description
Setting title or description on a schema's inner Meta class
emits them at the corresponding definition entry:
class UserSchema(Schema):
class Meta:
title = "User"
description = "A user account record."
name = fields.String()
Customizing the definitions path
By default nested schemas live under #/definitions/<Name>. Pass
definitions_path to use a different single-segment key:
Multi-segment paths (e.g. "components/schemas") are rejected because
they would produce a flat dict key with a slash in it rather than the
nested structure consumers expect — wrap the output yourself if you
need that shape.
Custom field types
Add a _jsonschema_type_mapping method to your field so we know how
to serialize it. Field-level metadata={...} and dump_default
values are then merged in automatically, so a single mapping method
gets you the full set of standard schema attributes.
class Colour(fields.Field):
def _jsonschema_type_mapping(self):
return {"type": "string"}
def _serialize(self, value, attr, obj):
r, g, b = value
return "#%02X%02X%02X" % (r, g, b)
class UserSchema(Schema):
favourite_colour = Colour(
dump_default="#ffffff",
metadata={"title": "Colour", "description": "Hex RGB"},
)
For wrapper-style custom fields that need to re-enter the dumping
machinery (e.g. to emit a $ref to a recursive schema), declare
_jsonschema_type_mapping(self, json_schema, obj) with the two extra
parameters and the JSONSchema instance + obj will be passed in.
Polymorphic schemas (marshmallow-oneofschema)
When the optional marshmallow-oneofschema
package is installed, dumping a OneOfSchema produces a JSON Schema
oneOf over each variant — with the discriminator field pinned to its
constant value:
from marshmallow import Schema, fields
from marshmallow_oneofschema import OneOfSchema
from marshmallow_jsonschema import JSONSchema
class TriangleSchema(Schema):
base = fields.Float(required=True)
height = fields.Float(required=True)
class CircleSchema(Schema):
radius = fields.Float(required=True)
class ShapeSchema(OneOfSchema):
type_schemas = {"triangle": TriangleSchema, "circle": CircleSchema}
JSONSchema().dump(ShapeSchema())
Variants are inlined (not $ref) so the schema actually validates the
wire format OneOfSchema produces. Custom type_field (default
"type") is honored. Install with pip install marshmallow-jsonschema[oneofschema].
React-JSONSchema-Form Extension
react-jsonschema-form
renders JSON Schema as a React form. It accepts a separate
uiSchema
that controls presentation; this package's
ReactJsonSchemaFormJSONSchema extension dumps both at once:
from marshmallow import Schema, fields
from marshmallow_jsonschema.extensions import ReactJsonSchemaFormJSONSchema
class MySchema(Schema):
first_name = fields.String(metadata={"ui:autofocus": True})
last_name = fields.String()
class Meta:
react_uischema_extra = {"ui:order": ["first_name", "last_name"]}
json_schema_obj = ReactJsonSchemaFormJSONSchema()
data = json_schema_obj.dump(MySchema())
ui_schema_json = json_schema_obj.dump_uischema(MySchema())
Contributing
Bug reports and pull requests are welcome. See
CONTRIBUTING.md for local-dev setup and
CONTRIBUTORS.md for the people who built this.
Works well with
Use together with marshmallow-jsonschema: extensions, drivers, servers and companion tools.
marshmallowPythie’s Index: 92A lightweight library for converting complex datatypes to and from native Python datatypes.93M downloads/mo
marshmallow-dataclassPythie’s Index: 30Python library to convert dataclasses into marshmallow schemas.4.2M downloads/mo
marshmallow-sqlalchemyPythie’s Index: 76SQLAlchemy integration with the marshmallow (de)serialization library4.5M downloads/mo