Skip to content

[RFC] Stop setting default value inside validate method #981

@rochacbruno

Description

@rochacbruno

There is a problem with the current way of setting default values when using validators.

The method _validate_items should have no side effect on the object being validated, inside that method, teh settings must be read only to perform the validation.

Currently this method calls set twice to set default values and apply cast transformation.

here and here

This is causing problems with the inspect history and is duplicating the calls to set

Proposal

  1. Stop calling set on validate_items
  2. Collect defaults and cast from registered validators

Refactoring

  1. Stop calling set and setdefault inside _validate_items
  2. change this block to value = settings.get(name, empty)
  3. On ValidatorList.register iterate all validators and register its default and cast parameter on settings._validators_defaults: DynaBox (OR store it on the instance of ValidatorList itself
  4. On Settings.get line change the implementation to raise KeyError and then on except lookup the self._validators_defaults else default
  5. On line lookup for cast also on self._validators_defaults if not passed directly to get
  6. On Settings init create an instance of ValidatorsList and then call the register on it, also change ValidatorList init to pass accepted args to register instead of super

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions