Exemple #1
0
    False
    '''
    def validate(self, obj, lazy=True, path=[]):
        partial = None
        for v in self._schemas:
            try:
                v.validate(obj, lazy, path)
            except errors.PartialValidationError as ex:
                partial = ex
            except errors.ValidationError:
                pass
            else:
                return
        if partial is not None:
            raise partial
        else:
            v_json_types = ', '.join(
                set(type(v).__name__ for v in self._schemas))
            msg = "%%(path)s is none of: '%s'" % v_json_types
            raise errors.ValidationError((msg, path))


# Save specialized _schemas into the Schema class
Schema.OR_SCHEMA = OR
Schema.AND_SCHEMA = AND

__all__ = get_schemas(globals())
if __name__ == '__main__':
    import doctest
    doctest.testmod(optionflags=doctest.REPORT_ONLY_FIRST_FAILURE, verbose=0)
Exemple #2
0
    def _expand_or_compress(self, is_expand, obj, **kwds):
        # Make a super call and disable validation
        # This will raise proper errors if obj is not valid and will guarantee
        # that if obj == null than obj is null. 
        obj = super(Array, self)._expand_or_compress(is_expand, obj, **kwds)
        kwds['validate'] = False

        if obj is self.null:
            return obj
        else:
            func = (self.type.expand if is_expand else self.type.compress)
            for i, item in enumerate(obj):
                obj[i] = func(item, **kwds)
            return obj

    def adapt(self, obj, inplace=False, path=[]):
        if not inplace:
            obj = list(obj)
        for idx, v in enumerate(obj):
            obj[idx] = self.array_t.adapt(v, inplace, path)
        return obj

# Save specialized _schemas into the Schema class
Schema.OBJECT_SCHEMA = Object
Schema.ARRAY_SCHEMA = Array

__all__ = get_schemas(globals())
if __name__ == '__main__':
    import doctest
    doctest.testmod(optionflags=doctest.REPORT_ONLY_FIRST_FAILURE, verbose=0)
Exemple #3
0
    @from_simple
    def validate(self, obj):
        return True

class Nothing(Schema):
    '''
    A validator that invalidates all objects.
    
    Examples
    --------
    
    >>> Nothing().is_valid('Forty two')
    False
    >>> Nothing().is_valid(42)
    False
    >>> Nothing().is_valid(None)
    False
    '''
    @from_simple
    def validate(self, obj):
        return False

# Save specialized schema into the Schema class
Schema.CTE_SCHEMA = Cte

__all__ = get_schemas(globals()) + ['undefined']
if __name__ == '__main__':
    import doctest
    doctest.testmod(optionflags=doctest.REPORT_ONLY_FIRST_FAILURE, verbose=0)
Exemple #4
0
            # Find all optional arguments
            mangling = '_%s__' % name
            msize = len(mangling)
            for arg_name in dic.keys():
                if arg_name.startswith(mangling):
                    dic[arg_name[msize:] + '?'] = dic.pop(arg_name)

            try:
                descr = dic['__doc__']
                tt = Object(dic, name=name, descr=descr)
            except KeyError:
                tt = Object(dic, name=name)
            return Array(tt)

def is_optional(sch, value=True):
    '''Flag field as optional'''

    sch.is_optional = value
    return sch

def is_root(sch, value=True):
    '''Flag field as root field'''

    sch.is_root = value
    return sch

__all__ = get_schemas(globals()) + ['mk_obj', 'mk_array_of', 'type_schema_factory', 'is_optional', 'is_root']
if __name__ == '__main__':
    import doctest
    doctest.testmod(optionflags=doctest.REPORT_ONLY_FIRST_FAILURE, verbose=0)