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

Deferred validation #41

Open
binaryDiv opened this issue Apr 6, 2022 · 0 comments
Open

Deferred validation #41

binaryDiv opened this issue Apr 6, 2022 · 0 comments
Labels
new feature New feature that is not (only) a validator class

Comments

@binaryDiv
Copy link
Contributor

binaryDiv commented Apr 6, 2022

Example use case:

You want to parse an API response (dictionary) that contains a list of objects, e.g. the following validataclasses:

@validataclass
class DataRecord:
    foo: str = StringValidator()
    bar: int = IntegerValidator()

@validataclass
class ApiResponseData:
    some_meta_data: str = StringValidator()
    data_records: List[DataRecord] = ListValidator(DataclassValidator(DataRecord))

Now, there could be invalid data, but you don't want to reject the whole API response just because one of the data records is invalid, but only ignore the invalid ones and save all the other records.

One solution for this would be a ListValidator with a discard option (soon to be implemented, see #40). This might be sufficient in some cases, but you don't get any information about the invalid data, so you cannot for example log the validation errors.

Proposal:
Deferred validation. You validate the outer validataclass, but do not validate the data records yet. You get a "partially" validated object (e.g. the meta data was validated, but the list of data records contains unvalidated data). Then, when iterating over the list of data records, you validate one data record at a time and collect/log/whatever the validation errors that occur.

This can be done with a AnythingValidator or a AnyDictValidator (see #19, but can be easily implemented in a few lines) and another validator later, but it could also be a nice new library feature.

Implementation ideas:

  • A special DeferredValidator meta validator. On validate() it returns a special object that contains the raw data and can be used later to validate. (This special object could be a DeferredValidator instance that is now "bound" to the data, or maybe even call it BoundValidator or similar.)
  • A validataclass option (that can be specified in the field tuples similar to the Default values) that will be interpreted by the DataclassValidator. (Sounds elegant, but I think it won't turn out that great, especially if you have a list and want to defer validation of the list items, but not the list itself, this would not work.)
  • (Remember: You need other type annotations now, since the dataclass won't contain the actual data objects but those "DeferredValidators" or however they will be called.)
@binaryDiv binaryDiv added new validator Implementation of a new validator class new feature New feature that is not (only) a validator class and removed new validator Implementation of a new validator class labels Apr 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature New feature that is not (only) a validator class
Projects
None yet
Development

No branches or pull requests

1 participant