简体   繁体   English

对象的 JSON Schema 构造。 共同属性,但某些属性不同

[英]JSON Schema construction for a objects with. common properties but differing in some properties

I have aa number of objects which share a common set of features but differ in one or more properties.我有许多对象,它们共享一组共同的功能,但在一个或多个属性上有所不同。 The common content is specified as media content in the definitions.公共内容在定义中被指定为媒体内容。 I have provided one such object with a 'format' property, but there are other objects, omitted to keep it short, that also have additional properties.我已经提供了一个具有“格式”属性的此类对象,但还有其他对象,为了保持简短而省略,它们也具有其他属性。 Here is a snippet of my attempt at constructing the schema.这是我尝试构建模式的片段。 Is this the correct way to accomplish, this?这是完成的正确方法吗? Many thanks非常感谢

 "definitions": {
 "media-content":{
  "type": "object",
  "title": {
    "type": "string"
  },
  "related-media": {
    "type": "object",
    "additionalProperties": {
      "type": "string"
    }
  }
 },

 "type": "object",
 "properties": {
    "type": {
        "format": "string",
        "enum":["audio", "video"]
    },
    "category": {
        "$ref": "#/definitions/media-content"
    }             
 }

Is this the way to do it?这是这样做的方法吗?

The first thing that stands out to me is that this isn't valid JSON Schema.让我印象深刻的第一件事是这不是有效的 JSON 模式。

The title keyword provides a title for the schema so it expects a string, but because you've provided a schema, it looks like you're wanting it to be a property. title关键字为模式提供了一个标题,因此它需要一个字符串,但是因为您已经提供了一个模式,所以看起来您希望它是一个属性。 Similarly related-media looks like you expect this to be a property.同样related-media看起来您希望这是一个属性。 Are you missing wrapping these in a properties keyword, like you have later for type and category ?您是否缺少将这些包装在properties关键字中,就像您稍后对typecategory一样?

These changes would make media-content look like this:这些更改将使media-content看起来像这样:

"media-content":{
  "type": "object",
  "properties": {
    "title": {
      "type": "string"
    },
    "related-media": {
      "type": "object",
      "additionalProperties": {
        "type": "string"
      }
    }
  }
}

I have provided one such object with a 'format' property我提供了一个具有“格式”属性的此类对象

Again, here, I'm not sure what you're getting at.再说一次,在这里,我不确定你在说什么。

"properties": {
    "type": {
        "format": "string",
        "enum":["audio", "video"]
    },
    "category": {
        "$ref": "#/definitions/media-content"
    }             
 }

This says you're expecting type to be a property in your object, but format isn't used right.这表示您希望type是对象中的一个属性,但format使用不正确。 Although the format keyword has some predefined types and does accept custom values, the way you're using it look like you really want to be using the type keyword.尽管format关键字有一些预定义的类型并且确实接受自定义值,但您使用它的方式看起来就像您真的想要使用type关键字一样。 In the end, it doesn't matter because enum will restrict the value to the items you declare in the array ( "audio" or "video" ).最后,这并不重要,因为enum会将值限制为您在数组中声明的项目( "audio""video" )。

It might be easier to see what you're trying to do if you posted a full minimum exaple.如果您发布了完整的最低示例,可能会更容易看到您正在尝试做什么。

That said, I'll try to build one to answer the question I think you're asking.也就是说,我将尝试构建一个来回答我认为您要问的问题。


It sounds like you're wanting to build a polymorphic relationship: an inheritance hierarchy where a base type defines some properties and a number of derived types define additional properties.听起来您想要建立一种多态关系:一个继承层次结构,其中基类型定义一些属性,而许多派生类型定义附加属性。

There's not really a good way to do that with JSON Schema because JSON Schema is a constraints system.使用 JSON Schema 并没有真正的好方法,因为 JSON Schema 是一个约束系统。 That is, you start with {} where anything is valid, and by adding keywords, you're reducing the number of values that are valid.也就是说,您从任何有效的{}开始,通过添加关键字,您可以减少有效值的数量。

Still, you might be able to achieve something close by using allOf and $ref .不过,您也许可以通过使用allOf$ref来实现一些接近的目标。

First, declare the base property set in its own schema.首先,在其自己的模式中声明基本属性集。 I'd separate them into independent schemas for easier handling.我会将它们分成独立的模式以便于处理。 You also need to give it an $id .您还需要给它一个$id

{
  "$id": "/base-type"
  "type": "object",
  "properties": {
    "base-prop-1": { "type": "string" },
    "base-prop-2": { "type": "number" }
  }
}

Next, for each of your "derived" schemas, you'll want to create a new schema, each with their own $id value, that references the base schema and declares its own additional requirements.接下来,对于每个“派生”模式,您需要创建一个新模式,每个模式都有自己的$id值,它引用基本模式并声明自己的附加要求。

{
  "$id": "/derived-type-1",
  "allOf": [
    { "$ref": "/base-type" },
    {
      "properties": {
        "derived-prop": { "type": "boolean" }
      }
    }
  ]
}

This second schema requires everything from the /base-type and also requires a derived-prop property that holds a boolean value.第二个模式需要/base-type中的所有内容,还需要一个包含布尔值的derived-prop属性。

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

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