相关疑难解决方法(0)

基于 JSON Schema 中的枚举值的属性

我正在构建一个 json 模式定义,它具有一组固定的控件,我目前使用enum. 但是,并非所有属性都与所有控件相关。

options如果controlType=,我只想要求一个属性dropdown

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "array",
  "items": {
    "type": "object",
    "properties": {
      "controlType": {
        "type": "string",
        "enum": ["title", "dropdown", "button"]
      },
      "options:": {
        "type": "array",
        "items": {"type": "string"}
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

如何有条件地在 json 模式中包含/需要一个字段?

schema json jsonschema

6
推荐指数
1
解决办法
4193
查看次数

有条件需要的 jsonSchema 属性取决于父对象

根据这个问题jsonschema attribute conditionally required,我可以应用条件必需的属性。但是,它只能依赖于同一级别对象的属性。在某些情况下,我希望所需的一个属性依赖于其父对象属性,这可能吗?对于下面的例子:

{
  type: 'object',
  properties: {
    { 
      os: { type: 'string', enum: ['macOs', 'windows'] },
      specs: {
        macModel: { 
          type: 'string', 
          enum: ['macbook air', 'macbook pro', 'macbook']
        },
        memory: { type: 'number' }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

是否可以满足这个要求:仅当/os等于macOs时才需要/spec/macModel

validation jsonschema conditional-statements

5
推荐指数
1
解决办法
2371
查看次数

jsonschema 枚举值以另一个枚举值为条件

我有一个适用于我的应用程序的可接受输入组合表:

noises   appearance
------   ----------
squeaks  fluffy
purrs    fluffy
hisses   fluffy
peeps    feathers
chirps   feathers
squeaks  feathers
hisses   scaly
Run Code Online (Sandbox Code Playgroud)

任何其他值的组合都是不可接受的。

如何在 JSON Schema 中对其进行编码?“架构的其余部分”看起来有点像这样:

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

目前我的应用程序正在使用 Draft 4,因为它是jsonschema 包的最后一个稳定版本所支持的。

jsonschema

5
推荐指数
1
解决办法
4425
查看次数

JsonSchema:根据另一个属性的值验证类型

我使用以下架构来验证我的json:

{
    "$schema": "http://json-schema.org/schema#",
    "title": " Rules",
    "description": "Describes a set of rules",
    "type": "object",
    "properties": {
        "rules": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "precedence": {
                        "type": "number",
                        "minimum": 0
                    },
                    "conditions": {
                        "type": "array",
                        "items": {
                            "type": "object",
                            "properties": {
                                "field": {
                                    "type": "string",
                                    "enum": [ "Name", "Size" ]
                                },
                                "relation": {
                                    "type": "string",
                                    "enum": [ "is", "is not", "is not one of", "is one of" ]
                                },
                                "value": {
                                    "type": ["array", "string", "number"]
                                }
                            }, …
Run Code Online (Sandbox Code Playgroud)

json jsonschema

4
推荐指数
2
解决办法
4803
查看次数

JSON Schema `required` 允许使用空字符串作为值

我正在使用 JSON 模式模板来验证在线表单收到的数据。验证器的要求之一是它允许根据对其他问题给出的答案要求一些问题。

例如,如果问题是Do you want a loan?并且用户回答yes,则What is the loan to be used for?需要将问题的答案设置为 required 以便用户必须提供答案。如果答案是,no则不需要第二个问题。

我使用定义来定义我的问题,然后在下面的主要问题模式中引用它们。我通过使用 Draft-07 中提供的 if-then-else 功能读到了这一点,我可以使用它根据其他问题的答案来设置某些问题。

在这个特定的例子中,我想要发生的是,如果用户输入Home improvements (General)问题 9的答案,那么问题 257 将被设置为必需且必须回答,否则应该抛出错误。

目前,当我将此验证器输入https://www.jsonschemavalidator.net/ 时,它无法按预期工作。实际情况是,即使问题 9 的答案是“家庭装修(一般)”,问题 257 的答案也可以留空

如何更改我的架构以提供我想要的行为?

JSON 架构

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
    "question3-9": {
      "type": "object",
      "properties": {
        "answer": {
          "type": "string",
          "enum": [
            "Home improvements (General)",
            "Other"
          ]
        }
      }
    },
    "question3-257": {
      "type": "object",
      "properties": { …
Run Code Online (Sandbox Code Playgroud)

json jsonschema json-schema-validator

4
推荐指数
1
解决办法
9389
查看次数

如何在 JSON 模式中模拟 switch 语句 (switch-case)?

我正在使用 JSON 架构jsonschema来验证 JSON 记录。这是一个示例架构。这里只有两个案例,但想象一下类似的场景,其中有一百个像这样布置的案例。

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "oneOf": [
      {
        "type": "object",
        "required": ["a", "b", "c"],
        "properties": {
          "a": {"type": "integer", "enum": [0]},
          "b": {"type": "integer", "enum": [0, 2, 4, 6, 8]},
          "c": {"type": "string", "enum": ["always the same"]}
        }
      },
      {
        "type": "object",
        "required": ["a", "b", "c"],
        "properties": {
          "a": {"type": "integer", "enum": [1]},
          "b": {"type": "integer", "enum": [1, 3, 5, 7, 9]},
          "c": {"type": "string", "enum": ["always the same"]}
        }
      }
    ]
}
Run Code Online (Sandbox Code Playgroud)

关键问题是字段的重复"c" …

validation jsonschema switch-statement python-jsonschema

3
推荐指数
1
解决办法
2856
查看次数

使用 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
查看次数