【问题标题】:json schema - require an object to not have a certain namejson 模式 - 要求对象没有特定名称
【发布时间】:2018-05-19 01:21:23
【问题描述】:

我想根据以下设置对象创建一个 json 架构(draft-06):

"settingData" : {
  "$id": "#settingData",
  "oneOf" : [
    { "type"  : "number" },
    { "type" : "array", "items" : { "oneOf" : [ { "type" : "number"},{ "type" : "null" } ] } },
    { "type" : "array", "items" : { "$ref" : "#setting" } }
  ]
},
"setting" : {
  "$id": "#setting",
  "type" : "object",
  "properties" : {
    "name": {
      "type": "string",
      "title": "type of setting",
      "examples": [
        "led-brightness"
      ]
    },
    "data": {
      "$ref" : "#settingData"
    }
  },
  "required" : ["type","data"]
},

我想创建这个对象的特化,它的设置名称为“status-ranges”,并包含上述定义的子集。 当名称为“status-ranges”时,如何使上述类型的“设置”不适用。 我考虑过使用 pattern,但是匹配除某个字符串之外的所有内容的正则表达式模式似乎有点特别,我尝试了一个,但它不起作用。然后是 not 关键字,但我仍在寻找如何应用它的示例。 有任何想法吗 ?

【问题讨论】:

标签: jsonschema


【解决方案1】:

在设置的名称属性中使用正则表达式对我有用:“模式”:“^((?!status-ranges).)*$”。这会从允许的名称中排除字符串“status-ranges”。'specialized' 设置有一个 const 名称“status-ranges”,如下所示:

"settingData" : {
  "$id": "#settingData",
  "oneOf" : [
    { "type"  : "number" },
    { "type" : "array", "items" : { "oneOf" : [ { "type" : "number"},{ "type" : "null" } ] } },
    { "type" : "array", "items" : { "$ref" : "#setting" } }
  ]
},
"setting" : {
  "$id": "#setting",
  "type" : "object",
  "properties" : {
    "name": {
      "type": "string",
      "pattern": "^((?!status-ranges).)*$",
      "title": "type of setting",
      "examples": [
        "led-brightness"
      ]
    },
    "data": {
      "$ref" : "#settingData"
    }
  },
  "required" : ["type","data"]
},
"statusRange" : {
  "$id": "#statusRange",
  "type" : "object",
  "properties" : {
    "name": {
      "const": "status-ranges"
    },
    "data": {
      "$ref" : "#settingData"
    }
  },
  "required" : ["type","data"]
},

我仍然想知道“不”在 JSON Schema 中是如何工作的。

【讨论】:

  • "pattern": "^((?!status-ranges).)*$" 更改为 "not":{"enum":["status-ranges"]}。或者使用draft-06,以后可以是"not":{"const":"status-ranges"}。验证器将检查该值是否与定义为常量值的 not 子模式无效。
猜你喜欢
  • 2020-10-31
  • 2015-05-29
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多