Ejemplo n.º 1
0
    def test_adjacent_reference(self):
        register = trafaret_schema.Register()
        addresses = trafaret_schema.json_schema(
            {
                "$id": "http://yuhuhu.com/address",
                "type": "object",
                "properties": {
                    "billing_address": {
                        "$ref": "#/definitions/address"
                    },
                    "shipping_address": {
                        "$ref": "#/definitions/address"
                    },
                },
                "definitions": {
                    "address": {
                        "type": "object",
                        "properties": {
                            "street_address": {
                                "type": "string"
                            },
                            "city": {
                                "type": "string"
                            },
                            "state": {
                                "type": "string"
                            },
                        },
                        "required": ["city"],
                    },
                },
            },
            context=register,
        )
        person = trafaret_schema.json_schema(
            {
                "type": "object",
                "properties": {
                    "name": {
                        "type": "string"
                    },
                    "address": {
                        "$ref":
                        "http://yuhuhu.com/address#/definitions/address"
                    },
                },
            },
            context=register,
        )

        data = {
            'name': 'Peotr',
            'address': {
                'city': 'Moscow'
            },
        }
        assert person.check(data) == data

        register.validate_references()
 def test_schemas(self):
     for root, dirs, files in os.walk(self.schema_dir):
         for filename in files:
             path = op.join(root, filename)
             with open(path) as f:
                 schema = json.load(f)
                 print('Check', path)
                 trafaret_schema.json_schema(schema)
Ejemplo n.º 3
0
    def test_const(self):
        check = trafaret_schema.json_schema({
            'const': 'blabla',
        })
        self.assertEqual(check('blabla'), 'blabla')

        check = trafaret_schema.json_schema({
            'const': 100,
        })
        self.assertEqual(check(100), 100)
Ejemplo n.º 4
0
    def test_maximum(self):
        check = trafaret_schema.json_schema({
            'type': 'number',
            'maximum': 5,
        })
        with self.assertRaises(t.DataError):
            check(10)

        check = trafaret_schema.json_schema({
            'type': 'number',
            'exclusiveMaximum': 5,
        })
        with self.assertRaises(t.DataError):
            check(5)
Ejemplo n.º 5
0
 def test_dependencies(self):
     check = trafaret_schema.json_schema({
         'type': 'object',
         'properties': {
             'a': {
                 'type': 'number'
             }
         },
         'dependencies': {
             'a': ['b', 'c']
         },
     })
     self.assertEqual(check({
         'bla': 1,
         'blabla': 3
     }), {
         'bla': 1,
         'blabla': 3
     })
     with self.assertRaises(t.DataError):
         check({'a': 'b'})
     self.assertEqual(check({
         'a': 1,
         'b': 3,
         'c': 4
     }), {
         'a': 1,
         'b': 3,
         'c': 4
     })
Ejemplo n.º 6
0
 def test_number(self):
     check = trafaret_schema.json_schema({
         'type': 'integer',
     })
     self.assertEqual(check(100), 100)
     with self.assertRaises(t.DataError):
         check(100.4)
Ejemplo n.º 7
0
 def test_max_length(self):
     check = trafaret_schema.json_schema({
         'type': 'string',
         'maxLength': 5,
     })
     with self.assertRaises(t.DataError):
         check('blabla')
Ejemplo n.º 8
0
 def test_ipv6(self):
     schema = trafaret_schema.json_schema({
         "type": "object",
         "properties": {
             "ip": {"format": "ipv6"}
         },
     })
     schema({'ip': '::1'})
Ejemplo n.º 9
0
 def test_uniq(self):
     check = trafaret_schema.json_schema({
         'type': 'array',
         'uniqueItems': True,
     })
     with self.assertRaises(t.DataError):
         check([1, 2, 3, 4, 5, 5])
     self.assertEqual(check([1, 2, 3, 4, 5]), [1, 2, 3, 4, 5])
Ejemplo n.º 10
0
 def test_max_items(self):
     check = trafaret_schema.json_schema({
         'type': 'array',
         'maxItems': 5,
     })
     with self.assertRaises(t.DataError):
         check([1, 2, 3, 4, 5, 6])
     self.assertEqual(check([1, 2, 3, 4, 5]), [1, 2, 3, 4, 5])
Ejemplo n.º 11
0
 def test_enum(self):
     check = trafaret_schema.json_schema({
         'enum': ['blabla', 200],
     })
     self.assertEqual(check('blabla'), 'blabla')
     self.assertEqual(check(200), 200)
     with self.assertRaises(t.DataError):
         check(300)
Ejemplo n.º 12
0
 def test_multiple_of(self):
     check = trafaret_schema.json_schema({
         'type': 'number',
         'multipleOf': 5,
     })
     self.assertEqual(check(10), 10)
     with self.assertRaises(t.DataError):
         check(11)
Ejemplo n.º 13
0
 def test_pattern(self):
     check = trafaret_schema.json_schema({
         'type': 'string',
         'pattern': 'bla+',
         'maxLength': 10,
         'minLength': 5,
     })
     self.assertEqual(check('blablabla'), 'blablabla')
Ejemplo n.º 14
0
 def test_time(self):
     schema = trafaret_schema.json_schema({
         "type": "object",
         "properties": {
             "time": {"format": "time"}
         },
     })
     schema({'time': '19:59'})
Ejemplo n.º 15
0
 def test_phone(self):
     schema = trafaret_schema.json_schema({
         "type": "object",
         "properties": {
             "phone": {"format": "phone"}
         },
     })
     schema({'phone': '+7 927 728 67 67'})
Ejemplo n.º 16
0
 def test_date(self):
     schema = trafaret_schema.json_schema({
         "type": "object",
         "properties": {
             "datetime": {"format": "date"}
         },
     })
     schema({'datetime': '2017-09-02'})
Ejemplo n.º 17
0
 def test_required(self):
     check = trafaret_schema.json_schema({
         'type': 'object',
         'required': ['a', 'b'],
     })
     with self.assertRaises(t.DataError):
         check({'a': 1})
     self.assertEqual(check({'a': 1, 'b': 2}), {'a': 1, 'b': 2})
Ejemplo n.º 18
0
 def test_email(self):
     schema = trafaret_schema.json_schema({
         "type": "object",
         "properties": {
             "email": {"format": "email"}
         },
     })
     schema({'email': '*****@*****.**'})
Ejemplo n.º 19
0
 def test_min_props(self):
     check = trafaret_schema.json_schema({
         'type': 'object',
         'minProperties': 2,
     })
     with self.assertRaises(t.DataError):
         check({'a': 1})
     self.assertEqual(check({'a': 1, 'b': 2}), {'a': 1, 'b': 2})
Ejemplo n.º 20
0
 def test_not(self):
     check = trafaret_schema.json_schema({
         'not': {
             'minLength': 5
         },
     })
     self.assertEqual(check('bla'), 'bla')
     with self.assertRaises(t.DataError):
         check('blabla')
Ejemplo n.º 21
0
 def test_simple_items(self):
     check = trafaret_schema.json_schema({
         'type': 'array',
         'items': {
             'type': 'number'
         },
     })
     with self.assertRaises(t.DataError):
         check([1, 2, 'a', 4, 5, 5])
     self.assertEqual(check([1, 2, 3, 4, 5]), [1, 2, 3, 4, 5])
Ejemplo n.º 22
0
def test_number():
    reg = trafaret_schema.Register()
    schema = trafaret_schema.json_schema(
        {
            "type": "number",
        },
        reg,
    )
    assert schema('100.0') == '100.0'
    assert schema(decimal.Decimal('100.0')) == decimal.Decimal('100.0')
Ejemplo n.º 23
0
 def test_contains(self):
     check = trafaret_schema.json_schema({
         'type': 'array',
         'contains': {
             'type': 'number'
         },
     })
     with self.assertRaises(t.DataError):
         check(['a', 'b', 'c'])
     self.assertEqual(check(['a', 'b', 'c', 5]), ['a', 'b', 'c', 5])
Ejemplo n.º 24
0
def test_number_as_decimal():
    reg = trafaret_schema.Register()
    reg.reg_format("decimal", decimal.Decimal)
    schema = trafaret_schema.json_schema(
        {
            "type": "number",
            "format": "decimal",
        },
        reg,
    )
    assert schema('100.0') == decimal.Decimal('100.0')
Ejemplo n.º 25
0
 def test_properties(self):
     check = trafaret_schema.json_schema({
         'type': 'object',
         'properties': {
             'a': {
                 'type': 'number'
             }
         },
     })
     with self.assertRaises(t.DataError):
         check({'a': 'b'})
     self.assertEqual(check({'a': 1, 'b': 2}), {'a': 1, 'b': 2})
Ejemplo n.º 26
0
    def test_reg_format(self):
        register = trafaret_schema.Register()
        register.reg_format('any_ip', t.IP)

        schema = trafaret_schema.json_schema({
                "type": "object",
                "properties": {
                    "ip_addr": {"format": "any_ip"}
                },
            },
            context=register,
        )
        schema({'ip_addr': '192.168.0.1'})
        schema({'ip_addr': '::1'})
Ejemplo n.º 27
0
 def test_all_of(self):
     check = trafaret_schema.json_schema({
         'allOf': [
             {
                 'minLength': 5
             },
             {
                 'maxLength': 10
             },
         ],
     })
     self.assertEqual(check('blabla'), 'blabla')
     with self.assertRaises(t.DataError):
         check('bla')
Ejemplo n.º 28
0
 def test_positional_items(self):
     check = trafaret_schema.json_schema({
         'type':
         'array',
         'items': [{
             'type': 'number'
         }, {
             'type': 'string'
         }],
     })
     with self.assertRaises(t.DataError):
         # bad 2nd position
         check([1, None])
     with self.assertRaises(t.DataError):
         # too long array
         check([1, 'a', 4, 5, 5])
     self.assertEqual(check([1, 'a']), [1, 'a'])
Ejemplo n.º 29
0
 def test_property_names(self):
     check = trafaret_schema.json_schema({
         'type': 'object',
         'propertyNames': {
             'type': 'string',
             'pattern': 'bla+',
         },
     })
     with self.assertRaises(t.DataError):
         check({'a': 'b'})
     self.assertEqual(check({
         'bla': 1,
         'blabla': 3
     }), {
         'bla': 1,
         'blabla': 3
     })
Ejemplo n.º 30
0
 def test_additional_items(self):
     check = trafaret_schema.json_schema({
         'type':
         'array',
         'items': [{
             'type': 'number'
         }, {
             'type': 'string'
         }],
         'additionalItems': {
             'type': 'number'
         },
     })
     with self.assertRaises(t.DataError):
         check([1, None, 4, 5, 5])
     with self.assertRaises(t.DataError):
         check([1, 'a', 'a', 5, 5])
     self.assertEqual(check([1, 'a', 5, 5, 5]), [1, 'a', 5, 5, 5])