Include Partial Data when Raising ModelConversionError #304
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Schematics already has excellent separation of data parsing and validation. This PR adds the ability to access the result of converting primitive data in case of ModelConversionError.
import_loop
is the central mechanism for converting primitive values to native Python objects. If it encounters a conversion error when working on one field, theimport_loop
catches and stores and the error and moves on to the next field instead of bailing out completely on the first error. By the end of theimport_loop
, it has all the partial data parsed based on best effort in addition to all errors encountered. However, ModelConversionError only exposes the errors. This PR makes parsing result accessible as well through ModelConversionError.Specifically, ModelConversionError is updated to have a
partial_data
(optional) attribute. Interested party could catch the exception and access the attribute to retrieve the parsing result. Existing code that has no interest in this attribute requires no change at all.This feature is very handy in implementing a "best effort" parsing, which in turn is useful in building system that acts in response to user intention rather than fully validated user data. For example, in a user sign up form, the phone number field being malformed should not prevent the application from parsing the "zip code" field and auto-selects proper value for the "state" field.