13

Suppose I have couple of objects like Vehicle and 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. Do json-schema provide custom type support . 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

Rishi Saraf
  • 1,644
  • 2
  • 14
  • 27

2 Answers2

18

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.

{
    "$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 - i.e. the data must match the schema that describes what a vehicle looks like.

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 - i.e. 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. 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.

JsonSchema is about describing the shape of data - i.e. how primitive types are combined in some kind of structure, but says nothing about the identity. 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).

Gordon Bean
  • 4,272
  • 1
  • 32
  • 47
1

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.

Amit Bhati
  • 5,569
  • 1
  • 24
  • 45
  • Yeah that is one option I want to know if json-schema provide custom type support . So instead of saying "type": "object" can I say "type": "vehicle" – Rishi Saraf Aug 28 '15 at 05:12
  • 4
    The type keyword refers to JSON primitive types. It can not be extended since json-schema v4. You relate "parent" schemas through $ref. If you are thinking in schema inheritance please have a look to this previously answered question: http://stackoverflow.com/questions/27410216/json-schema-and-inheritance – jruizaranguren Aug 28 '15 at 09:57