-
Notifications
You must be signed in to change notification settings - Fork 237
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
Keep a reference to the current document, 'rename' and 'remove_unknown' options #113
Conversation
@@ -701,6 +722,13 @@ def _validate_allof(self, allof, field, value): | |||
if not valid: | |||
self._error(field, errorstack) | |||
|
|||
def _validate_rename(self, rename, field, value): | |||
if isinstance(rename, _str_type): |
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.
this should be caught upon schema-validation.
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.
I think schema validation will check the field value type only, the rename field is just a configuration option which should be a string since since its value will replace the original field name in the document.
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.
i don't get your point.
what hinders you from checking if the provided value in the schema is a string in validate_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.
You're right, I'll add the check there and will change the type to collections.Hashable
.
there should be a |
@@ -298,6 +316,9 @@ def _validate_definition(self, definition, field, value): | |||
if validator: | |||
validator(definition[rule], field, value) | |||
|
|||
if 'rename' in definition: |
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.
shouldn't this happen before further validation like coercion?
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.
I've purposely added it last since a rename will change a document field which could break other (custom/user) validators looking for the original field name.
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.
remove_unknown
is included, see https://github.com/misja/cerberus/blob/references/cerberus/cerberus.py#L134
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.
i mean the possibility to override this option via a rule, that can be placed in a schema-defintion. since allow_unknown
(as Validator
-property) is inherited by derived validators, there is also the constraint allow_unknown
in order to override this. please see the documentation about it.
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.
I've purposely added it last since a rename will change a document field which could break other (custom/user) validators looking for the original field name.
intuitively i'd expect that first a normalization is applied and the result is then validated.
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.
Makes sense, I'll put the rename first. As for adding a remove_unknown
rule to the schema, will do.
as @misja points out this actually solves #107. which is a very cool incident, so i can leave town w/o that issue open. i'm going to investigate and review more tomorrow regarding this issue. an extension to the schema like the following works fine to check the correct execution order and result:
imo, the form oh, what happens in a case like this?: schema = {'foo': {'type': 'string'},
'bar': {'rename': 'foo'}}
document = {'bar': 42} |
Right, I just did a final commit, just a tweak really. I did leave the x = {'root': {'child': 'my value'}}
y = x['root']
y['child'] = 'other value'
assert x == {'root': {'child': 'other value'}} x = {'root': {'child': 'my value'}}
y = x['root']['child']
y = 'other value'
assert x == {'root': {'child': 'other value'}} |
Cool stuff. One thing I'd do however, is make |
Also, for next time maybe, make sure you split every new feature or fix into individual pull requests. Makes it easier on the maintainer and more importantly on project history and therefore, for others to review. |
I have been thinking about the 'reference to current document' feature recently. My though was to keep a current "path", probably just a member named What do you think about doing it this way instead. For my own applications it would be useful to know where in the document validation is occurring, but I think it would have the added benefit of not having to keep the main and current documents in sync. |
i was also thinking to refactor some stuff. especially a segregation of normalization of a document as a whole before validation. what you propose as but i can look into things at earliest as July. |
I think you are right. Perhaps the Validator class keeps a |
that's the plan. |
nice. then I think it would be better to have a |
I want/need to wrap up a 0.9 release soon, ideally as soon as #107 is solved. I can merge this so it gets included with next release, which I suspect would be ideal for @misja, or keep it on hold until further development is accomplished. Given @funkyfuture and @CD3 planned developments around this, it might be advisable to hold for now. Thoughts? |
@funkyfuture @CD3 as I see it, adding a layer to keep a trail is routing around a more persistent problem. One can perfectly iterate through a dict and keep pointers to elements within the document, it's just the current splitting up and passing of field and value to rules which messes things up. |
@nicolaiarocci #107 could be solved now by cherry picking my commits related to setting |
Having a |
unfortunately stress grew more than anticipated the last days and i won't be in town for two weeks. i'm not very fond of a release atm. here are some reasons: as i understand such thing would not fail respectively the behaviour is not determinate depending on order in which fields are validated, which is not inuitive imo: schema = {'foo': {'type': 'string'},
'bar': {'rename': 'foo'}}
document = {'bar': 42} there's no way to change the behaviour concerning in my impression the code is meanwhile wildly grown and some consolidating refactoring should be done before further features are added. several aspects have been spotted and noted in the recent discussions. few i haven't publicly articulated yet. unfortuantely i don't have the time now to comprehend all that. this is still unclear to me. as the list as argument for types hasn't been released yet, i'd rather remove it as with
if a release is necessary soon, i'd pledge to make it a |
This issue has been fixed. The unit tests added in this commit demonstrates that both organizations will work. |
thanks for the feedback. |
@funkyfuture To be honest I'm fine if this pull request is put on hold, I'm at least glad part of it has helped squash a related issue (serendipity ftw!). Whilst working on it doubt has crept in though if this library is suitable enough for my use, so am leaving it for now. |
that leaves the question about the removal of checking a list of types from my list. that's not too hard, once it's decided. we can also postpone an 'agglutinating' syntax for |
I agree (but I realize I've only been contributing for about a week). You should remove the list of types feature before the release so that you/we can implement a generic solution without having to worry about breaking backward compatibility. If you don't remove it now, you won't be able to remove it for a while. |
This relates to #95 , where no reference is kept to the document being validated and hence not being able to change or remove fields. This commit maintains references and adds a
current
property to the validator, pointing to the current document and making it available to custom validators.