Ejemplo n.º 1
0
    def test_one_valid_schema_should_pass(self):
        schema = OrField(schemas=[StrField(), BoolField(), IntField()])

        value = True

        validator = SchemaValidator(schema, value)
        validator.validate()
Ejemplo n.º 2
0
    def test_valid_value_should_pass(self):
        schema = FloatField(min=1, max=10)
        value = 5.0

        validator = SchemaValidator(schema=schema, value=value)

        validator.validate()
Ejemplo n.º 3
0
    def test_valid_bool_should_pass(self):
        schema = BoolField()
        value = False

        validator = SchemaValidator(schema=schema, value=value)

        validator.validate()
Ejemplo n.º 4
0
    def test_valid_str_should_pass(self):
        schema = StrField(min_length=1, max_length=5)
        value = 'abcde'

        validator = SchemaValidator(schema=schema, value=value)

        validator.validate()
Ejemplo n.º 5
0
    def test_valid_regex_should_pass(self):
        schema = RegexField('\\d{5}\\Z')

        value = '12345'

        validator = SchemaValidator(schema, value)

        validator.validate()
Ejemplo n.º 6
0
    def test_full_schema_should_pass(self):
        schema = ListField(
            min_items=1,
            max_items=3,
            item_schema=DictField(
                schema={
                    'name':
                    StrField(
                        min_length=2,
                        max_length=50,
                    ),
                    'age':
                    IntField(min=0, max=120),
                    'money':
                    FloatField(min=0.0, max=999.9),
                    'alive':
                    BoolField(),
                    'gender':
                    EnumField(accept=['M', 'F', 'O']),
                    'code':
                    RegexField(regex='^([0-9]{3})'),
                    'doc':
                    OrField(schemas=[
                        RegexField(
                            regex='[0-9]{3}\\.?[0-9]{3}\\.?[0-9]{3}\\-?[0-9]{2}'
                        ),  # cpf
                        RegexField(
                            regex=
                            '[0-9]{2}\\.?[0-9]{3}\\.?[0-9]{3}\\/?[0-9]{4}\\-?[0-9]{2}'
                        )  # cnpj
                    ])
                },
                strict=True,
                optional_props=['gender', 'code', 'doc']))

        value = [{
            'name': 'Batman',
            'age': 31,
            'money': 999.0,
            'alive': True,
            'gender': 'M',
            'code': '468',
            'doc': '759.425.730-85'
        }, {
            'name': 'Superman',
            'age': 29,
            'money': 0.0,
            'alive': True,
            'doc': '31.035.254/0001-79'
        }]

        validator = SchemaValidator(schema=schema, value=value)

        validator.validate()
Ejemplo n.º 7
0
    def test_optional_prop_should_be_valid_if_not_present(self):
        schema = DictField(schema={
            'foo': BoolField(),
            'bar': BoolField()
        },
                           strict=True,
                           optional_props=['bar'])
        value = {'foo': True}

        validator = SchemaValidator(schema=schema, value=value)

        validator.validate()
Ejemplo n.º 8
0
    def test_value_greater_should_raise_error(self):
        schema = FloatField(max=1)
        value = 2.0

        validator = SchemaValidator(schema=schema, value=value)

        try:
            validator.validate()
            self.fail()
        except SchemaValidationError as e:
            self.assertEqual(e.path, '$root')
            self.assertEqual(e.code, 'FLOAT_MAX')
Ejemplo n.º 9
0
    def test_not_type_float_should_raise_error(self):
        schema = FloatField()
        value = 'abc'

        validator = SchemaValidator(schema=schema, value=value)

        try:
            validator.validate()
            self.fail()
        except SchemaValidationError as e:
            self.assertEqual(e.path, '$root')
            self.assertEqual(e.code, 'FLOAT_TYPE')
Ejemplo n.º 10
0
    def test_max_length_should_raise_error(self):
        schema = StrField(max_length=4)
        value = 'abcde'

        validator = SchemaValidator(schema=schema, value=value)

        try:
            validator.validate()
            self.fail()
        except SchemaValidationError as e:
            self.assertEqual(e.path, '$root')
            self.assertEqual(e.code, 'STR_MAX_LENGTH')
Ejemplo n.º 11
0
    def test_invalid_type_should_raise_error(self):
        schema = StrField()
        value = 123

        validator = SchemaValidator(schema=schema, value=value)

        try:
            validator.validate()
            self.fail()
        except SchemaValidationError as e:
            self.assertEqual(e.path, '$root')
            self.assertEqual(e.code, 'STR_TYPE')
Ejemplo n.º 12
0
    def test_not_type_int_should_raise_error(self):
        schema = IntField(required=True)
        value = 'abc'

        validator = SchemaValidator(schema=schema, value=value)

        try:
            validator.validate()
            self.fail()
        except SchemaValidationError as e:
            self.assertEqual(e.path, '$root')
            self.assertEqual(e.code, 'INT_TYPE')
Ejemplo n.º 13
0
    def test_value_less_should_raise_error(self):
        schema = IntField(min=1)
        value = 0

        validator = SchemaValidator(schema=schema, value=value)

        try:
            validator.validate()
            self.fail()
        except SchemaValidationError as e:
            self.assertEqual(e.path, '$root')
            self.assertEqual(e.code, 'INT_MIN')
