dfurtado / dataclass-csv

Map CSV to Data Classes
Other
195 stars 21 forks source link

Validate duplicated header #42

Closed dfurtado closed 3 years ago

dfurtado commented 3 years ago

This PR adds validation when a CSV file contain duplication of headers

For example, give the following CSV file with the contents below:

name, email,age,name
User1,user@test.com,40,User2

In this case the value that is going to be set a property name in a dataclass is User2. It will overwrite the first value User1.

To avoid this problem, and data inconsistency, a validation is performed right when creating a instance of DataclassReader. If there are duplication a ValueError exception will be raised, showing which fields are duplicated.

In addition to the validation, it has been added support for skipping the validation, that is achieved by passing a keyword argument to the DataclassReader, like so:

reader = DataclassReader(f, User, validate_header=False)

This closes the issue: https://github.com/dfurtado/dataclass-csv/issues/10