标签: jsonschema

“描述”是 JSON 模式中对象的有效“属性”吗?

假设我正在编写一个 JSON 模式。“类型”是“对象”。在对象的“属性”中包含名为“描述”的属性是否合法?我问这个问题是因为“描述”是 JSON 模式中的关键字。

示例:在此示例中,我展示了表示葡萄酒年份的 JSON 对象的简单模式。我指定了四个属性:三个必需属性(房屋、年份和葡萄品种)和一个可选属性,名为“描述”。

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "Wine vintage",
    "description": "JSON schema for wine vintages",
    "type": "object",
    "properties": {
        "house": {
            "description": "The name of the house that made the wine",
            "type": "string"
        },
        "year": {
            "description": "The year in which the wine was made",
            "type": "integer"
        },
        "varieties": {
            "description": "The grape varieties used to make the wine",
            "type": "array",
            "items": {
                "type": "string",
                "minItems": 1,
                "uniqueItems": true
            }
        }
        "description": {
            "description": …
Run Code Online (Sandbox Code Playgroud)

json keyword jsonschema

2
推荐指数
1
解决办法
934
查看次数

jsonSchema 按模式验证数字键

你能帮我吗,我如何验证以下 json 中列表项的“键”(例如“1”、“2”、“3”):

   {
    "list" : {
        "1" : {
            "element1" : "1",
            "element2" : "2"
        },
        "2" : {
            "element1" : "1",
            "element2" : "2"
        },
        ....
        "512" : {
            "element1" : "1",
            "element2" : "2"
        }
    }
   }
Run Code Online (Sandbox Code Playgroud)

请给我一些常见的例子来验证 json 中的密钥。

json jsonschema json-schema-validator

2
推荐指数
1
解决办法
4093
查看次数

对象数组的 Json 架构未验证

我有一个 json 响应的架构

{
    "title": "Products",
    "description": "schema for products",
    "type": "array",
    "properties": {
        "id": {
            "description": "id of a product",
            "type": "integer"
        },
        "name": {
            "description": "name of the product",
            "type": "string"
        },
        "created_at": {
            "description": "record created_at",
            "type": "string",
            "format": "date-time"
        },
        "updated_at": {
            "description": "record updated_at",
            "type": "string",
            "format": "date-time"
        }
    },
    "required": ["id", "name"]
}
Run Code Online (Sandbox Code Playgroud)

我想将此架构与此 json 相匹配

[{
    "id": 1,
    "name": "Cricket Ball"
}, {
    "id": 2,
    "name": "Soccer Ball"
}, {
    "id": 3,
    "name": …
Run Code Online (Sandbox Code Playgroud)

arrays json jsonschema

2
推荐指数
1
解决办法
2719
查看次数

使用其他必填字段扩展$ ref

给定一个location类似这样的模式的一部分:

{
  type: 'object',
  required: [ 'country' ],
  additionalProperties: false,
  properties: {
    country: {
      enum: [ 'DE', 'CH', 'AT' ],
    },
    postalCode: { type: 'string' },
  },
};
Run Code Online (Sandbox Code Playgroud)

在我们的大多数用例中,只有国家/地区代码才能使位置有效.但是我们有几次需要邮政编码.

例如,在我们的案例中,公司总是需要postalCode.我们以前的location架构当然没有强制执行.其他模式需要不强制存在邮政编码的位置对象.这是我们公司的架构:

{
  type: 'object',
  required: [ 'name', 'location' ],
  additionalProperties: false,
  properties: {
    location: { $ref: 'location' },
    name: { type: 'string' },
    website: { type: 'string' },
  },
};
Run Code Online (Sandbox Code Playgroud)

在JSON Schema中是否有一种方法可以使用$ ref但是还要扩展它以使模式中的location属性company自动需要postalCode?我们当前的解决方案是基于location我们简单地更改required属性的位置创建第二个模式,但我希望有更好的方法.

谢谢.

jsonschema

2
推荐指数
1
解决办法
927
查看次数

Jsonschema RefResolver 解析 python 中的多个引用

我们如何使用 jsonschema.RefResolver 验证模式中的多个引用?

我有一个验证脚本,如果我在文件中有一个引用,它就可以很好地工作。我现在在一个架构中有两个或三个引用,它们位于不同的目录中。

base_dir = '/schema/models/'
with open (os.path.join(base_dir, 'Defined.json')) as file_object:
    schema = json.load(file_object)
    resolver = jsonschema.RefResolver('file://' + base_dir + '/' + 'Fields/Ranges.json', schema)
    jsonschema.Draft4Validator(schema, resolver=resolver).validate(data)
Run Code Online (Sandbox Code Playgroud)

我的 json 架构:

{
  "properties": {
    "description": {
        "type": "object",
        "after": {"type": ["string", "null"]},
        "before": {"type": "string"}
      },
      "width": {"type": "number"} ,
      "range_specifier": {"type": "string"},
      "start": {"type": "number", "enum" : [0, 1] } ,
      "ranges": {
        "$ref": "Fields/Ranges.json"
      },
      "values": {
        "$ref": "Fields/Values.json"
      }
  }
}
Run Code Online (Sandbox Code Playgroud)

所以我的问题是我应该有两个解析器,一个用于范围,一个用于值,并在 Draft4Validator 中分​​别调用解析器?或者有没有更好的方法来做到这一点?

python jsonschema

2
推荐指数
1
解决办法
5112
查看次数

Json Schema:声明一种类型或另一种类型的数组

我如何声明一个属性,它可以是一种类型的数组,即字符串,或另一种类型,即整数?我用谷歌搜索,我能找到的只是一种声明混合类型数组的方法。

我尝试了以下架构定义:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "definitions": {
    "content": {
      "oneOf": [
        {
          "type": "array",
          "items": [
            {
              "type": "string"
            }
          ]
        },
        {
          "type": "array",
          "items": [
            {
              "type": "integer"
            }
          ]
        }
      ]
    }
  },
  "type": "object",
  "properties": {
    "content": {
      "$ref": "#/definitions/content"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

这验证

{
  "content": [
     1, 2
  ]  
}
Run Code Online (Sandbox Code Playgroud)

{
  "content": [
     "a", "b"
  ]  
}
Run Code Online (Sandbox Code Playgroud)

但也验证

{
  "content": [
     1, "a"
  ]  
}
Run Code Online (Sandbox Code Playgroud)

我应该认为无效。

jsonschema

2
推荐指数
1
解决办法
2745
查看次数

使用 Fastify 和 AJV 验证请求模式

背景

我正在使用 Fastify 构建 API,这是我第一次使用 JSON 模式验证。我们的想法是,它既可以提高服务器代码的效率,又可以帮助我们的开发人员学习如何使用我的 API。

问题

我正在尝试验证允许客户端仅按名称查询小猫的路由。一个成功形成的查询看起来是/kittens?name=fluffykins

我这条路线的架构如下所示:

{
  querystring: {
    type: 'object',
    name: { type: 'string' },
  }
}
Run Code Online (Sandbox Code Playgroud)

如何让我的模式验证器只接受查询name并拒绝其他查询,例如/kittens?age=1?我更喜欢模式验证器独立于我的控制器代码处理它,并且它还支持我们将来可能添加的查询。

谢谢!

validation json jsonschema ajv

2
推荐指数
1
解决办法
1293
查看次数

使用 json-schema 来要求或禁止基于另一个属性值的属性?

我试图在 json-schema 中完成的是:当属性enabled是 时true,应该需要某些其他属性。当 时false,应该禁止这些属性。

这是我的 json 架构:

{
  "type": "object",
  "properties": {
    "enabled": { "type": "boolean" }
  },
  "required" : ["enabled"],
  "additionalProperties" : false,
  "if": {
    "properties": {
      "enabled": true
    }
  },
  "then": { 
    "properties": {
      "description" : { "type" : "string" },
      "count": { "type": "number" }
    },
    "required" : ["description", "count"]
  }
}
Run Code Online (Sandbox Code Playgroud)

使用ajv6.5 版进行验证count,无论 的值如何,这都会导致 require等enabled。例如,对于数据:

{ "enabled": false }
Run Code Online (Sandbox Code Playgroud)

我的验证错误是:

[ { keyword: …
Run Code Online (Sandbox Code Playgroud)

jsonschema ajv

2
推荐指数
1
解决办法
3842
查看次数

JSON Schema - 基于选定值的下拉列表其他下拉列表

假设我有以下数据结构

Option A
Option B
    Option B1
        Option B2.1
        Option B2.2
Run Code Online (Sandbox Code Playgroud)

我正在尝试创建一个允许这样做的枚举结构,以便我可以根据先前选择的下拉列表的值生成一个下拉列表。

例子 :

Select option A -> No additional dropdowns
Select option B -> Generate dropdown with Option B1
Select option B1 -> Generate dropdown with Option B2.1 and Option B2.2
Run Code Online (Sandbox Code Playgroud)

当你想用 childeren 创建一个数组时,这会起作用 enums 是否有任何类似的可能

{
  "$schema": "http://json-schema.org/draft-07/schema#",

  "definitions": {
    "medicine": {
      "type": "object",
      "properties": {
        "value": { "type": "string" },
        "categories": {
          "type": "array",
          "items": { "$ref": "#/definitions/medicine" }
        }
      }
    }
  },
  "properties": {
    "person": { …
Run Code Online (Sandbox Code Playgroud)

json jsonschema json-schema-validator dropdown

2
推荐指数
1
解决办法
2032
查看次数

具有随机数键的对象的 JSON 模式

我有一个对象,它的键是数字,它的值是字符串:

{
  0: 'blah',
  2: 'blah'
}
Run Code Online (Sandbox Code Playgroud)

这个对象是什么,每个键都是一个包含错误的数组的索引,字符串描述了那个错误。我能找到的最接近这个架构的是:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "0": {
      "type": "string"
    },
    "2": {
      "type": "string"
    }
  },
  "required": [
    "0",
    "2"
  ]
}
Run Code Online (Sandbox Code Playgroud)

然而,这是不准确的。也许下次只有索引“4”会出错。有没有办法在对象中描述动态键?

jsonschema

2
推荐指数
1
解决办法
552
查看次数