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

Is my implementation of exclusiveMinimum property correct?

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

我正在尝试实现 exclusiveMinimum 但是当我 post 到表单时它接受 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 需要是对象级别,而不是 属性 级别,因为架构被指定为 draft-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" 只是 shorthand 表示大于零。您可以随意称呼它。)