コード例 #1
0
 def from_dict(cls, data):
     if data is None:
         raise InvalidArgument('Missing input data')
     try:
         return cls(**data)
     except TypeError as ex:
         raise InvalidArgument('Invalid input: ' +
                               _generalize_init_type_error_message(ex))
コード例 #2
0
 def validate_range(self, value):
     max_value = self.max_value
     min_value = self.min_value
     if max_value is not None and value > max_value:
         raise InvalidArgument(
             f'Value `{self.property_name}` cannot be greater than {max_value}.'
         )
     if min_value is not None and value < min_value:
         raise InvalidArgument(
             f'Value `{self.property_name}` cannot be smaller than {str(min_value)}.'
         )
コード例 #3
0
 def develop_array(self, value):
     for item in value:
         if isinstance(item, self.model):
             yield item
         elif isinstance(item, Mapping):
             yield self.model.from_dict(item)
         else:
             raise InvalidArgument(f'Invalid item in collection: {item}')
コード例 #4
0
 def validate(self, instance, value):
     try:
         return self.enum_type(value)
     except ValueError:
         try:
             return self.enum_type[value]
         except KeyError:
             raise InvalidArgument(f'Value {value} is not valid')
コード例 #5
0
    def __init__(self, ancestor_type, **kwargs):
        super().__init__(**kwargs)
        if ancestor_type is None:
            raise EmptyArgumentException('required_type')

        if not inspect.isclass(ancestor_type):
            raise InvalidArgument('expected a class type')

        self.ancestor_type = ancestor_type
コード例 #6
0
    def validate_length(self, value):
        try:
            l = len(value)
        except TypeError as te:
            raise InvalidOperation(
                f'Cannot use as `LengthValidatable`: {str(te)}')

        max_length = self.max_length
        min_length = self.min_length
        if max_length is not None and max_length == min_length and l != max_length:
            raise InvalidArgument(
                f'Value `{self.property_name}` must be exactly {max_length} long.'
            )
        if max_length is not None and l > max_length:
            raise InvalidArgument(
                f'Value `{self.property_name}` cannot be longer than {max_length}.'
            )
        if min_length is not None and l < min_length:
            raise InvalidArgument(
                f'Value `{self.property_name}` cannot be shorter than {min_length}.'
            )
コード例 #7
0
    def from_configuration(cls, configuration, cls_type=None):
        if cls is Registry:
            raise InvalidArgument('call this method with a subclass of `Registry`')

        if isinstance(configuration, str):
            configuration = {'type': configuration}

        if cls_type is None:
            try:
                cls_type = cls._get_type(configuration)
            except (TypeError, ValueError) as error:
                raise InvalidArgument(f'Invalid {cls._get_class_keyname()} configuration. '
                                      f' Details: {str(error)}')

        try:
            return cls_type.from_dict(configuration)
        except Exception as error:

            raise InvalidArgument(f'Invalid {cls._get_class_keyname()} configuration. '
                                  f'Cannot create an instance of {cls_type.__name__} '
                                  f'using the input dictionary `{reprlib.repr(configuration)}`.'
                                  f' Details: {str(error)}')
コード例 #8
0
    def _get_type(cls, configuration, all_types=None):
        try:
            type_name = configuration['type']
        except TypeError:
            if configuration is None:
                raise InvalidArgument('configuration cannot be null')
            else:
                raise
        except KeyError:
            raise InvalidArgument(f"Missing `type` property in configuration object {cls._get_class_keyname()}. "
                                  f"Every configuration describing a {0} must have a `type` property: "
                                  f"the name of the {reprlib.repr(configuration)} it's referring to.")
        if all_types is None:
            all_types = cls.get_subclasses()
        found_types = [x for x in all_types if (x.get_class_name() == type_name
                                                or x.__name__.lower() == type_name.lower())]

        if not found_types:
            raise TypeNotFoundException(type_name, cls.__name__)

        if len(found_types) > 1:
            raise AmbiguousRegistryName(type_name, found_types)

        return found_types[0]
コード例 #9
0
 def validate(self, instance, value):
     if value not in self.possible_values:
         raise InvalidArgument('Value must be one of: {}'.format(', '.join(
             str(x) for x in self.possible_values)))
     return value
コード例 #10
0
def _get_rx(pattern):
    if isinstance(pattern, Pattern):
        return pattern
    if isinstance(pattern, str):
        return re.compile(pattern)
    raise InvalidArgument('Expected str or pattern')