Ejemplo n.º 14
0
    def test_not_accepted_value_should_raise_error(self):
        schema = EnumField(accept=['123'])
        value = '456'

        validator = SchemaValidator(schema=schema, value=value)

        try:
            validator.validate()
            self.fail()
        except SchemaValidationError as e:
            self.assertEqual(e.path, '$root')
            self.assertEqual(e.code, 'ENUM_VALUE_NOT_ACCEPT')
Ejemplo n.º 15
0
    def test_required_with_none_should_raise_error(self):
        schema = IntField(required=True)
        value = None

        validator = SchemaValidator(schema=schema, value=value)

        try:
            validator.validate()
            self.fail()
        except SchemaValidationError as e:
            self.assertEqual(e.path, '$root')
            self.assertEqual(e.code, 'REQUIRED_VALUE')
Ejemplo n.º 16
0
    def test_not_match_should_raise_error(self):
        schema = RegexField('\\d{5}\\Z')

        value = '1234'

        validator = SchemaValidator(schema, value)

        try:
            validator.validate()
            self.fail()
        except SchemaValidationError as e:
            self.assertEqual(e.code, 'REGEX_NOT_MATCH')
Ejemplo n.º 17
0
    def test_invalid_item_value_should_raise_error(self):
        schema = ListField(item_schema=BoolField())
        value = [True, False, 'zelda']

        validator = SchemaValidator(schema=schema, value=value)

        try:
            validator.validate()
            self.fail()
        except SchemaValidationError as e:
            self.assertEqual(e.path, '$root.$2')
            self.assertEqual(e.code, 'BOOL_TYPE')
Ejemplo n.º 18
0
    def test_more_items_should_raise_error(self):
        schema = ListField(item_schema=BoolField(), max_items=1)
        value = [True, False]

        validator = SchemaValidator(schema=schema, value=value)

        try:
            validator.validate()
            self.fail()
        except SchemaValidationError as e:
            self.assertEqual(e.path, '$root')
            self.assertEqual(e.code, 'LIST_MAX_ITEMS')
Ejemplo n.º 19
0
    def test_invalid_type_should_raise_error(self):
        schema = ListField(item_schema=BoolField())
        value = 'abc'

        validator = SchemaValidator(schema=schema, value=value)

        try:
            validator.validate()
            self.fail()
        except SchemaValidationError as e:
            self.assertEqual(e.path, '$root')
            self.assertEqual(e.code, 'LIST_TYPE')
Ejemplo n.º 20
0
    def test_raised_error_should_contain_node(self):
        age_field = IntField(min=18)

        schema = DictField(schema={'age': age_field})

        value = {'age': 12}

        try:
            validator = SchemaValidator(schema, value)
            validator.validate()
            self.fail()
        except SchemaValidationError as err:
            self.assertEqual(err.node, age_field)
Ejemplo n.º 21
0
    def test_multiple_schema_error_should_raise_error(self):
        schema = OrField(schemas=[StrField(), IntField()])

        value = True

        validator = SchemaValidator(schema, value)

        try:
            validator.validate()
            self.fail()
        except SchemaValidationError as e:
            self.assertEqual(e.code, 'OR_NO_MATCHING_SCHEMA')
            self.assertEqual(2, len(e.extra['errors']))
Ejemplo n.º 22
0
    def test_missing_prop_should_raise_error(self):
        schema = DictField(schema={'foo': BoolField(), 'bar': BoolField()})
        value = {'foo': True}

        validator = SchemaValidator(schema=schema, value=value)

        try:
            validator.validate()
            self.fail()
        except SchemaValidationError as e:
            self.assertEqual(e.path, '$root')
            self.assertEqual(e.code, 'DICT_PROP_MISSING')
            self.assertEqual(e.extra, {'prop': 'bar'})
Ejemplo n.º 23
0
    def test_invalid_nested_list_item_value_should_raise_error(self):
        schema = ListField(item_schema=DictField(
            schema={'values': ListField(item_schema=BoolField())}))
        value = [{'values': [True, False, 'Link']}]

        validator = SchemaValidator(schema=schema, value=value)

        try:
            validator.validate()
            self.fail()
        except SchemaValidationError as e:
            self.assertEqual(e.path, '$root.$0.values.$2')
            self.assertEqual(e.code, 'BOOL_TYPE')
Ejemplo n.º 24
0
    def test_nested_dict_with_invalid_prop_should_raise_error(self):
        schema = DictField(schema={
            'foo': BoolField(),
            'bar': DictField(schema={'baz': BoolField()})
        },
                           strict=True)
        value = {'foo': True, 'bar': {'baz': 123}}

        validator = SchemaValidator(schema=schema, value=value)

        try:
            validator.validate()
            self.fail()
        except SchemaValidationError as e:
            self.assertEqual(e.path, '$root.bar.baz')
            self.assertEqual(e.code, 'BOOL_TYPE')
Ejemplo n.º 25
0
    def test_not_allowed_prop_should_raise_error(self):
        schema = DictField(schema={
            'foo': BoolField(),
            'bar': BoolField()
        },
                           strict=True)
        value = {'foo': True, 'bar': False, 'baz': None}

        validator = SchemaValidator(schema=schema, value=value)

        try:
            validator.validate()
            self.fail()
        except SchemaValidationError as e:
            self.assertEqual(e.path, '$root')
            self.assertEqual(e.code, 'DICT_PROP_NOT_ALLOWED')
            self.assertEqual(e.extra, {'prop': 'baz'})