From 8fc9c3789ea58f2fa9d9cb3f6ee0574b4c555061 Mon Sep 17 00:00:00 2001 From: Axmacher Date: Mon, 30 Jan 2017 16:36:04 -0500 Subject: [PATCH] Add support for marshmallow Regexp validator --- marshmallow_jsonschema/base.py | 4 +++- marshmallow_jsonschema/validation.py | 26 ++++++++++++++++++++++++++ tests/__init__.py | 1 + tests/test_dump.py | 7 +++++++ 4 files changed, 37 insertions(+), 1 deletion(-) diff --git a/marshmallow_jsonschema/base.py b/marshmallow_jsonschema/base.py index fbda2aa..6e4c6ed 100644 --- a/marshmallow_jsonschema/base.py +++ b/marshmallow_jsonschema/base.py @@ -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'] @@ -73,6 +74,7 @@ validate.Length: handle_length, validate.OneOf: handle_one_of, validate.Range: handle_range, + validate.Regexp: handle_regexp } diff --git a/marshmallow_jsonschema/validation.py b/marshmallow_jsonschema/validation.py index 4a40b61..36c023e 100644 --- a/marshmallow_jsonschema/validation.py +++ b/marshmallow_jsonschema/validation.py @@ -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 diff --git a/tests/__init__.py b/tests/__init__.py index cdaae84..3a153e0 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -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): diff --git a/tests/test_dump.py b/tests/test_dump.py index 039c72f..95a21d8 100644 --- a/tests/test_dump.py +++ b/tests/test_dump.py @@ -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]+$'