<commit_before>from jsrn import exceptions
from jsrn.resources import create_resource_from_dict
from jsrn.fields import Field

__all__ = ('ObjectAs', 'ArrayOf',)


class ObjectAs(Field):
    default_error_messages = {
        'invalid': u"Must be a object of type ``%r``.",
    }

    def __init__(self, of, **kwargs):
        try:
            of._meta
        except AttributeError:
            raise TypeError("``%r`` is not a valid type for a related field." % of)
        self.of = of

        super(ObjectAs, self).__init__(**kwargs)

    def to_python(self, value):
        if value is None:
            return None
        if isinstance(value, self.of):
            return value
        if isinstance(value, dict):
            return create_resource_from_dict(value, self.of._meta.resource_name)
        msg = self.error_messages['invalid'] % self.of
        raise exceptions.ValidationError(msg)

    def clean(self, value):
        """
        Convert the value's type and run validation. Validation errors
        from to_python and validate are propagated. The correct value is
        returned if no error is raised.
        """
        value = self.to_python(value)
        return value


class ArrayOf(ObjectAs):
    default_error_messages = {
        'invalid': u"Must be a list of ``%r`` objects.",
    }

    def __init__(self, of, **kwargs):
        kwargs.setdefault('default', list)
        super(ArrayOf, self).__init__(of, **kwargs)

    def to_python(self, value):
        if value is None:
            return []
        if isinstance(value, list):
            super_to_python = super(ArrayOf, self).to_python
            return [super_to_python(i) for i in value]
        msg = self.error_messages['invalid'] % self.of
        raise exceptions.ValidationError(msg)

    def clean(self, value):
        """
        Convert the value's type and run validation. Validation errors
        from to_python and validate are propagated. The correct value is
        returned if no error is raised.
        """
        value = self.to_python(value)
        return value

<commit_msg>Fixed issue of validation dropping out early when parsing lists, side benefit is that ArrayOf composite items now report better errors including item index<commit_after>from jsrn import exceptions
from jsrn.resources import create_resource_from_dict
from jsrn.fields import Field

__all__ = ('ObjectAs', 'ArrayOf',)


class ObjectAs(Field):
    default_error_messages = {
        'invalid': u"Must be a object of type ``%r``.",
    }

    def __init__(self, of, **kwargs):
        try:
            of._meta
        except AttributeError:
            raise TypeError("``%r`` is not a valid type for a related field." % of)
        self.of = of

        super(ObjectAs, self).__init__(**kwargs)

    def to_python(self, value):
        if value is None:
            return None
        if isinstance(value, self.of):
            return value
        if isinstance(value, dict):
            return create_resource_from_dict(value, self.of._meta.resource_name)
        msg = self.error_messages['invalid'] % self.of
        raise exceptions.ValidationError(msg)

    def clean(self, value):
        """
        Convert the value's type and run validation. Validation errors
        from to_python and validate are propagated. The correct value is
        returned if no error is raised.
        """
        value = self.to_python(value)
        return value


class ArrayOf(ObjectAs):
    default_error_messages = {
        'invalid': u"Must be a list of ``%r`` objects.",
    }

    def __init__(self, of, **kwargs):
        kwargs.setdefault('default', list)
        super(ArrayOf, self).__init__(of, **kwargs)

    def to_python(self, value):
        if value is None:
            return []
        if isinstance(value, list):
            super_to_python = super(ArrayOf, self).to_python
            values = []
            errors = {}
            for idx, obj in enumerate(value):
                try:
                    values.append(super_to_python(obj))
                except exceptions.ValidationError, ve:
                    if hasattr(ve, 'message_dict'):
                        errors[str(idx)] = ve.message_dict
                    else:
                        errors[str(idx)] = ve.messages
            if errors:
                raise exceptions.ValidationError(errors)
            return values
        msg = self.error_messages['invalid'] % self.of
        raise exceptions.ValidationError(msg)

    def clean(self, value):
        """
        Convert the value's type and run validation. Validation errors
        from to_python and validate are propagated. The correct value is
        returned if no error is raised.
        """
        value = self.to_python(value)
        return value


{'fraction_non_alphanumeric': 0.09174121073434574, 'fraction_numerical': 0.0, 'mean_word_length': 2.161078238001315, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7426933', 'n_tokens_mistral': 1298, 'n_tokens_neox': 1260, 'n_words': 411}