简体   繁体   English

如何在 json-schema 中定义自定义对象类型

[英]How to define custom object type in json-schema

Suppose I have couple of objects like Vehicle and Computer.假设我有几个对象,如 Vehicle 和 Computer。

{"brand":"Ford", "dateOfManufacture":"23/082015"}
{"brand":"Apple", "dateOfManufacture":"23/082015"}

I know I can represent vehicle schema like below.我知道我可以表示如下的车辆模式。 However looking at schema doesn't tell me if its of Object type Vehicle or Computer.然而,查看模式并不能告诉我它是对象类型车辆还是计算机。 How can put that information in JSON.如何将该信息放入 JSON。 Do json-schema provide custom type support . json-schema 是否提供自定义类型支持。 So instead of saying "type": "object" can I say "type": "vehicle".因此,与其说“类型”:“对象”,不如说“类型”:“车辆”。

{
    "description": "schema validating people", 
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
      "properties": { 
            "firstName": {"type": "string"}, 
            "lastName": {"type": "string"}
        }
   }
}

TIA TIA

While you can't define a new type explicitly, you can define a schema describing what objects of that type look like, and then reference it in your master schema.虽然您无法明确定义新type ,但您可以定义一个描述该类型对象外观的架构,然后在您的主架构中引用它。

{
    "$schema": "http://json-schema.org/draft-07/schema",
    "definitions": {
        "vehicle": {
            "type": "object",
            "properties": {
                "brand": {
                    "type": "string", 
                    "enum": ["ford","toyota"]
                },
                "dateOfManufacture": {
                    "type": "string"
                }
            }
        }
    },
    "type": "object",
    "properties": {
        "primary": { "$ref": "#/definitions/vehicle" },
        "secondary": { "$ref": "#/definitions/vehicle" }
    }
}

This example describes an object with fields primary and secondary that are both of "type" vehicle - ie the data must match the schema that describes what a vehicle looks like.这个例子描述了一个对象,其字段primarysecondary都是“类型” vehicle ——即数据必须与描述vehicle外观的模式相匹配。

In typed programming languages, the concept of type is used to communicate the shape of data, but also something about the identity of that data - ie it gives an identity, or name, to the specific definition of a structure.在类型化编程语言中,类型的概念用于传达数据的形状,但也用于传达有关该数据身份的一些信息 - 即它为结构的特定定义提供身份或名称。

struct Foo { int a; string b; }
struct Bar { int a; string b; }

function quux(Foo foo) { ... }

In this dummy example, you can't pass a Bar into Quux , even though it looks just like a Foo .在这个虚拟示例中,您不能将Bar传递给Quux ,即使它看起来就像Foo This is because in addition to describing the shape of the data ( int a; string b; ), the type defines an identity to the data structure.这是因为除了描述数据的形状( int a; string b; )之外,该类型还定义了数据结构的身份。

JsonSchema is about describing the shape of data - ie how primitive types are combined in some kind of structure, but says nothing about the identity. JsonSchema 是关于描述数据的形状——即原始类型如何组合在某种结构中,但没有说明身份。 It cares about the names of fields and how they are structured, but doesn't care about what you called the schema (or analogously, the name of the struct ).它关心字段的名称以及它们的结构方式,但不关心您将架构称为什么(或者类似地, struct的名称)。

You can add product type also in schema like:-您也可以在架构中添加产品类型,例如:-

{"brand":"Ford", "dateOfManufacture":"23/082015", "productType":"vehicle"}
{"brand":"Apple", "dateOfManufacture":"23/082015", "productType":"computer"}

While deciding schema, you can ensure that it has all the necessary information for the classification of products.在决定模式时,您可以确保它具有产品分类所需的所有信息。

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

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