Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
size = value.get("size", None)
if size is not None and not isinstance(size, int):
raise ValidationError("field's size element must be an integer")
total_size = value.get("total_size", None)
if total_size is not None and not isinstance(total_size, int):
raise ValidationError("field's total_size element must be an integer")
refs = value.get("refs", None)
if refs is not None and not isinstance(refs, list):
# TODO: Validate that all refs are strings.
raise ValidationError("field's refs element must be a list of strings")
return DirDescriptor(path, size=size, total_size=total_size, refs=refs,)
elif not isinstance(value, None):
raise ValidationError("field must be a DirDescriptor, string or a dict")
def to_python(self, value):
"""Convert value if needed."""
if value is not None:
try:
return int(value)
except (TypeError, ValueError):
raise ValidationError("field must be an integer")
def to_python(self, value):
"""Convert value if needed."""
if value is not None:
try:
return float(value)
except (TypeError, ValueError):
raise ValidationError("field must be a float")
def to_python(self, value):
"""Convert value if needed."""
if isinstance(value, FileDescriptor):
return value
elif isinstance(value, str):
return FileDescriptor(value)
elif isinstance(value, dict):
try:
path = value["file"]
except KeyError:
raise ValidationError("dictionary must contain a 'file' element")
if not isinstance(path, str):
raise ValidationError("field's file element must be a string")
size = value.get("size", None)
if size is not None and not isinstance(size, int):
raise ValidationError("field's size element must be an integer")
total_size = value.get("total_size", None)
if total_size is not None and not isinstance(total_size, int):
raise ValidationError("field's total_size element must be an integer")
is_remote = value.get("is_remote", None)
if is_remote is not None and not isinstance(is_remote, bool):
raise ValidationError("field's is_remote element must be a boolean")
file_temp = value.get("file_temp", None)
if file_temp is not None and not isinstance(file_temp, str):
raise ValidationError("field's file_temp element must be a string")
elif isinstance(value, dict):
try:
path = value["dir"]
except KeyError:
raise ValidationError("dictionary must contain a 'dir' element")
if not isinstance(path, str):
raise ValidationError("field's dir element must be a string")
size = value.get("size", None)
if size is not None and not isinstance(size, int):
raise ValidationError("field's size element must be an integer")
total_size = value.get("total_size", None)
if total_size is not None and not isinstance(total_size, int):
raise ValidationError("field's total_size element must be an integer")
refs = value.get("refs", None)
if refs is not None and not isinstance(refs, list):
# TODO: Validate that all refs are strings.
raise ValidationError("field's refs element must be a list of strings")
return DirDescriptor(path, size=size, total_size=total_size, refs=refs,)
elif not isinstance(value, None):
raise ValidationError("field must be a DirDescriptor, string or a dict")
def clean(self, value):
"""Run validators and return the clean value."""
if value is None:
value = self.default
try:
value = self.to_python(value)
self.validate(value)
except ValidationError as error:
raise ValidationError(
"invalid value for {}: {}".format(self.name, error.args[0])
)
return value
def validate(self, value):
"""Validate field value."""
if value is not None and not isinstance(value, bool):
raise ValidationError("field must be a boolean")
super().validate(value)
def validate(self, value):
"""Validate field value."""
if self.required and value is None:
raise ValidationError("field is required")
if value is not None and self.choices is not None:
choices = [choice for choice, _ in self.choices]
if value not in choices and not self.allow_custom_choice:
raise ValidationError(
"field must be one of: {}".format(", ".join(choices),)
)
def to_python(self, value):
"""Convert value if needed."""
if isinstance(value, str):
return value
elif isinstance(value, dict):
try:
value = value["url"]
except KeyError:
raise ValidationError("dictionary must contain an 'url' element")
if not isinstance(value, str):
raise ValidationError("field's url element must be a string")
return value
elif not isinstance(value, None):
raise ValidationError("field must be a string or a dict")
size = value.get("size", None)
if size is not None and not isinstance(size, int):
raise ValidationError("field's size element must be an integer")
total_size = value.get("total_size", None)
if total_size is not None and not isinstance(total_size, int):
raise ValidationError("field's total_size element must be an integer")
is_remote = value.get("is_remote", None)
if is_remote is not None and not isinstance(is_remote, bool):
raise ValidationError("field's is_remote element must be a boolean")
file_temp = value.get("file_temp", None)
if file_temp is not None and not isinstance(file_temp, str):
raise ValidationError("field's file_temp element must be a string")
refs = value.get("refs", None)
if refs is not None and not isinstance(refs, list):
# TODO: Validate that all refs are strings.
raise ValidationError("field's refs element must be a list of strings")
return FileDescriptor(
path,
size=size,
total_size=total_size,
is_remote=is_remote,
file_temp=file_temp,
refs=refs,
)
elif not isinstance(value, None):
raise ValidationError("field must be a FileDescriptor, string or a dict")