-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #266 from rmspeers/x-anyOf
Setup for handling anyOf simplifying to oneOf on import.
- Loading branch information
Showing
3 changed files
with
98 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import pytest | ||
|
||
import python_jsonschema_objects as pjo | ||
|
||
|
||
def test_simple_array_anyOf(): | ||
basicSchemaDefn = { | ||
"$schema": "http://json-schema.org/draft-04/schema#", | ||
"title": "Test", | ||
"properties": {"ExampleAnyOf": {"$ref": "#/definitions/exampleAnyOf"}}, | ||
"required": ["ExampleAnyOf"], | ||
"type": "object", | ||
"definitions": { | ||
"exampleAnyOf": { | ||
# "type": "string", "format": "email" | ||
"anyOf": [ | ||
{"type": "string", "format": "email"}, | ||
{"type": "string", "maxlength": 0}, | ||
] | ||
} | ||
}, | ||
} | ||
|
||
builder = pjo.ObjectBuilder(basicSchemaDefn) | ||
|
||
ns = builder.build_classes(any_of="use-first") | ||
ns.Test().from_json('{"ExampleAnyOf" : "[email protected]"}') | ||
|
||
with pytest.raises(pjo.ValidationError): | ||
# Because string maxlength 0 is not selected, as we are using the first validation in anyOf: | ||
ns.Test().from_json('{"ExampleAnyOf" : ""}') | ||
# Because this does not match the email format: | ||
ns.Test().from_json('{"ExampleAnyOf" : "not-an-email"}') | ||
|
||
# Does it also work when not deserializing? | ||
x = ns.Test() | ||
with pytest.raises(pjo.ValidationError): | ||
x.ExampleAnyOf = "" | ||
|
||
with pytest.raises(pjo.ValidationError): | ||
x.ExampleAnyOf = "not-an-email" | ||
|
||
x.ExampleAnyOf = "[email protected]" | ||
out = x.serialize() | ||
y = ns.Test.from_json(out) | ||
assert y.ExampleAnyOf == "[email protected]" | ||
|
||
|
||
def test_simple_array_anyOf_withoutConfig(): | ||
basicSchemaDefn = { | ||
"$schema": "http://json-schema.org/draft-04/schema#", | ||
"title": "Test", | ||
"properties": {"ExampleAnyOf": {"$ref": "#/definitions/exampleAnyOf"}}, | ||
"required": ["ExampleAnyOf"], | ||
"type": "object", | ||
"definitions": { | ||
"exampleAnyOf": { | ||
# "type": "string", "format": "email" | ||
"anyOf": [ | ||
{"type": "string", "format": "email"}, | ||
{"type": "string", "maxlength": 0}, | ||
] | ||
} | ||
}, | ||
} | ||
|
||
builder = pjo.ObjectBuilder(basicSchemaDefn) | ||
|
||
with pytest.raises(NotImplementedError): | ||
builder.build_classes() |