Skip to content

Document field's metadata usage #98

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,27 @@ if __name__ == '__main__':


### Advanced usage
#### Customizing a field
When defining a `field` in a `Schema`, it is possible to override the `field`'s JSONSchema properties using the `metadata` param.
For example, to give a description to a field:

```python
class MySchema(Schema):
myfield = fields.String(metadata={'description': 'My description'})
```

As noted before, this also allows overriding as well as providing additional properties:

```python
>>>class MySchema(Schema):
... myfield = fields.String(metadata={'unkown_prop':'value', "type":"integer"}, required=True)
>>>JSONSchema().dump(MySchema())
{'$schema': 'http://json-schema.org/draft-07/schema#', 'definitions': {'MySchema': {'properties': {'myfield': {'title': 'myfield', 'type': 'integer', 'unkown_prop': 'value'}}, 'required': ['myfield'], 'type': 'object', 'additionalProperties': False}}, '$ref': '#/definitions/MySchema'}
```

In the above example, we added some unspecified field AND we also changed the type entirely.


#### Custom Type support

Simply add a `_jsonschema_type_mapping` method to your field
Expand Down