All Projects → fuhrysteve → Marshmallow Jsonschema

fuhrysteve / Marshmallow Jsonschema

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

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Marshmallow Jsonschema

Apispec
A pluggable API specification generator. Currently supports the OpenAPI Specification (f.k.a. the Swagger specification)..
Stars: ✭ 831 (+383.14%)
Mutual labels:  json-schema, marshmallow
Go Jsonschema
A tool to generate Go data types from JSON Schema definitions.
Stars: ✭ 164 (-4.65%)
Mutual labels:  json-schema
Libvirt Hook Qemu
Libvirt hook for setting up iptables port-forwarding rules when using NAT-ed networking.
Stars: ✭ 137 (-20.35%)
Mutual labels:  json-schema
Skillset
✨ Intuitive job-candidate skill visualization, taking advantage of D3.js and JSONResume.
Stars: ✭ 152 (-11.63%)
Mutual labels:  json-schema
Apifairy
A minimalistic API framework built on top of Flask, Marshmallow and friends.
Stars: ✭ 141 (-18.02%)
Mutual labels:  marshmallow
Ngx Formly
JSON powered / Dynamic forms for Angular
Stars: ✭ 2,109 (+1126.16%)
Mutual labels:  json-schema
React Json Schema
Configure and build views using JSON schemas mapped to React components
Stars: ✭ 131 (-23.84%)
Mutual labels:  json-schema
Json Schema To Openapi Schema
A little NodeJS package to convert JSON Schema to OpenAPI Schema Objects
Stars: ✭ 168 (-2.33%)
Mutual labels:  json-schema
Flama
🔥 Fire up your API with this flamethrower
Stars: ✭ 161 (-6.4%)
Mutual labels:  marshmallow
Jsonschema2db
Generate tables dynamically from a JSON Schema and insert data
Stars: ✭ 152 (-11.63%)
Mutual labels:  json-schema
Ajv Cli
Use 'Another Json Validator' (ajv) from the command line
Stars: ✭ 148 (-13.95%)
Mutual labels:  json-schema
Dms
基于Json Schema的动态Json数据配置平台
Stars: ✭ 142 (-17.44%)
Mutual labels:  json-schema
Raml Server
run a mocked server JUST based on a RAML API's definition .. zero coding
Stars: ✭ 158 (-8.14%)
Mutual labels:  json-schema
Kubernetes Json Schema
Schemas for every version of every object in every version of Kubernetes
Stars: ✭ 140 (-18.6%)
Mutual labels:  json-schema
Liform React
Generate forms from JSON Schema to use with React (& redux-form)
Stars: ✭ 167 (-2.91%)
Mutual labels:  json-schema
Json Schema To Typescript
Compile JSONSchema to TypeScript type declarations
Stars: ✭ 1,917 (+1014.53%)
Mutual labels:  json-schema
Json Schema Viewer
JavaScript tool for visualizing json-schemas
Stars: ✭ 147 (-14.53%)
Mutual labels:  json-schema
Cfworker
A collection of packages optimized for Cloudflare Workers and service workers.
Stars: ✭ 152 (-11.63%)
Mutual labels:  json-schema
Aiohttp Apispec
Build and document REST APIs with aiohttp and apispec
Stars: ✭ 172 (+0%)
Mutual labels:  marshmallow
Newtonsoft.json.schema
Json.NET Schema is a powerful, complete and easy to use JSON Schema framework for .NET
Stars: ✭ 167 (-2.91%)
Mutual labels:  json-schema

marshmallow-jsonschema: JSON Schema formatting with marshmallow

Build Status Coverage Status Code style: 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. (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

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)
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].