简体   繁体   English

使用另一个 JSON 架构验证 JSON 架构

[英]Validate JSON Schema with another JSON Schema

I am trying to validate JSON Schema using another JSON Schema.我正在尝试使用另一个 JSON 架构来验证 JSON 架构。

Example of JSON Schema to validate: https://jsonschema.net/home要验证的 JSON 架构示例: https://jsonschema.net/home

Reference of Validation schema to validate above schema: https://github.com/ajv-validator/ajv/blob/master/lib/refs/json-schema-draft-07.json验证模式的参考以验证上述模式: https://github.com/ajv-validator/ajv/blob/master/lib/refs/json-schema-draft-07.json

I have a requirement where property can only be of primitive type ie string, number, integer, boolean .我有一个要求, property只能是原始类型,即string, number, integer, boolean Since the root of JSON schema should have type as object and all the properties inside it will have type as primitive type, I am not sure how should I define type definition that validates the type at root level as object while type inside properties as primitive type.由于 JSON 模式的根应该具有typeobject并且其中的所有properties都将具有type作为原始类型,我不确定我应该如何定义type定义以验证根级别的typeobject而内部properties type为原始类型.

Sample JSON:样品 JSON:

{
    "$schema": "http://json-schema.org/draft-07/schema",
    "$id": "http://example.com/example.json",
    "type": "object",
    "title": "The root schema",
    "description": "The root schema comprises the entire JSON document.",
    "default": {},
    "examples": [
        {
            "name": "A green door"
        }
    ],
    "required": [
        "name"
    ],
    "properties": {
        "name": {
            "$id": "#/properties/name",
            "type": "string",
            "title": "The name schema",
            "description": "An explanation about the purpose of this instance.",
            "default": "",
            "examples": [
                "A green door"
            ]
        }
    },
    "additionalProperties": true
}

Validation JSON that validates the type :验证type的验证 JSON :

  definitions: {
    simpleTypes: {
      enum: ['array', 'boolean', 'integer', 'null', 'number', 'object', 'string'],
    }
  },
  properties: {
  type: {
      anyOf: [
        { $ref: '#/definitions/simpleTypes' },
      ],
    },
  }

From the above simpleTypes -> enum if I remove object , my JSON becomes invalid.从上面的simpleTypes -> enum如果我删除object ,我的 JSON 变得无效。

Any way I can define enum for root type as different from type present inside properties ?有什么方法可以将根typeenum定义为与properties中存在的type不同?

Start by making a copy of the draft-07 schema and give it a unique $id .首先制作一个 draft-07 模式的副本,并给它一个唯一的$id

{
  "$schema": "http://json-schema.org/draft-07/schema",
  "$id": "https://example.com/my-custom-draft-07-schema",
  "definitions": {
    ...original-definitions...
  },
  ...original-schema...
}

Since you want different constraints for schemas in different places, you'll need to refactor the schema to a definition so it can be referenced and extended for different situations.由于您希望不同位置的模式有不同的约束,因此您需要将模式重构为定义,以便可以针对不同情况引用和扩展它。 Don't forget to change all the recursive references ( { "$ref": "#" } ) to point to the definition you created ( { "$ref": "#/definitions/schema" } ).不要忘记更改所有递归引用( { "$ref": "#" } )以指向您创建的定义( { "$ref": "#/definitions/schema" } )。

{
  "$schema": "http://json-schema.org/draft-07/schema",
  "$id": "https://example.com/my-custom-draft-07-schema",
  "alllOf": [{ "$ref": "#/definitions/schema" }],
  "definitions": {
    ...original-definitions-with-modified-$refs...
   "schema": {
      ...original-schema-with-modified-$refs...
    }
  }
}

Next you'll need to add another definition specific to property schemas and change the $refs for property schemas to use the definition that was created ( { "$ref": "#/definitions/property-schema" } ).接下来,您需要添加另一个特定于属性模式的定义,并更改属性模式的 $refs 以使用创建的定义 ( { "$ref": "#/definitions/property-schema" } )。 Don't forget to change patternProperties and additionalProperties as well as properties .不要忘记更改patternPropertiesadditionalProperties以及properties Other keywords, such as anyOf should continue to reference the generic schema ( { "$ref": "#/definitions/schema" } ).其他关键字,例如anyOf应该继续引用通用模式( { "$ref": "#/definitions/schema" } )。

{
  "$schema": "http://json-schema.org/draft-07/schema",
  "$id": "https://example.com/my-custom-draft-07-schema",
  "alllOf": [{ "$ref": "#/definitions/schema" }],
  "definitions": {
    ...original-definitions-with-modified-$refs...
    "schema": {
      ...original-schema-with-modified-$refs...
    },
    "property-schema": {
      "allOf": [{ "$ref": "#/definitions/schema" }]
    }
  }
}

At this point, the schema has just been refactored.此时,架构刚刚被重构。 None of the behavior has changed except that you now have a place to put your custom constraints so they will only apply where you want them to.除了您现在有一个放置自定义约束的地方之外,没有任何行为发生变化,因此它们只会应用到您想要它们的位置。 It should be relatively straightforward to add your constraints from here.从这里添加约束应该相对简单。

{
  "$schema": "http://json-schema.org/draft-07/schema",
  "$id": "https://example.com/my-custom-draft-07-schema",
  "alllOf": [{ "$ref": "#/definitions/schema" }],
  ...add-root-schema-constraints-here...
  "definitions": {
    ...original-definitions-with-modified-$refs...
    "schema": {
      ...original-schema-with-modified-$refs...
      ...add-global-constraints-here...
    },
    "property-schema": {
      "allOf": [{ "$ref": "#/definitions/schema" }]
      ...add-property-schema-constraints-here...
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM