Skip to content
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

[marshmallow plugin] Inspect validators #66

sloria opened this issue Apr 13, 2016 · 3 comments


Copy link

commented Apr 13, 2016

Many of marshmallow's validators can be inspected to provide additional data when constructing OpenAPI entities.

from marshmallow import fields, validate
from apispec.ext.marshmallow.swagger import field2property

field = fields.Int(validate=validate.Range(min=3, max=42))

prop = field2property(field)
assert prop['minimum'] == 3
assert prop['maximum'] == 42

This comment has been minimized.

Copy link

commented May 2, 2016

There are 3 objects in the OpenAPI spec that support validation fields:

The header object only supports simple types, so there is no marshmallow schema to validate.

Matching validators:

Marshmallow Validator OpenAPI Validator
Email pattern (specific)
Equal enum (single)
Length (list?) minItems, maxItems
Length (*) minLength, maxLength
OneOf enum
Range minimum, maximum
Regexp pattern
URL pattern (specific)

Unsupported marshmallow validators:

Marshmallow Validator OpenAPI Validator
ContainsOnly (maybe enum?)
Validator (user defined?)

It should be possible to design this mapping in such a way that user defined marshmallow validators could register their own OpenAPI validator mapping with apispec.

Unsupported OpenAPI validators:

Marshmallow Validator OpenAPI Validator

Maybe there should be an issue for marshmallow to support these validators, so that there can be a subset of marshmallow validators that completely cover the OpenAPI spec.


This comment has been minimized.

Copy link

commented Feb 18, 2017

allow_none is somehow related, although it is not a Validator.

It would map to nullable, except nullable does not exist... yet. There seems to be a wide consensus upon the use of x-nullable until nullable is added (OpenAPI 3). It has been integrated in ReDoc, for instance.

Should we introduce x-nullable in field2property?

Right now, the workaround is to specify both allow_none=True and x_nullable=True in the model.


This comment has been minimized.

Copy link
Member Author

commented Mar 3, 2017

Closing this for now. Feel free to reopen if this requires further discussion.

@sloria sloria closed this Mar 3, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
3 participants
You can’t perform that action at this time.