Skip to content

must_exist=False raise Validation error on attribute presenting in config file #1286

@qiuxiaomu

Description

@qiuxiaomu

I have set up two validators like this:

 def validators(self):
        return [
            Validator(
                'config.len_delta',
                is_type_of=int,
                must_exist=False,
                default=2,
            ),
            Validator(
                'config.expire',
                is_type_of=int,
                must_exist=False,
                default=2,
            ),
        ]

The error message I got was the config.expire cannot exist in env test.

The error was raised from here:

            # is name required but not exists?
            if self.must_exist is True and value is empty:
                _message = self.messages['must_exist_true'].format(name=name, env=env)
                raise ValidationError(_message, details=[(self, _message)])
            
            # Does this code not indicate that name is not required but name has a value?
            # If it is not required, it cannot have a value? Is this really the intention?
            if self.must_exist is False and value is not empty:
                _message = self.messages['must_exist_false'].format(name=name, env=env)
                raise ValidationError(_message, details=[(self, _message)])

And I read from validator's reference that:

must_exist is alias to required requirement. (executed after when)

I had the same interpretation as it was described in the API reference.

Through the above setting I was trying to convey that the attribute does not have to be present; if it was not present, it carries a default value of 2.

In short, does must_exist=False = "not required" = "optional", or must_exist=False = "Cannot have it in config" ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions