Skip to content

fix(validation): handle_one_of uses oneOf instead of enum #95

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
Show file tree
Hide file tree
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: 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 @@ -486,8 +486,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 @@ -53,17 +53,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 @@ -76,8 +70,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"}]


def test_range():
Expand Down