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

better error messages for regular expression validation #42

Closed
wbolster opened this issue Mar 8, 2019 · 4 comments
Closed

better error messages for regular expression validation #42

wbolster opened this issue Mar 8, 2019 · 4 comments

Comments

@wbolster
Copy link
Contributor

wbolster commented Mar 8, 2019

string fields can have a pattern for validation, and since re.search() is used under the hood, the pattern can be either a regular expression as a string or a compiled regular expression (re.compile(...) is cached and can have flags).

so far so good, but the error message uses this:

        "pattern": "Must match the pattern /{pattern}/.",

the str() conversion for compiled regexes looks like code:

>>> my_regex = re.compile('foo')

>>> f"oops: {my_regex}"
"oops: re.compile('foo')"

but the .pattern attribute is a string:

>>> f"oops: {my_regex.pattern}"
'oops: foo'

so, perhaps the error message for regular expressions could be improved. 🌳

@tomchristie
Copy link
Member

Right now the pattern attribute is strictly a string...

https://github.com/encode/typesystem/blob/master/typesystem/fields.py#L106

I'd be very happy to:

  • Accept a PR that switched that to allowing either a string or a regex.
  • Added a pattern_regex property on the String field, which is used to store the compiled pattern.
  • Switch the validation to use the compiled pattern_regex to perform the matching.

So I think __init__ would look something like this...

if pattern is None:
    self.patten is None
    self.pattern_regex is None
elif isinstance(pattern, str):
    self.pattern = pattern
    self.pattern_regex = re.compile(pattern)
elif isinstance(pattern, re.Pattern):
    self.pattern = pattern.pattern
    self.pattern_regex = pattern

@tomchristie
Copy link
Member

tomchristie commented Mar 8, 2019

Similar to this we'll also want beter support for customizing error messages, so that eg...

# Match valid variable names like 'someFunctionName123'
variable_name = typesystem.String(
    pattern="^a-zA-Z_[a-zA-Z0-9_]*$",
    errors={"pattern": "Must be a valid variable name."}
)

But we'll handle that under #28

@wbolster
Copy link
Contributor Author

wbolster commented Mar 8, 2019

i'll cook a pr. it's gonna be simpler than your suggestions.

@tomchristie
Copy link
Member

Sure thing 👍
However you implement, you'll want to make sure that the pattern attribute always gets set to a plain string - we inspect that attribute when generating JSON schema documents.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants