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

Add option to ignore None values to Optional() #297

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,9 @@ def validate(self, data, **kwargs):
continue
skey.handler(nkey, data, e)
else:
if isinstance(skey, Optional):
if hasattr(skey, "ignore_none") and skey.ignore_none and value is None:
continue
try:
nvalue = Schema(svalue, error=e, ignore_extra_keys=i).validate(value, **kwargs)
except SchemaError as x:
Expand Down Expand Up @@ -698,7 +701,8 @@ class Optional(Schema):

_MARKER = object()

def __init__(self, *args, **kwargs):
def __init__(self, *args, ignore_none=False, **kwargs):
self.ignore_none = ignore_none
default = kwargs.pop("default", self._MARKER)
super(Optional, self).__init__(*args, **kwargs)
if default is not self._MARKER:
Expand Down