Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fuhrysteve/marshmallow-jsonschema
JSON Schema Draft v7 (http://json-schema.org/) formatting with marshmallow
https://github.com/fuhrysteve/marshmallow-jsonschema
json-schema marshmallow marshmallow-jsonschema react-jsonschema-form
Last synced: 5 days ago
JSON representation
JSON Schema Draft v7 (http://json-schema.org/) formatting with marshmallow
- Host: GitHub
- URL: https://github.com/fuhrysteve/marshmallow-jsonschema
- Owner: fuhrysteve
- License: mit
- Created: 2015-12-03T21:44:46.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2024-04-02T15:07:05.000Z (11 months ago)
- Last Synced: 2025-01-30T12:07:10.948Z (12 days ago)
- Topics: json-schema, marshmallow, marshmallow-jsonschema, react-jsonschema-form
- Language: Python
- Homepage:
- Size: 231 KB
- Stars: 208
- Watchers: 10
- Forks: 71
- Open Issues: 61
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGES
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
- starred-awesome - marshmallow-jsonschema - JSON Schema Draft v4 (http://json-schema.org/) formatting with marshmallow (Python)
README
## marshmallow-jsonschema: JSON Schema formatting with marshmallow
![Build Status](https://github.com/fuhrysteve/marshmallow-jsonschema/workflows/build/badge.svg)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/python/black)marshmallow-jsonschema translates marshmallow schemas into
JSON Schema Draft v7 compliant jsonschema. See http://json-schema.org/#### 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.6 and marshmallow>=3.11. (For python 2 & marshmallow 2 support, please use marshmallow-jsonschema<0.11)
```
pip install marshmallow-jsonschema
```#### Some Client tools can render forms using JSON Schema
* [react-jsonschema-form](https://github.com/mozilla-services/react-jsonschema-form) (recommended)
* See below extension for this excellent library!
* https://github.com/brutusin/json-forms
* https://github.com/jdorn/json-editor
* https://github.com/ulion/jsonform### Examples
#### Simple Example
```python
from marshmallow import Schema, fields
from marshmallow_jsonschema import JSONSchemaclass UserSchema(Schema):
username = fields.String()
age = fields.Integer()
birthday = fields.Date()user_schema = UserSchema()
json_schema = JSONSchema()
json_schema.dump(user_schema)
```Yields:
```python
{'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
```python
from marshmallow import Schema, fields
from marshmallow_jsonschema import JSONSchema
from tests import UserSchemaclass 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](http://i.imgur.com/jJv1wFk.png)
This example renders a form not dissimilar to how [wtforms](https://github.com/wtforms/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](https://github.com/brutusin/json-forms).```python
from flask import Flask, jsonify
from marshmallow import Schema, fields
from marshmallow_jsonschema import JSONSchemaapp = 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 '''$(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);
}
});
});