-
Notifications
You must be signed in to change notification settings - Fork 75
Add schema validation to PyDict -> Document #88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add schema validation to PyDict -> Document #88
Conversation
|
||
Ok(value) | ||
} | ||
|
||
fn extract_value_single_or_list(any: &PyAny) -> PyResult<Vec<Value>> { | ||
if let Ok(values) = any.downcast::<PyList>() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not related to this PR, but I think this would benefit from using .extract::<Vec<Value>>()
or at least .extract::<Vec<&PyAny>>()
instead of .downcast::<PyList>()
to handle any sequence and not just lists.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this needlessly create a temporary Vec
? I'm transforming/mapping the elements inside, so maybe it's not worth collecting until the very end?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this needlessly create a temporary Vec? I'm transforming/mapping the elements inside, so maybe it's not worth collecting until the very end?
Yes, that would be the cost for supporting arbitrary sequences (like 1d NumPy arrays) instead of just lists. It is a trade-off and since it isn't really part of this PR it is probably best left to do elsewhere if done at all.
(Alternatively, this could use any.iter()
to just iterate of the sequence, but .iter()
also works for e.g. strings which would need to be checked before doing this.)
27caa50
to
432ce44
Compare
@GodTamIt do you want to rebase again now that the 0.20.1 PR has been merged |
432ce44
to
a3abbee
Compare
67337d2
to
5b8e97e
Compare
Thanks! This should be squash-merged |
This change addresses the issue mentioned here: #47
Document.extend()
andDocument.from_dict()
support an optionalschema
field that, when provided, validates the provided dictionary against the schema.This additionally fixes the issue where all numeric values are first parsed as
I64
and upon failure, parsed asF64
. This can cause problems for fields that are unsigned (U64
) but are parsed asI64
instead, or fields that are float (F64
) but is parsed asI64
. Now, when aschema
is provided, values will be parsed according to the schema's field specification.