I have the following example circuit:
{ "id": "http://schema.acme.com/widgets", "$schema": "http://json-schema.org/draft-06/schema#", "definitions": { "bentWidget": { "type": "object", "required": ["angle", "baseWidget"], "properties": { "angle": { "type": "number", "minimum": 0, "maximum": 90 }, "baseWidget": { "$ref": "#/definitions/baseWidget" } } }, "highPowerWidget": { "type": "object", "required": ["power", "baseWidget"], "properties": { "power": { "type": "number", "minimum": 101, "maximum": 200 }, "baseWidget": { "$ref": "#/definitions/baseWidget" } } }, "color": { "description": "the color of a widget", "type": "string" }, "baseWidget": { "description": "The base type for a widget", "type": "object", "required": [ "title", "version", "colors" ], "properties": { "title": { "type": "string", "maximum": 100, "minimum": 1, "pattern": "^[a-zA-Z]+((_[a-zA-Z]+)*|([a-zA-Z]+_)*|_)" }, "flanged": { "type": "boolean" }, "version": { "type": "string", "maximum": 64, "minimum": 1 }, "colors": { "type": "array", "items": { "$ref": "#/definitions/color" } } } } }, "anyOf": [ { "$ref": "#/definitions/baseWidget" }, { "$ref": "#/definitions/bentWidget" }, { "$ref": "#/definitions/highPowerWidget" } ] }
and I want to check it, so I write this to a file:
{ "type": "highPowerWidget", "title": "foobar", "version": "foo" }
and then I run ajv on it from the shell
$ ajv -s widgetSchema.json -d widget-highPower.json widget-highPower.json valid
And he tells me that it is valid, and that is not true, for HighPowerWidget there must be a power property and a property of "inherited" and colors.
I was able to test my individual schemas by deleting the anyOf section and entering something like this:
"properties": { "testObject": { "type": "object", "oneOf": [ { "$ref": "#/definitions/baseWidget" }, { "$ref": "#/definitions/bentWidget" }, { "$ref": "#/definitions/highPowerWidget" } ] } }, "required": [ "testObject" ]
and checking this json:
{ "testObject": { "type" : "highPowerWidget", "title" : "title", "version" : "baz", "colors" : [ "red", "green", "blue"], "flanged" : true } }
But this seems wrong for two reasons.
- I do not need to have a testObject property
- The oneOf function only worked when I enumerated a single line using several lines confirming something.
I think I have a basic misunderstanding of how I can write, or at least test my circuits. My goal is to have an array of objects that everyone checks, and a set of files that all fail. That way I can unit test my circuit.