Validation should pass None through
Closed this issue · 2 comments
thclark commented
Calling:
name='whatever_strand'
validate_strand(self, name, source=None)
in the case where the twine has no strand name
results in a KeyError
, because the implementation assumes presence of the key, but the validate
method does not have an else
statement in the case that if strand_data is not None:
def validate_strand(self, name, source, **kwargs):
""" Validates a single strand by name
"""
return self.validate({name: source}, **kwargs)[name]
In general if asked to validate a None
strand, Twined should pass the None
through.
To fix:
if strand_data is not None:
# ...
else:
sources[strand_name] = None
thclark commented
Workaround looks like this:
Workaround does NOT look like this because validation exceptions in Twined (rightly) also inherit from KeyError
😭
# Working around https://github.com/octue/twined/issues/55
try:
serialised[k] = self.twine.validate_strand(k, getattr(self, k), cls=OctueJSONEncoder)
except KeyError as e:
print(e.args[0])
serialised[k] = None