示例#1
0
    def validate(self) -> None:
        """
        Validate the list.

        Raises:
            AttributesError: if the list is not valid
        """
        errors: Dict[str, BaseAttributeError] = {}
        for i, item in enumerate(self):
            if not isinstance(item, self.values_type):
                item_type = type(item)
                item_repr = repr(item)
                errors[str(i)] = InvalidAttributeError(
                    message=(f'Expecting {self.values_type} for item {i} ; ' +
                             f'got {item_type} ({item_repr})'),
                    context=self,
                )
            elif isinstance(item, Validatable):
                try:
                    item.validate()
                except BaseAttributeError as e:
                    errors[str(i)] = e
        if errors:
            raise AttributesError(
                message='Validation failed',
                errors=errors,
                context=self,
            )
示例#2
0
    def apply_validator(
        self,
        validator: ValidatorProtocol[T],
        value: Any,
    ) -> None:
        """
        Apply the validator to the given value.

        Args:
            validator: a validator
            value: a value

        Raises:
            InvalidAttributeError: if the attribute value is invalid
        """
        try:
            validator(value=value, )
        except ValidatorError as e:
            name_repr = repr(self.name)
            if self.short_description:
                name_repr = f'{self.short_description} ; {name_repr}'
            safe_value_repr = self.as_repr(value)
            message = f'Validation error for {name_repr} = {safe_value_repr}: {e}'
            raise InvalidAttributeError(
                message=message,
                context=self,
            )
示例#3
0
 def _ensure_default_value_type(self) -> None:
     if not isinstance(self.default, self.value_type):
         default_type = type(self.default)
         safe_default_repr = self.as_repr(self.default)
         message = f'Expecting {self.value_type} for default value ; got {default_type} ({safe_default_repr}).'
         raise InvalidAttributeError(
             message=message,
             context=self,
         )
示例#4
0
    def __init__(
        self,
        value_type: Type[T],
        short_description: Optional[Text] = None,
        description: Optional[Text] = None,
        default: Optional[T] = None,
        secret: bool = False,
        required: bool = False,
        deprecated: bool = False,
        validator: Optional[ValidatorProtocol[T]] = None,
    ) -> None:
        """
        Initialize the attribute.

        Args:
            value_type: a type expected for the value of the attribute
            short_description: a short description of the attribute
            description: a description of the attribute
            default: a default value for the attribute
            secret: a flag indicating if the attribute is secret (avoid representing/printing sensible data)
            required: a flag indicating if the attribute must have a non-None value
            deprecated: a flag indicating if the attribute is deprecated
            validator: a function validating the value of the attribute

        Raises:
            InvalidAttributeError: if the attribute could not be initialized
        """
        self.value_type = value_type
        self.short_description = short_description
        self.description = description
        self.secret = secret
        self.required = required
        self.deprecated = deprecated
        self.default = default
        self.validator = validator
        if default is not None:
            self._ensure_default_value_type()
            if validator:
                try:
                    validator(value=default, )
                except ValidatorError as e:
                    raise InvalidAttributeError(
                        message=
                        f'Validation error for default value ({repr(default)}): {e}',
                        context=self,
                    )
示例#5
0
 def _ensure_value_type(
     self,
     value: Any,
 ) -> None:
     if not isinstance(value, self.value_type):
         name_repr = repr(self.name)
         if self.short_description:
             name_repr = f'{self.short_description} ; {name_repr}'
         value_type = type(value)
         safe_value_repr = self.as_repr(value)
         message = (
             f'Wrong value type for {name_repr} = {safe_value_repr}: expecting {self.value_type} got {value_type}'
         )
         raise InvalidAttributeError(
             message=message,
             context=self,
         )
示例#6
0
 def _apply_validator(
     self,
     validator: ValidatorProtocol[T],
     value: Any,
 ) -> None:
     try:
         validator(value=value, )
     except ValidatorError as e:
         name_repr = repr(self.name)
         if self.short_description:
             name_repr = f'{self.short_description} ; {name_repr}'
         safe_value_repr = self.as_repr(value)
         message = f'Validation error for {name_repr} = {safe_value_repr}: {e}'
         raise InvalidAttributeError(
             message=message,
             context=self,
         )