Commit Graph

42 Commits

Author SHA1 Message Date
Eric Jolibois 4680940146 fix: keep the order of the fields when validate_assignment is set (#2075)
Following #2000 and #2046 we can't `pop` the current value when assigning
a new one (which was probably the most efficient) as we want to keep the
order in the `__dict__`.
So let's do a shallow copy of the `__dict__` without the current value

fix #2073
2020-10-31 18:47:22 +00:00
John Sabath 6b1a0bff54 Prevent fields from being deleted from BaseModel.__dict__ when a field validator raises an unexpected exception (#2046)
* don't pop the value off __dict__

* adding change description

* still pop the field, but catch any exc, set the field to og value, and reraise exc

* adding tests

* fixing test

* improve tests/test_validators.py tests

Co-authored-by: John Sabath <john.sabath@equipmentshare.com>
Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
2020-10-28 19:21:26 +00:00
me-ransh f2af9f7806 Fix #1999: call validator with correct "values" type in setattr (#2000)
* call validator with correct "values" type in BaseModel.__setattr__

* Fix code review comments
- use `self.__dict__` as the values parameter
- refine changed markdown

Co-authored-by: Ran Shaham <ransh@mobileye.com>
2020-10-25 19:18:56 +00:00
Tyler Wozniak 3e4c1b5714 Adding deque to valid field types (#1935)
* Adding deque to valid field types

* Added missing type example

* Fix bad copy-paste

Co-authored-by: PrettyWood <em.jolibois@gmail.com>

* correct enum validator name

* correct enum validator name, take2

Co-authored-by: PrettyWood <em.jolibois@gmail.com>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2020-10-25 17:52:34 +00:00
beezee e3c5e1d3cd aggregate root validation errors (#1586)
* aggregate root validation errors

* add changelog

* update language in docs around root validators and prior failure

* factor out unique_list
2020-07-03 20:47:04 +01:00
ll H 8aebba2c35 check ModelField().validate_always when inheriting (#1545)
fix #1155

* fix issure #1155

* add changes.1545-dcHHH.md

* improve change description

Co-authored-by: dchhh <hudacong@geetest.com>
Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
2020-06-29 11:04:44 +01:00
Daniel Burkhardt Cerigo 913025ac3f FIX: validation on model attribute with nested Literal breaks (#1364)
* Add tests for nested literals validator

* Implement flatten literal in validator

* Add test for flatten literal

* Add changelog entry

* Add test skip markers if not Literal

* Refactor: use improved all literals implementation

From Github user PrettyWood, see PR https://github.com/samuelcolvin/pydantic/pull/1364

* Add testing for typing module

Includes moving corresponding tests.

* Remove unnecessary type hint

* Move all literals test to test_utils
2020-05-23 13:01:43 +01:00
Stephen Brown II a5b0e741e5 Update pytest and pytest sugar, fix test (#1312)
Bumps [pytest](https://github.com/pytest-dev/pytest) from 5.3.5 to 5.4.0.
- [Release notes](https://github.com/pytest-dev/pytest/releases)
- [Changelog](https://github.com/pytest-dev/pytest/blob/master/CHANGELOG.rst)
- [Commits](https://github.com/pytest-dev/pytest/compare/5.3.5...5.4.0)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
Update pytest and pytest-sugar, fix test for

- https://github.com/Teemu/pytest-sugar/pull/188 makes pytest-sugar compatible with pytest 5.4+
- pytest 5.4.0 flipped the signs for assert statement display, so this flips the string to match.
Fix test_fastapi.sh permissions
2020-04-30 13:11:02 +01:00
Samuel Colvin 0b5ccffc76 Fix argument in validators during assignment, fix #1172 (#1174) 2020-01-17 16:30:21 +00:00
Aviram Hassan 5510a13f6c Added optional for root_validator to be skipped if values validation fails (#1050)
* Added optional for root_validator to be skipped if values validation fails

* cleaner usage of skip_on_failure

* skip_on_failure: documentation update
2019-12-16 11:15:06 +00:00
Sebastián Ramírez d9bbb05a16 Implement Optional required (#1031)
* Implement Optional required, when creating a ModelField(required=True), make it persist

* Add test for nullable required

* Improve formatting of Undefined custom object

* Refactor field infer/creation with Undefined to make it idempotent

Needed for when _type_analysis is re-run in Generics

* Add PR changes

* Increment/update tests with code review

* Update/refactor Undefined implementation with code review

* Fix BoolUndefined as string type for mypy, not runtime

* Add docs about required Optional

* Add explicit tests for Any

* Apply code review requested changes

* move tests out of test_validators.py
2019-11-28 16:48:33 +00:00
dmontagu c71326d4a6 Add better support for validator reuse (#941)
* Add better support for validator reuse

* Clean up classmethod unpacking

* Add changes

* Fix coverage check

* Make 3.8 compatible

* Update changes/940-dmontagu.md

Co-Authored-By: Samuel Colvin <s@muelcolvin.com>

* Make allow_reuse discoverable by adding to error message

* switch _check_validator_name to _prepare_validator
2019-11-25 15:15:05 +00:00
Samuel Colvin 87fb5cce12 change CI to 3.8 (#902)
* change CI to 3.8

* fix linting

* travis jobs for 3.8

* redo noqa comments

* add 3.8 to azure
2019-10-17 10:01:27 +01:00
Samuel Colvin 6e5a1363cc V0.32 merge (#852)
* fix(validate-assignment): do not validate extra fields when `vaidate_assignment` is on

* Update history

* Fix `value` vs. `value_`

* Add tests for `value` vs `value_` case

* uprev

* Fix generic required (#742)

* Fix required fields on GenericModel

* lint

* version up

* __post_init__ with inheritance, fix #739 (#740)

* Fix custom Schema on GenericModel fields (#754)

* Fix custom Schema on GenericModel fields

* Add PR#

* uprev

* Fix error messages for Literal types with multiple allowed values (#770)

* Fix error messages for Literal types with multiple allowed values

* Incorporate feedback

* update history
2019-10-01 17:22:05 +01:00
Samuel Colvin 1d0d98ba19 Root validators (#817)
* root validators and rename __obj__ -> __root__

* implement root validation

* tweak Validator

* dataclass and generic tests, docs

* repeat and signature checks

* fix inheritance

* tweaks tests and var names

* improvements to 'from_orm' to work better with root validators (#822)

* improvements to 'from_orm' to work better with root validators

* cython compatibility and tweaks

* tweak config order

* added test for derived classes using custom getter_dict config (#833)

* added test for derived classes using custom getter_dict config

* fix linting

* fix formatting

* cleanup
2019-09-30 17:19:44 +01:00
Samuel Colvin 16263bafea None behaviour (#803)
* tweaks to None behaviour

* prevent sub_fields for Optional fields by default

* rewrite None validation

* rename whole > each_item on validators

* cleanup processing of the Json type

* fix schema coverage and cleanup

* tweak validate_model

* change and docs

* fix validators on optional fields

* coverage

* remove is_none_validator

* minor performance improvements to ErrorWrapper

* fix coverage

* fix PaymentCardNumber

* undo schema changes, fix

* tweak validators
2019-09-18 11:38:21 +01:00
Daniel Petti aa5e8c60b7 Feature/frozenset support (#762)
* Better error for unsuported "typing" objects.

Fixes #745

Add a better error message for fields with types from the typing
module that are not directly supported by Pydantic. Previously,
it caused a cryptic assertion failure.

* Add support for frozenset fields.

Also provide an example of their usage.

Fixes #745

* Address review comments.

Fixes #745

* use equals not "is" for int comparison.
2019-08-21 13:13:46 +01:00
Abdussamet Koçak f41d5dca3c Support assert statements inside validators (#653)
* Support assert statements inside validators

* Add a validator example that uses assert

* Add warning about consequences of using -O optimization flag

* Fix a typo

* Fix incomplete validator

* Extend exception name generation

* Improve tests

* Clarify pytest behaviour

* handle assertion error name, fix build

* Address feedback

* docs cleanup

* Incorporate feedback

* fix quotes
2019-08-15 12:06:21 +01:00
Samuel Colvin 461b852e4e enforcing single quotes (#612)
* enforcing single quotes

* update history
2019-06-21 12:32:03 +01:00
Samuel Colvin 27323aa8b2 fix for python 3.8 (#396)
* fix for python 3.8

* history and tweak types in main.py
2019-02-15 18:29:13 +00:00
Samuel Colvin baade9a117 rebuild validator arguments (#388)
* rebuild validator arguments

* cleanup and tests

* update docs
2019-02-13 10:31:36 +00:00
pyup.io bot a85334682c Scheduled monthly dependency update for February (#382)
* Update flake8 from 3.6.0 to 3.7.4

* Update mypy from 0.650 to 0.660

* Update pycodestyle from 2.4.0 to 2.5.0

* Update pyflakes from 2.0.0 to 2.1.0

* Update pytest from 4.0.2 to 4.2.0

* Update pytest-cov from 2.6.0 to 2.6.1

* fix test linting
2019-02-04 20:31:42 +00:00
Samuel Colvin 0f7f8fcf14 allow validate_always with default=None (#344)
* allow validate alwasy with None, fix #132

* prevent whole validators being called on subitems

* improve Optional validators edge cases
2018-12-29 11:07:45 +00:00
Samuel Colvin eeb48fbeac don't call validators on keys of dictionaries, fix #254 (#342) 2018-12-28 16:07:48 +00:00
Samuel Colvin c725a4a5e5 prevent validators being called repeatedly after inheritance (#327)
* prevent validators being called repeatedly after inheritance

* fix linting

* more tests
2018-12-25 22:33:18 +00:00
Hugo Duncan 3249330b80 Properly set Config in create_model (#320)
* Properly set Config in create_model

Set the Config attribute in create_model, so it is found by the
MetaModel.

* Black formatting fixes

* Remove uneeded validator logic

* Use single quotes

* Add return value to inherit_validators

* Refactor inherit_validators

* Add HISTORY.rst entry

* Address feedback

* Extend existing validator test cases and remove field aliasing

* Add test cases for validators inherted from parent

* Make test case names consistent with where validators are configured

* Add detail create_model validator inheritance tests
2018-12-07 20:27:42 +00:00
Samuel Colvin 9ff946d2fa PyObject = None, and Pattern (#306)
* PyObject = None, fix #305

* history formatting on pypi

* fix history rendering in setup

* allow Pattern validation, fix #303

* fix for python3.7

* fix linting

* proper processing for regex pattern errors
2018-11-18 16:11:29 +00:00
Samuel Colvin 15850a43c5 moving to black (#287)
* moving to black

* put back flake8

* remove isort option

* putting back isort

* uprev pycodestyle

* remove black from docs/examples

* tweak parse.py
2018-11-15 11:30:07 +00:00
Nicholas Hyatt 10414a7a04 use type() in int validator (#264)
* use type() in int validator to avoid returning a bool since bool is and instance of int

* add note to HISTORY.rst

* add tests & add username and PR # to HISTORY.rst

* use not ininstance(v, bool) to allow int subclassing
2018-09-21 10:37:16 +01:00
Samuel Colvin f0f9de5f96 improve docs on error handling (#198)
* improve docs on error handling

* change ValidationError signature

* cleanup

* rename _raw_errors > raw_errors

* improve _display_error_type_and_ctx
2018-06-11 13:06:50 +01:00
Samuel Colvin 9021d94e31 validator exception typo, fix #150 (#191) 2018-06-04 11:11:09 +01:00
Nikita Grishko 4f4e22ef47 Error context and message (#183)
* POC of error context and message

* Move type errors to the `errors.py` module; Change errors interface a bit

* Rename `.as_dict()` to `.dict()`

* Fix `PydanticErrorMixin` constructor

* Rename `exceptions.py` to `error_wrappers.py`

* Do not include nullable `ctx`

* Fix tests

* Added `int_validator`; Added `IntegerError`

* Added `float_validator`; Added `FloatError`

* Get rid of `__mro__` in prior of `exc.code`

* Removed `min_number_size` and `max_number_size` from config (#174)

* Added `NumberMinSizeError` and `NumberMaxSizeError`

* Added `NoneIsNotAllowedError`

* Added `EnumError`

* Added `path_validator`; Added `PathError`

* Added `DictError`

* Added `ListError`

* Added `TupleError`

* Added `SetError`

* Added `datetime` related errors

* Added `bytes` and `str` related errors

* Added `SequenceError`

* Improved code coverage

* Display error context in string representation of validation error

* Redefine error message templates using config

* Review fixes

* Updated changelog
2018-05-31 14:35:38 +01:00
Nikita Grishko 31683f8dc4 Errors format (#179)
* Get rid of `track` in errors

* Move `display_as_type` func into utils module

* Get rid of error as `namedtuple`

* Renamed `Error.index` to `Error.loc`

* New way to get error type

* New way to get error message

* New errors format

* Renamed `flatten_errors` property to `flat_errors`

* `__slots__` for `Error` and `ValidationError`

* `loc` as `tuple`

* Tests

* Review fixes

* `flatten_errors` as generator
2018-05-23 14:50:04 +01:00
Samuel Colvin a3a9b14531 validate attributes of parent models (#141)
* validate attributes of parent models, fix #137

* add history
2018-03-25 17:46:55 +01:00
Samuel Colvin a8096959e2 check for invalid validators (#140)
* check for invalid validators

* documentation and history
2018-03-25 16:41:48 +01:00
Samuel Colvin 89201f08b0 add wildcard validators (#128)
* add wildcard validators

* update docs and history

* more tests

* history links
2018-02-06 19:32:30 +00:00
Samuel Colvin 7050654062 better tests for validator checks 2017-11-08 15:39:44 +00:00
Samuel Colvin ed2b3f2afd errors for invalid validator use 2017-11-08 15:36:19 +00:00
Samuel Colvin acf3128dcd Validator always (#102)
* validators called always, fix #100

* testing validate always validators

* adding docs
2017-11-08 14:37:01 +00:00
Samuel Colvin ae6f5e7192 prevent duplicate validators (#101)
* prevent duplicate validators

* uprev and history
2017-11-08 13:53:14 +00:00
Samuel Colvin 91f962e533 replace values() with dict() (#99)
* replace values(), with dict(), fix #98

* add history and test
2017-11-07 13:28:42 +00:00
Samuel Colvin dfc5924936 Better validators (#97)
* working on improved validators

* full tests for validators

* tweask

* tweaking fields.py

* adding docs

* add history

* fix classmethod validators
2017-11-07 13:06:44 +00:00