Skip to content
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

Cerberus 2: Proposal to change the default_setter behaviour #438

Closed
funkyfuture opened this issue Sep 6, 2018 · 2 comments
Closed

Cerberus 2: Proposal to change the default_setter behaviour #438

funkyfuture opened this issue Sep 6, 2018 · 2 comments
Milestone

Comments

@funkyfuture
Copy link
Member

as #427 shows, the default_setter rule should be usable in a way that allows the used handler(s) to determine whether a field should be added to the document at all. therefore the proposed change is to delegate the responsibility of (not) adding a field to the document to the handler(s). currently it must return a value that is then set as the field's content in any case - though that can be prevented by raising an exception. #279 must be resolved as a requirement for this change.

cc @andreymal, @dkellner

@linupi
Copy link

linupi commented Dec 2, 2019

related to #515 I mentioned that the functionally discussed in this issue would be very helpful. In my sub-classed validator I solved it by introducing a special NotProvided type. It looks roughly like this:

class MyValidator(Validator):
	_NotProvided = type("_NotProvided", (), {})()


    def _normalize_default_setter(self, mapping, schema, field):
        """ {'oneof': [
                {'type': 'Callable'},
                {'type': 'string'}
                ]} """
        if 'default_setter' in schema[field]:
            setter = schema[field]['default_setter']
            if isinstance(setter, str):
                setter = self.__get_rule_handler('normalize_default_setter', setter)
            
            tmp = setter(mapping)                    
            if tmp != self._NotProvided:
	            mapping[field] = tmp
           #the last three lines replace `mapping[field] = setter(mapping)` in the original code        
        
my_custom_default_setter(mapping):
    ...
    if # I want to set a default:
        return MY_RETURN_VALUE
    else:
        retrun MyValidator._NotProvided

instead of always setting mapping[field] = setter(mapping) at the end of _normalize_default_setter I check for for _NotProvided first. Like this None is also a value that can be set by the provided setter.

In case this approach is considered for cerberus I suggest however to have an equivalent of _NotProvided on the module level instead of inside the validator.

@funkyfuture
Copy link
Member Author

closing this issue as there are currently no intentions to continue a next major release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants