-
Notifications
You must be signed in to change notification settings - Fork 240
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 default normalization rule #172
Add default normalization rule #172
Conversation
cool. cool. cool. please also extend the documentation. your examples above are fine to be added as
i tend to overriding a
what would be a usecase for that? |
@@ -454,6 +454,7 @@ def __normalize_mapping(self, mapping, schema): | |||
if self.purge_unknown: | |||
self._normalize_purge_unknown(mapping, schema) | |||
self._normalize_coerce(mapping, schema) | |||
self._normalize_default(mapping, schema) |
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.
though i have only one silly argument for setting defaults before coercing, here it is: it seems more intuitive to normalize in that order: purge unwanted items, fill up missing items, work on items.
but maybe it's be useful to coerce a value to None
in order to replace it with a default. coercing defaults on the other hand shouldn't make sense.
so propably it's better to keep it like it is. but i'd be interested in other thoughts.
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.
but maybe it's be useful to coerce a value to None in order to replace it with a default. coercing defaults on the other hand shouldn't make sense.
Agreed.
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.
but maybe it's be useful to coerce a value to None in order to replace it with a default.
But as told by @nicolaiarocci and I also think the same, using default to replace None
seems odd, specially if None
is a valid value (nullable
fields for example). But I know that maybe becomes handy, that's why I proposed to only override None
s with default only for non nullable
fields, but the @nicolaiarocci argument in the comment below seems valid too.
coercing defaults on the other hand shouldn't make sense.
It shouldn't but I don't know if it hurts, maybe it's handy and safer.
I'd say that in this case the user explicitly wants a
I'm unsure about this. As @funkyfuture says, there are other ways to achieve the same result already. I probably prefer a static default value. |
I was particularly thinking in something like:
Then if But yeah, this over-complicate the feature and makes it more unintuitive. I really don't understand which other way you have to accomplish this, can you give an example? |
This method allow to validate a document for a specific schema/validator and then compare the normalized document to another document in one single call.
Updates: added documentation and |
Very nice. However this does not seem to work with lists of dictionaries.
Blocks with: |
i don't think this is over-complicated. and i think that's an interesting use-case, so this may come up at some point anyway.
what about the following: in conjunction with an ability to ignore specific errors that'd allow flexible usage.
i think that may be #168. |
Let's do it this way:
|
For the record, I have a |
@nicolaiarocci and Eve's @dnohales concerning the non-static defaults: that could be achieved if coercing happened after setting defaults; so i'm curious about this question: are dynamic defaults still in the realm of normalization or is it generating data? and thus, does it belong into a library with a focus on validation? |
Errors should be show stoppers in my opinion. Especially so when reported by a validation tool. Furthermore, In Eve when |
So, I updated the code. Let me know if you agree with the changes (specially the latest commit), since I see some discrepancies about the compatibility with Eve. |
But yeah, I agree with @nicolaiarocci regarding the nullable matter. |
Thanks! |
This is for #131, it adds the
default
normalization rule to add missing fields in the document. Few examples:Concerns and thoughts
nullable
isFalse
, value isNone
anddefault
is present, should we override the field withdefault
or throw a validation error?default
is a callable, should we run the callable and set the callable result? Do we need another option likedefault_is_callable
that when isTrue
we call thedefault
value if is a callable and when isFalse
we don't call anything? We also can always call thedefault
value if is callable and if the user really want to set a function definition as a default value for the document, she could wrap it with a lambda function or something like that.