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

Improve introspection of validators #533

Merged
merged 14 commits into from
Sep 28, 2021
Merged

Conversation

ngnpope
Copy link
Contributor

@ngnpope ngnpope commented Sep 23, 2021

This resolves the bug in #388 which I have also bumped into.
There weren't many tests covering validator introspection, so I've added a lot and it should be quite robust now.

I also made use of exclusiveMaximum and exclusiveMinimum when using max_digits to construct minimum and maximum for decimals as they are not going to be inclusive, i.e. max_digits = 3 and decimal_places = 2###.##, which is 1000 < x < 1000 and not 1000 ≤ x ≤ 1000.

Other than that, introspection of validators for child fields was also fixed, some missing validators were added, missing minProperties and maxProperties are now handled and tests for various subclasses of validators too.

@codecov
Copy link

codecov bot commented Sep 23, 2021

Codecov Report

Merging #533 (470878b) into master (bbc6023) will increase coverage by 0.05%.
The diff coverage is 99.53%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #533      +/-   ##
==========================================
+ Coverage   98.66%   98.71%   +0.05%     
==========================================
  Files          56       57       +1     
  Lines        6122     6292     +170     
==========================================
+ Hits         6040     6211     +171     
+ Misses         82       81       -1     
Impacted Files Coverage Δ
drf_spectacular/openapi.py 97.20% <98.52%> (+0.23%) ⬆️
tests/test_regressions.py 100.00% <100.00%> (ø)
tests/test_validators.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update bbc6023...470878b. Read the comment docs.

@ngnpope ngnpope force-pushed the validator-fixes branch 2 times, most recently from b9029ec to 6d8c650 Compare September 26, 2021 23:18
@ngnpope
Copy link
Contributor Author

ngnpope commented Sep 27, 2021

Hi @tfranzel. I think this one should be done now. Let me know if there is anything else that needs addressing.

Copy link
Owner

@tfranzel tfranzel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ngnpope thank you for this top-notch PR! outstanding work! really happy about this.

just have a few minors.

you basically refactored the last remnants of the original upstream implementation, which were a pending todo for quite some time now. that's also the reason why the method names were not really aligned with the rest of the code.

tests/test_fields.yml Show resolved Hide resolved
drf_spectacular/openapi.py Show resolved Hide resolved
tests/test_regressions.py Outdated Show resolved Hide resolved
tests/test_regressions.py Outdated Show resolved Hide resolved
drf_spectacular/openapi.py Show resolved Hide resolved
tests/test_validators.py Outdated Show resolved Hide resolved
drf_spectacular/openapi.py Outdated Show resolved Hide resolved
Use `FloatField` as `maximum` is only valid with `numeric` type. As we
can't use `DecimalField` because its `max_digits` and `decimal_places`
attributes take precedence over those of the `DecimalValidator`, we just
need another closely compatible field type.
Ensure that only validation keywords and values that are suitable for
the detected type are used, e.g. `maximum` isn't valid for `type` of
`string`.

See the following links for details:

- https://datatracker.ietf.org/doc/html/draft-wright-json-schema-validation-00#section-4
- https://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.6

Closes tfranzel#338.
For example, if `IntegerField` has a `min_value` of `10`, we don't want
a `MinValueValidator` with a value of `5` to override what has already
been assigned to `minimum`.
If we are have max digits set to 4 and decimal places set to 1, we
expect the maximum value to be 999.9, but we are setting it to be 1000.
In this case we expect the maximum bound to be exclusive, not inclusive
as is the default.

Obviously, if we encounter another non-DecimalValidator that would
override, we'd need to remove the exclusive flag.

Note: This will need tweaking for OpenAPI 3.1 which uses a newer version
of the JSON Schema specification. In that case `exclusiveMaximum` and
`exclusiveMinimum` are no longer boolean values and should replace the
`maximum` and `minimum` properties instead.
@ngnpope
Copy link
Contributor Author

ngnpope commented Sep 28, 2021

@ngnpope thank you for this top-notch PR! outstanding work! really happy about this.

You're welcome 🙂 I've made the requested changes.

@tfranzel tfranzel merged commit 37ee061 into tfranzel:master Sep 28, 2021
@ngnpope ngnpope deleted the validator-fixes branch September 28, 2021 11:30
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

Successfully merging this pull request may close these issues.

2 participants