Skip to content

Commit

Permalink
Add support for marshmallow Regexp validator
Browse files Browse the repository at this point in the history
  • Loading branch information
Axmacher authored and Axmacher committed Jan 30, 2017
1 parent 6f51771 commit 8fc9c37
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
4 changes: 3 additions & 1 deletion marshmallow_jsonschema/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from marshmallow.class_registry import get_class
from marshmallow.compat import text_type, binary_type, basestring

from .validation import handle_length, handle_one_of, handle_range
from .validation import (handle_length, handle_one_of, handle_range,
handle_regexp)


__all__ = ['JSONSchema']
Expand Down Expand Up @@ -73,6 +74,7 @@
validate.Length: handle_length,
validate.OneOf: handle_one_of,
validate.Range: handle_range,
validate.Regexp: handle_regexp
}


Expand Down
26 changes: 26 additions & 0 deletions marshmallow_jsonschema/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,29 @@ def handle_range(schema, field, validator, parent_schema):
schema['exclusiveMaximum'] = True

return schema


def handle_regexp(schema, field, validator, parent_schema):
"""Adds validation logic for ``marshmallow.validate.Regexp``, setting the
values appropriately for ``fields.String`` and its subclasses.
Args:
schema (dict): The original JSON schema we generated. This is what we
want to post-process.
field (fields.Field): The field that generated the original schema and
who this post-processor belongs to.
validator (marshmallow.validate.Regexp): The validator attached to the
passed in field.
parent_schema (marshmallow.Schema): The Schema instance that the field
belongs to.
Returns:
dict: A, possibly, new JSON Schema that has been post processed and
altered.
"""
if not isinstance(field, fields.String):
return schema

if validator.regex and getattr(validator.regex, 'pattern', None):
schema['pattern'] = validator.regex.pattern
return schema
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class UserSchema(Schema):
validate=validate.Length(min=1, max=3))
github = fields.Nested(GithubProfile)
const = fields.String(validate=validate.Length(equal=50))
hex_number = fields.String(validate=validate.Regexp('^[a-fA-F0-9]+$'))


class BaseTest(unittest.TestCase):
Expand Down
7 changes: 7 additions & 0 deletions tests/test_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,10 @@ class UserSchema(Schema):
json_schema = JSONSchema()
dumped = json_schema.dump(schema).data
assert dumped['properties']['favourite_colour'] == {'type': 'string'}

def test_regexp_validator():
schema = UserSchema()
json_schema = JSONSchema()
dumped = json_schema.dump(schema).data
_validate_schema(dumped)
assert dumped['properties']['hex_number']['pattern'] == '^[a-fA-F0-9]+$'

0 comments on commit 8fc9c37

Please sign in to comment.