def validate(self, value): super().validate(value) if self.pattern and not self.pattern.fullmatch(value): raise ValidationError(f'value must match pattern {self.pattern}', offender=self) if self.allowed_characters is not None: try: i, c = next(((i, c) for (i, c) in enumerate(value) if c not in self.allowed_characters)) except StopIteration: pass else: raise ValidationError( f'character {c} (position {i}) is not allowed', offender=self) if self.forbidden_characters is not None: try: i, c = next(((i, c) for (i, c) in enumerate(value) if c in self.forbidden_characters)) except StopIteration: pass else: raise ValidationError( f'character {c} (position {i}) is forbidden', offender=self)
def validate(self, value): super().validate(value) for p in value: try: exists = p.exists() except OSError: raise ValidationError('path seems invalid', offender=self.edit) if not exists: raise ValidationError("path doesn't exists", offender=self.edit) if exists \ and p.is_dir(): raise ValidationError('path is a directory', offender=self.edit)
def validate(self, d): super().validate(d) for (k, v), subwidget in self.result_zip_subwidget(d, self.inners): try: subwidget.maybe_validate(v) except ValidationError as e: raise ValidationError('error validating ' + subwidget.title, offender=subwidget) from e
def validate(self, value: List[List[T]]): for i, (inner_row, v_row) in enumerate(zip(self.inners, value)): for j, (inner, v) in enumerate(zip(inner_row, v_row)): try: inner.maybe_validate(v) except ValidationError as e: raise ValidationError(f'error validating {i, j}', offender=inner) from e
def validate(self, value: List[List[T]]): for i, (inner_row, v_row) in enumerate(zip(self.inners, value)): for field_name, (inner, v) in zip(self.value_type._fields, zip(inner_row, v_row)): try: inner.maybe_validate(v) except ValidationError as e: raise ValidationError( f'error validating {i}[{field_name}]', offender=inner) from e
def validate(self, value): super().validate(value) try: exists = value.exists() except OSError: raise ValidationError('path seems invalid', offender=self.edit) if exists: if self.exist_cond not in (True, None): raise ValidationError("path already exists", offender=self.edit) # if the file exists, we don't need to check it else: if self.exist_cond not in (False, None): raise ValidationError("path doesn't exists", offender=self.edit) # so checking of a filename is valid is stupid complicated, slow, and fallible, # https://stackoverflow.com/questions/9532499/check-whether-a-path-is-valid-in-python-without-creating-a-file-at-the-paths-ta/34102855#34102855 # we're just gonna check for invalid characters if not filename_valid(value): raise ValidationError('path seems invalid', offender=self.edit) if exists \ and value.is_dir(): raise ValidationError('path is a directory', offender=self.edit)
def validate(self, v): super().validate(v) if v == '': raise ValidationError('a name must be provided')
def ret(v): if min is not None and v < min: raise ValidationError(f'value must be at least {min}') if max is not None and v >= max: raise ValidationError(f'value must be less than {max}')