可以引用JSON整数属性吗? [英] Can JSON integer attributes be referenced?

查看:122
本文介绍了可以引用JSON整数属性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下要验证的JSON模式和Python单元测试.

I have the following JSON schema that I want to validate and Python unittest.

{
    "properties": {

        "traffic_parameters" {

            "capacity": {

                "oneOf": [
                    {
                        "max_percentage": {

                            "type": "integer",
                            "minimum" : 1,
                            "maximum" : 150
                        },

                        "min_percentage": {

                            "type": "integer",
                            "minimum" : 1,
                            "maximum" : {

                                "$ref": "#/properties/traffic_parameters/capacity/oneOf/max_percentage/minimum"
                            }
                        }
                    },

                    {
                        "percentage_range": {

                            "type": "array",
                            "minItems": 1,
                            "maxItems": 10,
                            "items": {

                                "type": "integer"
                            }
                        }
                    }
                ]
            }
        }
    }
}

使用jsonschema验证整个模式文件,确定. 但是,在编写单元测试时,出现以下错误;

Using jsonschema I validate the whole schema file OK. However, on writing unittests I get the following error;

capacity = {'oneOf': [{'max_percentage': {'type': 'integer', 'minimum': 1, 'maximum': 150}, 'min_percentage': {'type': 'integer', 'minimum': 1, 'maximum': {'$ref': '#/properties/traffic_parameters/capacity/oneOf/max_percentage/minimum'}}}, {'percentage_range': {'type': 'array', 'minItems': 1, 'maxItems': 10, 'items': {'type': 'integer'}}}]}
-----------------------------------------
index0 = {'max_percentage': {'type': 'integer', 'minimum': 1, 'maximum': 150}, 'min_percentage': {'type': 'integer', 'minimum': 1, 'maximum': {'$ref': '#/properties/traffic_parameters/capacity/oneOf/max_percentage/minimum'}}}
-----------------------------------------
min_percentage = {'type': 'integer', 'minimum': 1, 'maximum': {'$ref': '#/properties/traffic_parameters/capacity/oneOf/max_percentage/minimum'}}
E.........
======================================================================
ERROR: test_invalid_minimum__traffic_parameters__capacity__min_percentage (__main__.SchemaTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_test-variables_schema.py", line 160, in test_invalid_minimum__traffic_parameters__capacity__min_percentage
    validate(0, min_percentage)
  File "/local/tools/PACKAGES/python3/lib/python3.6/site-packages/jsonschema/validators.py", line 540, in validate
    cls.check_schema(schema)
  File "/local/tools/PACKAGES/python3/lib/python3.6/site-packages/jsonschema/validators.py", line 83, in check_schema
    raise SchemaError.create_from(error)
jsonschema.exceptions.SchemaError: {'$ref': '#/properties/traffic_parameters/capacity/oneOf/max_percentage/minimum'} is not of type 'number'

Failed validating 'type' in schema['properties']['maximum']:
    {'type': 'number'}

On instance['maximum']:
{'$ref': '#/properties/traffic_parameters/capacity/oneOf/max_percentage/minimum'}


    FAILED (errors=1)

我的测试是Python单元测试,用于测试属性的最小值和最大值.最大值是对上面直接定义的另一个属性的引用.

My test are Python unit tests that tests the minimum and maximum of the attribute. The maximum being a ref to another attribute defined directly above.

所示的测试测试的是最小值,但它是错误的最大值.如果我正在测试最大值,则错误将是相同的,这正是我正在研究的内容.

The test shown tests the minimum but it is the maximum value that is in error. The error would be the same if I were testing the maximum, it's just what I was working on.

我尝试将类型重新指定为最大"的整数",但错误仍然存​​在.

I tried re-specifying the type as "integer" for "maximum" but the error remains.

如何在不更改属性类型的情况下克服此错误?该参考很重要,因为我希望此属性的最大值与上一个属性的最小值直接相关.

How can I get past this error without changing the type of the attribute? The reference is important because I want the maximum of this attribute to be directly related to the minimum of the previous attribute.

还有,在单元测试中还有另一种(更简便的)引用这些变量的方法吗?

Also, is there another (easier) way to reference these variables in unittests?

这是功能

def test_invalid_minimum__traffic_parameters__capacity__min_percentage(self):
    global test_schema

    capacity = test_schema["traffic_parameters"]["capacity"]
    print ("capacity = " + str(capacity))
    print ("-----------------------------------------")

    index0 = capacity["oneOf"][0]
    print ("index0 = " + str(index0))
    print ("-----------------------------------------")

    min_percentage = index0["min_percentage"]
    print ("min_percentage = " + str(min_percentage))

    with self.assertRaises(ValidationError ):
        validate(0, min_percentage)

谢谢.

推荐答案

JSON参考规范对可以$ref进行的内容没有任何限制,但实际上,我见过的任何验证器仅支持指向JSON模式的$ref.我不确定为什么没有人支持该功能,但是我还没有看到我认为做这样的事情是个好主意的情况.

The JSON Reference spec doesn't place any limits on what can be $ref'd, but in practice, any validator that I have ever seen only supports $refs that point to a JSON Schema. I'm not sure why none ever supported that functionality, but I have yet to see a case where I thought it was a good idea to do such a thing.

在最近发布的JSON Schema草案06中,仅支持JSON Schema的常见做法已成为一条规则.因此,即使您确实找到了支持$ref ing整数的验证器,我也不建议您使用它,因为它将来会使升级变得更加困难.

In the recently released JSON Schema draft-06, the common practice of supporting only JSON Schemas became a rule. So, even if you do find a validator that supports $refing integers, I wouldn't recommend using it because it will make it harder to upgrade in the future.

这篇关于可以引用JSON整数属性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