Skip to content

Commit

Permalink
Handle validate.oneOf(iterable)
Browse files Browse the repository at this point in the history
According to the marshmallow docs for validate.OneOf [1], the enum
choices and labels are iterables, not necessarily lists.

[1]: http://marshmallow.readthedocs.io/en/latest/api_reference.html#marshmallow.validate.OneOf
  • Loading branch information
erik committed Mar 10, 2018
1 parent f6c2dae commit 0907857
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions marshmallow_jsonschema/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ def handle_one_of(schema, field, validator, parent_schema):
altered.
"""
if validator.choices:
schema['enum'] = validator.choices
schema['enumNames'] = validator.labels
schema['enum'] = list(validator.choices)
schema['enumNames'] = list(validator.labels)

return schema

Expand Down
19 changes: 19 additions & 0 deletions tests/test_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,3 +440,22 @@ class TestSchema(Schema):
'type': 'string',
'description': 'Directly on the field!',
}

def test_dumps_iterable_enums():
mapping = {'a': 0, 'b': 1, 'c': 2}

class TestSchema(Schema):
foo = fields.Integer(validate=validate.OneOf(
mapping.values(), labels=mapping.keys()))

schema = TestSchema()
json_schema = JSONSchema()
dumped = json_schema.dump(schema).data

assert dumped['definitions']['TestSchema']['properties']['foo'] == {
'enum': [0, 1, 2],
'enumNames': ['a', 'b', 'c'],
'format': 'integer',
'title': 'foo',
'type': 'number'
}

0 comments on commit 0907857

Please sign in to comment.