the main problem resides on validate a json against a schema that deals with arrays. So, if I put a different value seems to be still valid?
json schema:
{
"transactions" : {
"type" : "array",
"items" : {
"type" : "object",
"properties" : {
"type" : {
"type" : "string",
"enum" : ["BREAK"]
},
"required":["type"]
},
"required":["items"]
}
}
}
Input JSON:
{
"transactions":[
{
"type":"BREAKDDDDDdddddddddddddddddddddddddddJDJDJDJDJDJDJDJ"
}
]
}
result: No errors found. JSON validates against the schema.
This is wrong as we haven't defined an enum type like "BREAKDDDDD"
http://www.jsonschemavalidator.net/
Any thoughts on this?
Your JSON Schema is missing certain attributes. Look at the example provided here on how to start the schema http://json-schema.org/example1.html.
Update your schema to the below and try
{
"type": "object",
"properties": {
"transactions": {
"type": "array",
"items": {
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": ["BREAK"]
}
},
"required": ["type"]
}
}
}
}
To validate an enum value in a JSON schema, we can use the enum keyword. The enum keyword allows you to specify a list of allowed values for a property. For example, if you have an "enum": ["one", "two"] in your JSON schema, this will only allow the values "one", "two".
{
"type": "object",
"properties": {
"prop": {
"type": "string",
"enum": ["one", "two"]
}
}
}
Related
I am trying to create JSON schema for below JSON,
{
"messages":{
"bookCreated":{
"default":{
"channels":{
"sqs":{
"enabled":true,
"topic":"sample-topic",
"shares":true
}
}
}
},
"bookCreationInProgress":{
"default":{
"channels":{
"sns":{
"enabled":true,
"topic":"sample-sns",
"shares":true
}
}
}
},
"bookCreationCompleted":{
"default":{
"channels":{
"s3":{
"enabled":true,
"topic":"sample-s3-bucket",
"shares":true
}
}
}
}
}
}
Inside message bookCreated, bookCreationInProgress , bookCreationCompleted similarly we have several dynamic properties. Inside each of these objects default and channel details are mandatory.
And each channel has a set of mandatory attributes.
I browsed internet to create JSON schema for the above json but I couldn't get any reference of how to create json schema for nested map objects.
Since I couldn't able to construct the json schema for very first dynamic object I couldn't able to construct the schema further.
{
"$schema": "app_messages",
"type": "object",
"additionalProperties": true,
"anyOf": [
{
"required": ["messages"]
}
],
"properties": {
"id": {
"type": "string"
}
}
}
It would be really great if somebody would help me to share the pointers of how to handle map of dynamic properties in JSON schema. Any help would be really appreciable.
A good solution use "additionalProperties" as json-object (instead of boolean) combined to "$ref" and "$defs".
A talking example might be:
{
"$schema": "app_messages",
"type": "object",
"additionalProperties": {
"anyOf": [
{ "$ref": "#/$defs/subobj1" },
{ "$ref": "#/$defs/subobj2" }
]
},
"properties": {
"id": { "type": "string" }
},
"$defs": {
"subobj1": {
"type": "object",
"properties": {
"message": { "type": "string" }
...
},
"required": [ "message" ]
},
"subobj2": {
"type": "object",
"properties": {
"message": { "type": "string" }
...
},
"required": [ "message" ]
}
}
}
In this way main object can have "id" properties and all additional properties must match one of sub definition "subobj1" or "subobj2".
Given that I have a JSON file something like this:
{
"organisation":"Acme Co. Ltd",
"organisation_abbreviation":"acme",
"document_types":["invoice","credit-note"],
"invoice":{
"date":"2017-05-31",
"value":238.44,
"description":"invoice for xxx"
},
"credit_note":{
"date":"2017-05-22",
"value":0.0,
"description":"DNOTE for xxx"
},
}
The salient being that in document types, I define the various permitted document types, and then later down I have a section for each of the document types named here.
How can I write a schema validation that will check that each document type section is a one of the types mentioned above (the example would fail because 'credit-note' != 'credit_note')
Would that fill what you need by using the OneOf:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Acme billing",
"type" : "object",
"definitions": {
"Entry": {
"type": "object",
"properties": {
"date": "string",
"value": "number",
"description": "string"
}
}
},
"properties": {
"organisation": "string",
"organisation_abbreviation": "string",
"document_types": {
"enum": ["invoice","credit-note"]
}
"oneOf" : [{
"properties": {
"invoice": {
"$ref" : "#/definitions/Entry"
},
"credit-note":{
"$ref" : "#/definitions/Entry"
}
]
},
"additionalProperties":false
}
}
}
I have the following JSON data that I would like to validate.
[
{ "fieldType": "oneThing" },
{ "fieldType": "anotherThing" },
{ "fieldType": "oneThing" }
]
And my current (non working) schema is:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "array",
"items": {
"oneOf": [
{ "$ref": "#/definitions/oneThing" },
{ "$ref": "#/definitions/anotherThing" }
]
},
"definitions": {
"oneThing": {
"type": "object",
"properties": {
"fieldType": {
"type": "string",
"pattern": "oneThing"
}
},
"required": [
"fieldType"
]
},
"anotherThing": {
"type": "object",
"properties": {
"fieldType": {
"type": "string",
"pattern": "anotherThing"
}
},
"required": [
"fieldType"
]
}
}
}
I'm getting the following error but I fail to see what I'm doing wrong.
[] Object value found, but an array is required
More context: I'm generating a dynamic HTML form based on a JSON configuration. The HTML form will have a specific set of valid field types and the same field type may exist multiple times in the config, thus oneThing appearing more than once in the above sample json.
As it turns out, this had nothing to do with my JSON schema but with how I was calling the library that was parsing the schema.
I'm using https://github.com/justinrainbow/json-schema and was passing the wrong data type to the class. Duh!
I am trying to document an existing use of JSON using json-schema. The system permits the following two possabilities for one of the object attributes.
Either
{
"tracking_number" : 123
}
Or
{
"tracking_number" : [ 123, 124, 125 ]
}
How can I express this using json schema?
Use anyOf to assert that the property must conform to one or another schema.
{
"type": "object",
"properties": {
"tracking_number": {
"anyOf": [
{ "$ref": "#/definitions/tracking_number" },
{ "type": "array", "items": { "$ref": "#/definitions/tracking_number" }
]
},
"definitions": {
"tracking_number": { "type": "integer" }
}
}
I'd like to use oneOf schemas which differ only by value of xyType property. I'd like to have two of them: one where the xyType is set to "1" and the second one where xyType is any other value. Can this be done using json schemas?
"oneOf": [
{
"properties": {
"xyType": "enum": ["1"],
"whatever" : "string"
},
"type": "object"
},
{
"properties": {
"xyType": "enum": [], /// NOT "1"?
"whatever" : "string"
},
"type": "object"
}
]
There's a not operator, and the enum keyword, and you can use them together, like
{
"not": {
"enum": ["1"]
}
}