Skip to content
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

Add support for marshmallow Regexp validator #24

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
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]+$'