-
Notifications
You must be signed in to change notification settings - Fork 44
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
Comments
Right now the https://github.com/encode/typesystem/blob/master/typesystem/fields.py#L106 I'd be very happy to:
So I think 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 |
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 |
i'll cook a pr. it's gonna be simpler than your suggestions. |
Sure thing 👍 |
string fields can have a
pattern
for validation, and sincere.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:
the
str()
conversion for compiled regexes looks like code:but the
.pattern
attribute is a string:so, perhaps the error message for regular expressions could be improved. 🌳
The text was updated successfully, but these errors were encountered: