You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.)
The text was updated successfully, but these errors were encountered:
Example use case:
You want to parse an API response (dictionary) that contains a list of objects, e.g. the following validataclasses:
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 aAnyDictValidator
(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:
DeferredValidator
meta validator. Onvalidate()
it returns a special object that contains the raw data and can be used later to validate. (This special object could be aDeferredValidator
instance that is now "bound" to the data, or maybe even call itBoundValidator
or similar.)A validataclass option (that can be specified in the field tuples similar to the Default values) that will be interpreted by the(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.)DataclassValidator
.The text was updated successfully, but these errors were encountered: