Skip to content

Commit

Permalink
Fix field.Object(pattern_project={}) pattern definition, closes #158
Browse files Browse the repository at this point in the history
  • Loading branch information
lyschoening committed Jan 10, 2019
1 parent 66f11dc commit 4686d97
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
2 changes: 2 additions & 0 deletions flask_potion/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ def __init__(self, properties=None, pattern=None, pattern_properties=None, addit

if isinstance(pattern_properties, (type, Raw)):
self.pattern_properties = _field_from_object(self, pattern_properties)
elif isinstance(pattern_properties, dict):
self.pattern_properties = {p: _field_from_object(self, f) for p, f in pattern_properties.items()}

def schema():
request = {"type": "object"}
Expand Down
17 changes: 15 additions & 2 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,18 @@ def test_object_format_any(self):
def test_object_convert_properties(self):
pass

def test_object_convert_pattern(self):
def test_object_pattern_schema(self):
o = fields.Object(fields.Integer, pattern="[A-Z][0-9]+")

self.assertEqual({"A3": 1, "B12": 2}, o.convert({"A3": 1, "B12": 2}))
self.assertEqual({
"type": "object",
"additionalProperties": False,
"patternProperties": {
"[A-Z][0-9]+": {"type": "integer"}
}
}, o.response)

o = fields.Object(pattern_properties={"[A-Z][0-9]+": fields.Integer})

self.assertEqual({
"type": "object",
Expand All @@ -297,6 +305,11 @@ def test_object_convert_pattern(self):
}
}, o.response)

def test_object_convert_pattern(self):
o = fields.Object(fields.Integer, pattern="[A-Z][0-9]+")

self.assertEqual({"A3": 1, "B12": 2}, o.convert({"A3": 1, "B12": 2}))

with self.assertRaises(ValidationError):
o.convert({"A2": "string"})

Expand Down

0 comments on commit 4686d97

Please sign in to comment.