I am trying to validate this specific schema:
{
"messages": [
{
"name": "test msg",
"id": "0x100",
"signals": {
"0": {"name": "Engine RPM", "bit_length": 16},
"16": {"name": "Gear", "bit_length": 3},
"19": {"name": "Battery Voltage", "bit_length": 5}
}
}
]
}
I am using the python from jsonschema import Draft4Validator to validate this schema... however I am not sure how to continue.
This is my current schema validation so far:
{
"$schema" : "https://json-schema.org/schema#",
"type" : "object",
"properties" :
{
"messages" :
{
"type" : "array",
"items" :
{
"properties" :
{
"name" :
{
"type" : "string"
},
"id" :
{
"type" : "string"
},
"signals" :
{
"type" : "object"
},
"properties" :
{
}
},
"required": ["name", "id", "signals"]
}
}
}
}
The problem I am facing is I am not sure how to deal with the objects that are in the "signals" key as they start with a string and are NOT consistent ("0", "16", "19")... How could I go about validating this by ensuring the type is always a string, disregarding whether or not the string is consistent.
Thanks to all of those who reply in advance.
I was able to accomplish this by doing the following:
{
"$schema" : "https://json-schema.org/schema#",
"type" : "object",
"properties" :
{
"messages" :
{
"type" : "array",
"items" :
{
"properties" :
{
"name" :
{
"type" : "string"
},
"id" :
{
"type" : "string"
},
"signals" :
{
"type" : "object"
},
"properties" :
{
"start_bit" :
{
"type" : "object",
"properties" :
{
"name" :
{
"type" : "string"
},
"bit_length" :
{
"type" : "string"
},
"factor" :
{
"type" : "string"
},
"offset" :
{
"type" : "string"
}
},
"required" : ["name", "bit_length", "factor", "offset"]
}
}
},
"required": ["name", "id", "signals"]
}
}
}
}
To "avoid" having to keep the string consistent, in the validator file, I can put any string (obviously it makes more sense to name the string what is represents, in my case "start_bit") and then by NOT having it be required.
Related
[PS : I have gone through all the possible issues under same error and tried to fix my Json Schema]
I have the following Avro Schema (Converted to JSON using http://json-schema-validator.herokuapp.com/avro.jsp) :
{
"definitions" : {
"record:parentTest.test.MyPlatformData" : {
"description" : "representsthetestevent",
"type" : "object",
"required" : [ "event", "subjects" ],
"additionalProperties" : false,
"properties" : {
"event" : {
"$ref" : "#/definitions/record:parentTest.test.event_record"
},
"subjects" : {
"type" : "array",
"items" : {
"$ref" : "#/definitions/record:parentTest.test.TESTEVENT_record"
}
}
}
},
"record:parentTest.test.event_record" : {
"type" : "object",
"required" : [ "created_at", "id", "event_at", "subject", "action", "up_event_id", "up_source" ],
"additionalProperties" : false,
"properties" : {
"created_at" : {
"type" : "string"
},
"id" : {
"type" : "integer",
"minimum" : -2147483648,
"maximum" : 2147483647
},
"event_at" : {
"type" : "string"
},
"subject" : {
"type" : "string"
},
"action" : {
"type" : "string"
},
"up_event_id" : {
"default" : "null",
"oneOf" : [ {
"type" : "string"
}, {
"type" : "null"
} ]
},
"up_source" : {
"default" : "null",
"oneOf" : [ {
"type" : "string"
}, {
"type" : "null"
} ]
}
}
},
"record:parentTest.test.TESTEVENT_record" : {
"type" : "object",
"required" : [ "mydata", "id", "user_id", "data_id", "place_id", "my_object" ],
"additionalProperties" : false,
"properties" : {
"mydata" : {
"$ref" : "#/definitions/record:parentTest.test.mydata_record"
},
"id" : {
"type" : "integer",
"minimum" : -9223372036854775808,
"maximum" : 9223372036854775807
},
"user_id" : {
"type" : "integer",
"minimum" : -9223372036854775808,
"maximum" : 9223372036854775807
},
"data_id" : {
"type" : "integer",
"minimum" : -9223372036854775808,
"maximum" : 9223372036854775807
},
"place_id" : {
"type" : "integer",
"minimum" : -2147483648,
"maximum" : 2147483647
},
"my_object" : {
"$ref" : "#/definitions/record:parentTest.test.my_object_record"
}
}
},
"record:parentTest.test.mydata_record" : {
"type" : "object",
"required" : [ "id", "del" ],
"additionalProperties" : false,
"properties" : {
"id" : {
"type" : "integer",
"minimum" : -9223372036854775808,
"maximum" : 9223372036854775807
},
"del" : {
"$ref" : "#/definitions/record:parentTest.test.del_record"
}
}
},
"record:parentTest.test.del_record" : {
"type" : "object",
"required" : [ "status", "track_details", "cr", "service_level" ],
"additionalProperties" : false,
"properties" : {
"status" : {
"$ref" : "#/definitions/record:parentTest.test.status_record"
},
"tracking_details" : {
"type" : "array",
"items" : {
"$ref" : "#/definitions/record:parentTest.test.tracking_number_detail_record"
}
},
"cr" : {
"$ref" : "#/definitions/record:parentTest.test.cr_record"
},
"service_level" : {
"$ref" : "#/definitions/record:parentTest.test.service_level_record"
}
}
},
"record:parentTest.test.status_record" : {
"type" : "object",
"required" : [ "name", "status_at" ],
"additionalProperties" : false,
"properties" : {
"name" : {
"type" : "string"
},
"status_at" : {
"type" : "string"
}
}
},
"record:parentTest.test.tracking_number_detail_record" : {
"type" : "object",
"required" : [ "id", "cr", "status" ],
"additionalProperties" : false,
"properties" : {
"id" : {
"type" : "string"
},
"cr" : {
"$ref" : "#/definitions/record:parentTest.test.tracking_cr_record"
},
"status" : {
"$ref" : "#/definitions/record:parentTest.test.trac_status_record"
}
}
},
"record:parentTest.test.tracking_cr_record" : {
"type" : "object",
"required" : [ "id" ],
"additionalProperties" : false,
"properties" : {
"id" : {
"type" : "integer",
"minimum" : -2147483648,
"maximum" : 2147483647
}
}
},
"record:parentTest.test.tracking_status_record" : {
"type" : "object",
"required" : [ "name", "status_at" ],
"additionalProperties" : false,
"properties" : {
"name" : {
"oneOf" : [ {
"type" : "string"
}, {
"type" : "null"
} ]
},
"status_at" : {
"oneOf" : [ {
"type" : "string"
}, {
"type" : "null"
} ]
}
}
},
"record:parentTest.test.cr_record" : {
"type" : "object",
"required" : [ "id" ],
"additionalProperties" : false,
"properties" : {
"id" : {
"type" : "integer",
"minimum" : -2147483648,
"maximum" : 2147483647
}
}
},
"record:parentTest.test.service_level_record" : {
"type" : "object",
"required" : [ "sc_id", "ss_id", "ss_class" ],
"additionalProperties" : false,
"properties" : {
"sc_id" : {
"type" : "integer",
"minimum" : -2147483648,
"maximum" : 2147483647
},
"ss_id" : {
"type" : "integer",
"minimum" : -2147483648,
"maximum" : 2147483647
},
"ss_class" : {
"type" : "string"
}
}
},
"record:parentTest.test.mydata_record" : {
"type" : "object",
"required" : [ "s_id" ],
"additionalProperties" : false,
"properties" : {
"s_id" : {
"oneOf" : [ {
"type" : "integer",
"minimum" : -2147483648,
"maximum" : 2147483647
}, {
"type" : "null"
} ]
}
}
}
},
"$ref" : "#/definitions/record:parentTest.test.MyPlatformData"
}
However on inserting the following data, I get following error
Input Data
{
"event": {
"action": "TESTEVENT",
"created_at": "2021-01-21T14:16:23+00:00",
"event_at": "2021-01-21T14:16:23.747",
"id": 28001755,
"subject": "TEST_ITEM",
"up_source": {
"string": "TEST_W"
},
"up_event_id": {
"string": "59c7eec8-9bf6-4907-9df9-628d92abd278"
}
},
"subjects": [
{
"id": 12345678,
"user_id": 5197509841,
"data_id": 3497218791,
"place_id": 49,
"mydata": {
"s_id": {
"int": 29489
}
},
"my_object": {
"id": 5417499011,
"del": {
"status": {
"name": "TESTEVENT",
"status_at": "2021-01-21T09:09:00"
},
"tracking_details": [
{
"id": "CS303755237",
"cr": {
"id": 961
},
"status": {
"name": {
"string": "TESTEVENT"
},
"status_at": {
"string": "2021-01-21T09:09:00"
}
}
}
],
"cr": {
"id": 961
},
"service_level": {
"sc_id": 2,
"ss_id": 5,
"ss_class": "MY_TEST_SS"
}
}
}
}
]
}
Error
Caused by: org.apache.avro.AvroTypeException: Expected start-union. Got VALUE_NUMBER_INT
I was able to finally find the issue.
Being a beginner, I wasn't able to comprehend that time.
For anyone facing similar issue :
Rule : Whenever a field is marked "datatype" and "null" both, it is
important to mention the datatype, if the value is not null.
For example :
For Avro schema, field definition :
"up_source" : {
"default" : "null",
"oneOf" : [ {
"type" : "string"
}, {
"type" : "null"
} ]
}
Possible declarations :
"up_source": {
"string": "TEST_123"
},
OR
"up_source": null,
One of my fields was missing this.
I generated the following JSON:
{
"someString" : "example",
"obj1" : {
"opt1" : 1,
"opt2" : 1,
"opt3" : "aaa"
},
"obj2" : {
"opt1" : 55,
"opt2" : 55,
"opt3" : "bbb"
}
}
and there will be more of objects(obj1, obj2, obj3, obj4, ...) with same data type (opt1, opt2, opt3)
now I want to create schema for this but i don't know how to combine all this objects in schema.
EDIT:
I created schema:
root: {
"type" : "object",
"oneOf" : [
{
"properties" : {
"someString" : { "type" : "string" }
},
"patternProperties" : { "^.*$" : { "$ref" : "./schemas/myPatternProperties.json#" } },
"additionalProperties" : false }
}
]
}
and myPatternProperties.json looks:
{
"type" : "object",
"properties" : {
"opt1" : { "type" : "number" },
"opt2" : { "type" : "number" },
"opt3" : { "type" : "string" },
}
"required" : [ "opt1", "opt2", "opt3" ]
}
Is there anything wrong because, my generated JSON is still not recognized as this schema type.
As I understand your problem is to describe object with a lot of properties with the same type and some naming rules. To solve that you must specify patternProperties section
{
"patternProperties": {
"^(/[^/]+)+$": { "$ref": "http://some.site.somewhere/entry-schema#" }
}
that construction specify pattern to match for properties. Example how to use patternProperties Read more at specification
UPDATE
Actually full scheme must be something like that
{
"$schema": "http://json-schema.org/draft-06/schema#",
"type": "object",
"properties": {
"someString": {
"type": "string"
}
},
"patternProperties": {
"^obj([0-9]+)$": {
"$ref": "#/definitions/objEntity"
}
},
"additionalProperties": false,
"required": [ "someString" ],
"definitions": {
"objEntity": {
"type": "object",
"properties": {
"opt1": { "type": "number" },
"opt2": { "type": "number" },
"opt3": { "type": "string" }
},
"required": ["opt1", "opt2", "opt3"]
}
}
}
Of course you can split that scheme to more than 1 file, and change links to type definitions.
In react app I need to implement ui schema.
The scheme storages in database and react app take it from there and then put it
to Form from react-jsonschema-form package, but textfield doesn't change, it's still "input" but not "textarea".
I use
"_uiSchema" : {
"content" : {
"items" : {
"text" : {
"ui:widget" : "textarea"
}
}
}
},
but text didnt chage type. And text is
"text" : {
"type" : "string"
},
And full Schema
{
"title" : "Career",
"type" : "object",
"properties" : {
"_id" : {
"type" : "string",
"title" : "_id"
},
"urlPage" : {
"type" : "string",
"title" : "url"
},
"subUrls" : {
"type" : "array",
"title" : "Sub urls",
"items" : {
"type" : "string"
}
},
"content" : {
"type" : "array",
"title" : "page",
"items" : [
{
"title" : "content",
"type" : "object",
"properties" : {
"categoryTitle" : {
"type" : "string",
"title" : "Category title"
},
"popUp" : {
"type" : "object",
"properties" : {
"successSubTitle" : {
"type" : "string"
},
"successTitle" : {
"type" : "string"
},
"subTitlePopUp" : {
"type" : "string"
},
"titlePopUp" : {
"type" : "string"
}
}
},
"categorySubTitle" : {
"type" : "string",
"title" : "Category sub title"
},
"tabs" : {
"type" : "array",
"title" : "Tabs",
"items" : {
"type" : "object",
"properties" : {
"path" : {
"type" : "string"
},
"title" : {
"type" : "string"
}
}
}
},
"notFindMessage" : {
"type" : "string"
},
"data" : {
"title" : "Open positions",
"type" : "array",
"items" : {
"type" : "object",
"properties" : {
"category" : {
"type" : "string"
},
"summary" : {
"type" : "string"
},
"details" : {
"type" : "array",
"items" : {
"type" : "object",
"properties" : {
"sectionTitle" : {
"type" : "string"
},
//here is my text field
"text" : {
"type" : "string"
},
"listItems" : {
"type" : "array",
"items" : {
"type" : "string"
}
}
}
}
}
}
}
}
}
}
]
}
}
}
I read guide on te github (https://github.com/mozilla-services/react-jsonschema-form#the-uischema-object)
UPDATE
ui schema that works
"content" : {
"items" : {
"data" : {
"items" : {
"details" : {
"items" : {
"myText" : {
"ui:widget" : "textarea"
}
}
}
}
}
}
}
I have checked the source code of this library and seems like you have to have description for items in uiSchema.
"_uiSchema" : {
"content" : {
"items" : {
"description" : {
"ui:widget" : "textarea"
}
}
}
}
The following schema and document validate just fine even though the last line of the document uses "none" as a value which is not in the enum.
{
"$schema" : "http://json-schema.org/draft-04/schema#",
"title" : "Test schema",
"definitions" : {
"xxx_type" : {
"enum" : [ "X1", "X2", "X3" ]
},
"xxx_info" : {
"type" : "object",
"properties" : {
"date" : { "type" : "string" },
"category" : {
"type" : "array",
"items" : { "$ref" : "#/definitions/xxx_type" }
}
},
"required" : [ "date", "category" ]
}
},
"XXX" : {
"type" : "array",
"items" : {
"$ref" : "#/definitions/xxx_info"
}
}
}
{
"XXX" : [ { "date" : "2015/01/01", "category" : [ "X1" ] },
{ "date" : "2015/02/01", "category" : [ "X2" ] },
{ "date" : "2015/03/01", "category" : [ "X3" ] }
{ "date" : "2015/04/01", "category" : [ "none" ] }
]
}
The following says invalid. (The two lines before the "XXX" definition are the only real difference.) Here the "none" is invalid.
{
"$schema" : "http://json-schema.org/draft-04/schema#",
"title" : "Test schema",
"definitions" : {
"xxx_type" : {
"enum" : [ "X1", "X2", "X3" ]
},
"xxx_info" : {
"type" : "object",
"properties" : {
"date" : { "type" : "string" },
"category" : {
"type" : "array",
"items" : { "$ref" : "#/definitions/xxx_type" }
}
},
"required" : [ "date", "category" ]
}
},
"type" : "object",
"properties" : {
"XXX" : {
"type" : "array",
"items" : {
"$ref" : "#/definitions/xxx_info"
}
}
}
}
{
"XXX" : [ { "date" : "2015/01/01", "category" : [ "X1" ] },
{ "date" : "2015/02/01", "category" : [ "X2" ] },
{ "date" : "2015/03/01", "category" : [ "X3" ] }
{ "date" : "2015/04/01", "category" : [ "none" ] }
]
}
The first one says it is valid because it does not validate the xxx property.
But on the second one you have changed the jsonSchema to meet the JSON document and this time it is validating the xxx property using the jsonSchema and the property is invalid because it contains an invalid node the one with the category "none"
I am trying to figure out how oneOf works by building a schema which validates two different object types. For example a person (firstname, lastname, sport) and vehicles (type, cost).
Here are some sample objects:
{"firstName":"John", "lastName":"Doe", "sport": "football"}
{"vehicle":"car", "price":20000}
The question is what have I done wrongly and how can I fix it. Here is the schema:
{
"description": "schema validating people and vehicles",
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"required": [ "oneOf" ],
"properties": { "oneOf": [
{
"firstName": {"type": "string"},
"lastName": {"type": "string"},
"sport": {"type": "string"}
},
{
"vehicle": {"type": "string"},
"price":{"type": "integer"}
}
]
}
}
When I try to validate it in this parser:
https://json-schema-validator.herokuapp.com/
I get the following error:
[ {
"level" : "fatal",
"message" : "invalid JSON Schema, cannot continue\nSyntax errors:\n[ {\n \"level\" : \"error\",\n \"schema\" : {\n \"loadingURI\" : \"#\",\n \"pointer\" : \"/properties/oneOf\"\n },\n \"domain\" : \"syntax\",\n \"message\" : \"JSON value is of type array, not a JSON Schema (expected an object)\",\n \"found\" : \"array\"\n} ]",
"info" : "other messages follow (if any)"
}, {
"level" : "error",
"schema" : {
"loadingURI" : "#",
"pointer" : "/properties/oneOf"
},
"domain" : "syntax",
"message" : "JSON value is of type array, not a JSON Schema (expected an object)",
"found" : "array"
} ]
Try this:
{
"description" : "schema validating people and vehicles",
"type" : "object",
"oneOf" : [
{
"type" : "object",
"properties" : {
"firstName" : {
"type" : "string"
},
"lastName" : {
"type" : "string"
},
"sport" : {
"type" : "string"
}
}
},
{
"type" : "object",
"properties" : {
"vehicle" : {
"type" : "string"
},
"price" : {
"type" : "integer"
}
},
"additionalProperties":false
}
]
}
oneOf need to be used inside a schema to work.
Inside properties, it's like another property called "oneOf" without the effect you want.