Make coerce working within items rule#361
Conversation
36b3ea9 to
9d06a06
Compare
funkyfuture
left a comment
There was a problem hiding this comment.
thanks for this extension!
one thing isn't clear for me. the docs state regarding the items rule:
The items will only be evaluated if the given iterable’s size matches the definition’s.
Is this also addressed in this normalization context?
i'd like to see a test that covers the case when a value in a document throws a ValueError on coerce. the handling seems covered. yet error handling deserves some regession tests, especially the error representations.
| validator = self._get_child_validator( | ||
| document_crumb=field, schema_crumb=(field, 'items'), | ||
| schema=schema) | ||
| value_type = type(mapping[field]) |
There was a problem hiding this comment.
why is type being called here?
There was a problem hiding this comment.
Hi!
i'd like to see a test that covers the case when a value in a document throws a ValueError on coerce.
I've added tests for ValueError and TypeError.
why is
typebeing called here?
I used __normalize_sequence as a base. This line is added by commit eb24176 (Ensures container types are preserved in normalization). If tuple passed instead list, type will be preserved.
>>> schema = {'things': {'type': 'list', 'items': [{'coerce': int}, {'coerce': str}]}}
>>> validator = Validator(schema)
>>> validator.validate({'things': ['1', 2]})
True
>>> validator.document
{'things': [1, '2']}
>>> validator.validate({'things': ('1', 2)})
True
>>> validator.document
{'things': (1, '2')}
9d06a06 to
b1475fd
Compare
|
a'ight, cool.
ah, thanks for the reminder. ;-) just one thing, that should be a simple: when the number of fields in a sequence of a documnet is different from the amount of defined items in the relating |
Well... It seems I understand now why you asking :) So, is there alternative to Answer for your question is, yes, lengths must match :) |
|
now, i don't get your question. due to lack of context. but i'd recommend StackOverflow for such questions. regarding the length match; there's just a condition missing in line 688 to address this, right? oh, and one possibility isn't covered: when an |
|
@oev81 are you going to finalize this patch? |
@funkyfuture, sorry for not answering. I found the approach of coerce (conversion before validation) does not feet my task (conversion after validation, or conversion within validation). |
|
@nicolaiarocci would you re-open this pr and assign me to it? i'd complete this blind spot some day. |
Case example:
schema = {'things': {'type': 'list', 'items': [{'coerce': int}, {'coerce': str}]}}