Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我对ExclusiveEmimum属性的实现是否正确?_Python_Json_Jsonschema - Fatal编程技术网

Python 我对ExclusiveEmimum属性的实现是否正确?

Python 我对ExclusiveEmimum属性的实现是否正确?,python,json,jsonschema,Python,Json,Jsonschema,我正在使用JsonSchema 2.6.0验证python程序的表单数据 我试图实现exclusiveMinimum,但当我发布到表单时,它接受0作为有效值,但不应该 from jsonschema import Draft3Validator orderValidationSchema = { "$schema": "http://json-schema.org/draft-04/schema#", "type"

我正在使用
JsonSchema 2.6.0
验证python程序的表单数据

我试图实现
exclusiveMinimum
,但当我发布到表单时,它接受0作为有效值,但不应该

        from jsonschema import Draft3Validator

        orderValidationSchema = {
            "$schema": "http://json-schema.org/draft-04/schema#",
            "type": "object",
            "properties": {
                "total_amount": {
                    "$ref": "#/definitions/floatRef",
                    "required": "true",
                    "exclusiveMinimum": 0
                },
                "payable_amount": {
                    "$ref": "#/definitions/floatRef",
                    "required": "true",
                    "exclusiveMinimum": 0
                },
            },
            "definitions": {
                "floatRef": {
                    "type": "number",
                },
            }
        }
在浏览了一些git问题和其他链接之后,我尝试了下面所示的方法,但仍然没有成功

        from jsonschema import Draft3Validator

        orderValidationSchema = {
            "$schema": "http://json-schema.org/draft-04/schema#",
            "type": "object",
            "properties": {
                "total_amount": {
                    "$ref": "#/definitions/floatRef",
                    "required": "true",
                    "minimum": 0,
                    "exclusiveMinimum": "true"
                },
                "payable_amount": {
                    "$ref": "#/definitions/floatRef",
                    "required": "true",
                    "minimum": 0,
                    "exclusiveMinimum": "true"
                },
            },
            "definitions": {
                "floatRef": {
                    "type": "number",
                },
            }
        }
如果我有任何错误,请务必告诉我

我正在使用
Draft3Validator
,以防出现任何与之相关的问题

下面是传递到此模式的json

    {
         "total_amount" : 100000,
         "payable_amount" : 10000
    }

您的架构存在一些问题。 首先,
exclusiveMinimum
必须是布尔值,而不是字符串。 其次,
required
需要是对象级别的,而不是属性级别,因为模式被指定为草稿4模式。如果可能,您应该使用Draft4Validator

第三,
$ref
。这个关键字一直到draft-7,替换了整个对象的内容,这意味着该对象中的其他关键字将被忽略。 解决方案是将要应用于实例属性的两个模式包装在
allOf
中。我已经用下面的模式演示了这一点,它似乎满足了您的要求

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "total_amount": {
      "$ref": "#/definitions/floatRefMTZ"
    },
    "payable_amount": {
      "$ref": "#/definitions/floatRefMTZ"
    }
  },
  "required": ["total_amount", "payable_amount"],
  "definitions": {
    "floatRef": {
      "type": "number"
    },
    "floatRefMTZ": {
      "allOf": [
        {
          "$ref": "#/definitions/floatRef"
        },
        {
          "minimum": 0,
          "exclusiveMinimum": true
        }
      ]
    }
  }
}

(“MTZ”只是零以上的简写。你可以随意称呼它。)

你能提供你要验证的JSON实例吗?一个问题是你使用的是
Draft3Validator
,但是你的模式识别为草稿-4 JSON模式。我已经用我传递的JSON更新了这个问题。至于另一个问题,
Draft3validator
在我引用draft-4json模式创建的另一个模式中工作得很好。太好了!我有一个解决办法给你。现在发布。谢谢,它工作了!不过需要一些调整,因为draft3validators不支持列表。将此标记为正确答案。谢谢!不客气。是的,如果您不能使用draft-4,那么您必须修改
required
关键字的用法。我们强烈建议您尽可能使用最新的草稿。该实现支持3、4和6。是否有任何原因导致您无法从草稿-3中移动?它正在与我们的应用程序中的其他服务一起使用,并且存在一些我们现在不想解决的依赖关系。但在接下来的几个月里,我们最终会进行草案4。啊,好的。如果可以的话,我会转到草案6,因为它比4更接近最终规范。支持7与该库也在路上!