fuhrysteve / marshmallow-jsonschema

JSON Schema Draft v7 (http://json-schema.org/) formatting with marshmallow
MIT License
209 stars 72 forks source link

Introduce ability to modify JSON schema property name #88

Closed yeralin closed 5 years ago

yeralin commented 5 years ago

Addresses #83

Now, a user has an ability to modify JSON schema property name using metadata['name'] parameter.

Ex., consider the following schema:

class ExampleSchema(Schema):
    api_token = String(description='API token.')

Resulting JSON schema would look like this:

{
    "$ref": "#/definitions/ExampleSchema",
    "$schema": "http://json-schema.org/draft-07/schema#",
    "definitions": {
        "ExampleSchema": {
            "additionalProperties": false,
            "properties": {
                "api_token": {
                    "description": "API token.",
                    "title": "api_token",
                    "type": "string"
                }
            },
            "type": "object"
        }
    }
}

If a user desires to modify JSON schema property name (the part "api_token": {), one can simply add name parameter to the schema:

class ExampleSchema(Schema):
    api_token = String(name='api-token', description='API token.')

Which will yield:

{
    "$ref": "#/definitions/ExampleSchema",
    "$schema": "http://json-schema.org/draft-07/schema#",
    "definitions": {
        "ExampleSchema": {
            "additionalProperties": false,
            "properties": {
                "api-token": { <- used metadata['name']
                    "description": "API token.",
                    "title": "api_token",
                    "type": "string"
                }
            },
            "type": "object"
        }
    }
}
coveralls commented 5 years ago

Coverage Status

Coverage remained the same at 100.0% when pulling 5912cdad53bedd0004b5e1572dc82fc47f6eaa18 on yeralin:modify-json-property-name into b29a8afd11d8eb7f0d34a8ad7de72cf102105a89 on fuhrysteve:master.

LukeMarlin commented 5 years ago

Looks alright, had a reason for not merging it @fuhrysteve?

fuhrysteve commented 5 years ago

LGTM! Thanks for the pull