-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
plugin.api.validate: ListSchema, re.Pattern and NoneOrAllSchema #4691
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
plugin.api.validate: ListSchema, re.Pattern and NoneOrAllSchema #4691
Conversation
The linting failure is caused by a new major version of flake8 with unrelated linting errors. Will take a look at this later and then rebase here once fixed. |
In contrast to the `list` instance validation where sequence subsets get validated, the `ListSchema` validates that the input list has the same length and that all list items validate successfully according to the defined schemas.
Use the `search()` method and return `None` or a `re.Match` instance. This avoids having to explicitly define the validation pattern ```py validate.transform(re.compile(...).search) ```
de6b0ce
to
4a4e2e8
Compare
This helps avoiding the validation pattern ```py validate.any(None, validate.all(...)) ``` and is useful for `re.Pattern` and `xml_xpath_string` validations, where the validation result can be `None` and is usually used for gracefully returning no validation results instead of raising an error.
4a4e2e8
to
9ddfa2f
Compare
These changes look really good @bastimeyer, lowering the complexity is always appreciated. Thank you!
Based on what the code does and the description you've added doc wise, what about changing |
I was talking about renaming the I was thinking about maybe renaming it to
|
Yeah I don't think this is is very helpful. I don't think it's worth stressing about too much, I can't think of much better than |
Let's keep the There's one minor concern I have with the Btw, I've already modified, fixed and rewritten 52 plugins based on these new validation changes the last couple of days. Not sure though how I will submit these changes, as there are a couple of complete rewrites that shouldn't be included in commits of basic/simple validation pattern changes. |
What about only breaking out the full rewrites in to separate PRs, and all the small fixes in to one PR? Seems like a hassle to have a PR for every small fix just because it's a different plugin. |
That's what I was planning to do. |
Any further comments? If not, then please merge, so I can submit (some of) the plugin changes I was talking about. |
In contrast to the `list` instance validation where sequence subsets get validated, the `ListSchema` validates that the input list has the same length and that all list items validate successfully according to the defined schemas.
This helps avoiding the validation pattern ```py validate.any(None, validate.all(...)) ```
In contrast to the `list` instance validation where sequence subsets get validated, the `ListSchema` validates that the input list has the same length and that all list items validate successfully according to the defined schemas.
This helps avoiding the validation pattern ```py validate.any(None, validate.all(...)) ```
This adds new schema validation methods that are useful for commonly used patterns.
ListSchema
The list instance validation schema (eg.
[int, str]
) only validates a subset of the input, which means it defines any items that can match. This is often not what plugin authors have in mind when trying to validate a list where each indexed item needs to be validated.The
ListSchema
(viavalidate.list(...)
) on the other hand therefore checks for equal length of the input list and then validates each item according to the schema.re.Pattern
Instead of having to define the
validate.transform(re.compile(...).search)
validation pattern, a simplere.compile(...)
is now supported, which will call thesearch()
method internally and return eitherNone
or an instance ofre.Match
. Other methods likefindall()
for example can still be called viavalidate.transform()
.NoneOrAllSchema
This avoids having to repeatedly define the
validate.any(None, validate.all(...))
validation pattern via the newly addedvalidate.none_or_all(...)
schema. This is especially useful forre.Pattern
orvalidate.xml_xpath_string(...)
validations.I am not really happy with the naming of this though. Any better suggestions than
none_or_all
?