Skip to content

Commit

Permalink
fix(validation): handle_one_of uses oneOf instead of enum
Browse files Browse the repository at this point in the history
enumNames is not part of the JSON Schema spec, but it is possible to get the same behaviour using a
oneOf with titles
  • Loading branch information
jgroth committed Oct 9, 2019
1 parent 37d99bb commit 809233f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 18 deletions.
21 changes: 18 additions & 3 deletions marshmallow_jsonschema/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def handle_length(schema, field, validator, parent_schema):

def handle_one_of(schema, field, validator, parent_schema):
"""Adds the validation logic for ``marshmallow.validate.OneOf`` by setting
the JSONSchema `enum` property to the allowed choices in the validator.
the JSONSchema `oneOf` property to the allowed choices in the validator.
Args:
schema (dict): The original JSON schema we generated. This is what we
Expand All @@ -69,8 +69,23 @@ def handle_one_of(schema, field, validator, parent_schema):
dict: New JSON Schema that has been post processed and
altered.
"""
schema["enum"] = list(validator.choices)
schema["enumNames"] = list(validator.labels)
if schema["type"] not in ["string", "number"]:
return schema

if "oneOf" in schema:
return schema

choices = [
field._serialize(choice, field.name, None) for choice in validator.choices
]
if not choices:
return schema

labels = validator.labels if validator.labels else choices
schema["oneOf"] = [
{"type": schema["type"], "title": label, "const": choice}
for choice, label in zip(choices, labels)
]

return schema

Expand Down
6 changes: 4 additions & 2 deletions tests/test_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,10 @@ class TestSchema(Schema):
dumped = validate_and_dump(schema)

assert dumped["definitions"]["TestSchema"]["properties"]["foo"] == {
"enum": [v for v in mapping.values()],
"enumNames": [k for k in mapping.keys()],
"oneOf": [
{"type": "number", "title": k, "const": v}
for k, v in zip(mapping.keys(), mapping.values())
],
"format": "integer",
"title": "foo",
"type": "number",
Expand Down
47 changes: 34 additions & 13 deletions tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,11 @@ def test_one_of_validator():

dumped = validate_and_dump(schema)

assert dumped["definitions"]["UserSchema"]["properties"]["sex"]["enum"] == [
"male",
"female",
"non_binary",
"other",
]
assert dumped["definitions"]["UserSchema"]["properties"]["sex"]["enumNames"] == [
"Male",
"Female",
"Non-binary/fluid",
"Other",
assert dumped["definitions"]["UserSchema"]["properties"]["sex"]["oneOf"] == [
{"type": "string", "title": "Male", "const": "male"},
{"type": "string", "title": "Female", "const": "female"},
{"type": "string", "title": "Non-binary/fluid", "const": "non_binary"},
{"type": "string", "title": "Other", "const": "other"},
]


Expand All @@ -63,8 +57,35 @@ class TestSchema(Schema):
dumped = validate_and_dump(schema)

foo_property = dumped["definitions"]["TestSchema"]["properties"]["foo"]
assert foo_property["enum"] == []
assert foo_property["enumNames"] == []
assert "oneOf" not in foo_property


def test_one_of_object():
class TestSchema(Schema):
foo = fields.Dict(validate=OneOf([{"a": 1}]))

schema = TestSchema()

dumped = validate_and_dump(schema)

foo_property = dumped["definitions"]["TestSchema"]["properties"]["foo"]
assert "oneOf" not in foo_property


def test_one_of_custom_field():
class CustomField(fields.String):
def _jsonschema_type_mapping(self):
return {"type": "string", "oneOf": [{"const": "one"}, {"const": "two"}]}

class TestSchema(Schema):
foo = CustomField(validate=OneOf(["one", "two"]))

schema = TestSchema()

dumped = validate_and_dump(schema)

foo_property = dumped["definitions"]["TestSchema"]["properties"]["foo"]
assert foo_property["oneOf"] == [{"const": "one"}, {"const": "two"}]


@pytest.mark.skipif(MARSHMALLOW_3, reason="marshmallow 2 only")
Expand Down

0 comments on commit 809233f

Please sign in to comment.