Skip to main content

JSON Schema Draft v7 (http://json-schema.org/) formatting with marshmallow

Project description

marshmallow-jsonschema-2: JSON Schema formatting with marshmallow

Build Status Ruff

marshmallow-jsonschema translates marshmallow schemas into JSON Schema Draft v7 compliant jsonschema. See http://json-schema.org/

Forked from marshmallow-jsonschema

Why would I want my schema translated to JSON?

What are the use cases for this? Let's say you have a marshmallow schema in python, but you want to render your schema as a form in another system (for example: a web browser or mobile device).

Installation

Requires python>=3.9 and marshmallow>=3.11.

pip install marshmallow-jsonschema-2

The import name remains marshmallow_jsonschema for drop-in compatibility with the original package:

from marshmallow_jsonschema import JSONSchema

Some Client tools can render forms using JSON Schema

Examples

Simple Example

from marshmallow import Schema, fields
from marshmallow_jsonschema import JSONSchema

class UserSchema(Schema):
    username = fields.String()
    age = fields.Integer()
    birthday = fields.Date()

user_schema = UserSchema()

json_schema = JSONSchema()
json_schema.dump(user_schema)

Yields:

{'properties': {'age': {'format': 'integer',
                        'title': 'age',
                        'type': 'number'},
                'birthday': {'format': 'date',
                             'title': 'birthday',
                             'type': 'string'},
                'username': {'title': 'username', 'type': 'string'}},
 'required': [],
 'type': 'object'}

Nested Example

from marshmallow import Schema, fields
from marshmallow_jsonschema import JSONSchema
from tests import UserSchema


class Athlete(object):
    user_schema = UserSchema()

    def __init__(self):
        self.name = 'sam'


class AthleteSchema(Schema):
    user_schema = fields.Nested(JSONSchema)
    name = fields.String()

    
athlete = Athlete()
athlete_schema = AthleteSchema()

athlete_schema.dump(athlete)

Complete example Flask application using brutisin/json-forms

Screenshot

This example renders a form not dissimilar to how wtforms might render a form.

However rather than rendering the form in python, the JSON Schema is rendered using the javascript library brutusin/json-forms.

from flask import Flask, jsonify
from marshmallow import Schema, fields
from marshmallow_jsonschema import JSONSchema

app = Flask(__name__)


class UserSchema(Schema):
    name = fields.String()
    address = fields.String()


@app.route('/schema')
def schema():
    schema = UserSchema()
    return jsonify(JSONSchema().dump(schema))


@app.route('/')
def home():
    return '''<!DOCTYPE html>
<head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/brutusin.json-forms/1.3.0/css/brutusin-json-forms.css"><Paste>
<script src="https://code.jquery.com/jquery-1.12.1.min.js" integrity="sha256-I1nTg78tSrZev3kjvfdM5A5Ak/blglGzlaZANLPDl3I=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.string/3.3.4/underscore.string.min.js"></script>
<script src="https://cdn.jsdelivr.net/brutusin.json-forms/1.3.0/js/brutusin-json-forms.min.js"></script>
<script>
$(document).ready(function() {
    $.ajax({
        url: '/schema'
        , success: function(data) {
            var container = document.getElementById('myform');
            var BrutusinForms = brutusin["json-forms"];
            var bf = BrutusinForms.create(data);
            bf.render(container);
        }
    });
});
</script>
</head>
<body>
<div id="myform"></div>
</body>
</html>
'''


if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True)

Advanced usage

Custom Type support

Simply add a _jsonschema_type_mapping method to your field so we know how it ought to get serialized to JSON Schema.

A common use case for this is creating a dropdown menu using enum (see Gender below).

class Colour(fields.Field):

    def _jsonschema_type_mapping(self):
        return {
            'type': 'string',
        }

    def _serialize(self, value, attr, obj):
        r, g, b = value
        r = "%02X" % (r,)
        g = "%02X" % (g,)
        b = "%02X" % (b,)
        return '#' + r + g + b 

class Gender(fields.String):
    def _jsonschema_type_mapping(self):
        return {
            'type': 'string',
            'enum': ['Male', 'Female']
        }


class UserSchema(Schema):
    name = fields.String(required=True)
    favourite_colour = Colour()
    gender = Gender()

schema = UserSchema()
json_schema = JSONSchema()
json_schema.dump(schema)

React-JSONSchema-Form Extension

react-jsonschema-form is a library for rendering jsonschemas as a form using React. It is very powerful and full featured.. the catch is that it requires a proprietary uiSchema to provide advanced control how the form is rendered. Here's a live playground

(new in version 0.10.0)

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()
schema = MySchema()

# here's your jsonschema
data = json_schema_obj.dump(schema)

# ..and here's your uiSchema!
ui_schema_json = json_schema_obj.dump_uischema(schema)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

marshmallow_jsonschema_2-0.1.0.tar.gz (18.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

marshmallow_jsonschema_2-0.1.0-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

Details for the file marshmallow_jsonschema_2-0.1.0.tar.gz.

File metadata

  • Download URL: marshmallow_jsonschema_2-0.1.0.tar.gz
  • Upload date:
  • Size: 18.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for marshmallow_jsonschema_2-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2905e560fbbecbb2deed36a5daf6ff66de2e428375cc4a8104a442fc3bfda3c3
MD5 6bafe4e7b5ea6e38183b9fbd2b73a94f
BLAKE2b-256 409029bc200f8caf1c890c5424c051ef760830886f1ca76e5161c65f418712f5

See more details on using hashes here.

Provenance

The following attestation bundles were made for marshmallow_jsonschema_2-0.1.0.tar.gz:

Publisher: publish.yml on JacobChesslo/marshmallow-jsonschema-2

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file marshmallow_jsonschema_2-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for marshmallow_jsonschema_2-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b1b16b68e478f52b1b373c81927559a000b355ed68a866748a2266fb4933cddf
MD5 2ca5f90e55a9a20b26abe491c8f7521d
BLAKE2b-256 697106ebdb422fea80ae5dedc0896b9e59ccc16c9e9c55cab1cea0115adc3f96

See more details on using hashes here.

Provenance

The following attestation bundles were made for marshmallow_jsonschema_2-0.1.0-py3-none-any.whl:

Publisher: publish.yml on JacobChesslo/marshmallow-jsonschema-2

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page