JSON Schema: First Item of Array - arrays

I need a JSON array of arbitrary length. Each item in the array is a JSON object, they all have same keys and types.
But the schema should make one exception: the first object doesn't need all keys, so schemas required list should be shorter for the first item.
I tried schemas with "items" and "prefixItems" without luck. It seems that "prefixItems" will be ignored independently of draft version when used with "items". Because array can be of arbitrary length, I guess I cannot use multiple schemas with "items".
{
"description": "Schema for array data",
"$schema": "http://json-schema.org/draft-2020-12/schema#",
"version": "0-0-6",
"type": "object",
"required": [
"data"
],
"properties": {
"data": {
"type": "array",
"prefixItems": [{
"type": "object",
"required": [
"name"
],
"properties": {
"name" : { "type" :"string" },
"age" : { "type" : "number" },
"city" : { "type" : "string" }
}
}],
"items":
{
"type": "object",
"required": [
"name", "age", "city"
],
"properties": {
"name" : { "type" :"string" },
"age" : { "type" : "number" },
"city" : { "type" : "string" }
}
}
}
}
}
My data:
{
"data" : [
{
"name": "Tom"
},
{
"name": "Ben",
"age": 32,
"city": "Berlin"
},
{
"name": "Mike",
"age": 40,
"city": "Boston"
}
]
}
For validation I use:
https://www.jsonschemavalidator.net/
https://jsonlint.com/
Validation of my example gives error for first item:
"Required properties are missing from object: age, city."

Use additionalItems instead of items.
Schema:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "array",
"prefixItems": [
{ "type": "number" },
{ "type": "string" },
{ "enum": ["Street", "Avenue", "Boulevard"] },
{ "enum": ["NW", "NE", "SW", "SE"] }
],
"additionalItems": {
"type": "number"
}
}
Data that validates:
[
1600,
"Pennsylvania",
"Avenue",
"NW",
1]
The issue you're running into is that the validators only support up to draft 7 of the standard, and using items the way we both want to was new in the 2019 draft. It's not supported yet.

Related

JSON Schema: Check the array to validate if a certain block of JSON objects is contained in it

I have a JSON array of arbitrary length. Each item in the array is a nested block of JSON objects, they all have same properties but different values.
I need a JSON schema to check the array if the last block in the array has the values defined in the schema.
How should the scheme be defined so that it only considers the last block in the array and ignores all the blocks before in the array?
My current solution successfully validates the JSON objects if there is only one block in the array. As soon as I have more blocks, it fails because all the others are not valid against my schema - for sure, this corresponds to the expected behaviour.
In my example, the JSON array contains two nested blocks of JSON objects. These differ for the following items:
event.action = "[load|button]"
event.label = "[journey:device-only|submit,journey:device-only]"
type = "[page|track]"
An example for my data are:
[
{
"page": {
"path": "order/checkout/summary",
"language": "en"
},
"cart": {
"ordercase": "neworder",
"product_list": [
{
"name": "Apple iPhone 14 Plus",
"quantity": 1,
"price": 1000
}
]
},
"event": {
"action": "load",
"label": "journey:device-only"
},
"type": "page"
},
{
"page": {
"path": "order/checkout/summary",
"language": "en"
},
"cart": {
"ordercase": "neworder",
"product_list": [
{
"name": "Apple iPhone 14 Plus",
"quantity": 1,
"price": 1000
}
]
},
"event": {
"action": "button",
"label": "submit,journey:device-only",
},
"type": "track"
}
]
And the schema I use which works fine for the second block if the block would be the only one in the array:
{
"type": "array",
"$schema": "http://json-schema.org/draft-07/schema#",
"items": {
"type": "object",
"required": ["event", "page", "type"],
"properties": {
"page": {
"type": "object",
"properties": {
"path": {
"const": "order/checkout/summary"
},
"language": {
"enum": ["de", "fr", "it", "en"]
}
},
"required": ["path", "language"]
},
"event": {
"type": "object",
"additionalProperties": false,
"properties": {
"action": {
"const": "button"
},
"label": {
"type": "string",
"pattern": "^[-_:, a-z0-9]*$",
"allOf": [
{
"type": "string",
"pattern": "^\\S*(?:(submit,|,submit))\\S*$"
},
{
"type": "string",
"pattern": "^\\S*(journey:(?:(device-only|device-plus)))\\S*$"
}
]
}
},
"required": ["action", "label"]
},
"type": {
"enum": ["track", "string"]
}
}
}
}

How to create array json schema for an array string which contains some fixed values and may have other additonal values

So I have this json schema:-
{
"type": "object",
"properties": {
"campaignType": {
"type": "string",
"enum": [
"export"
]
},
"clientid": {
"type": "integer",
"minimum": 1
},
"select": {
"type": "object",
"minProperties": 1,
"anyOf": [
{
"required": [
"list"
]
},
{
"required": [
"segment"
]
}
],
"properties": {
"list": {
"type": "array",
"items": {
"type": "integer"
}
},
"segment": {
"type": "array",
"items": {
"type": "integer"
}
}
}
},
"attributes": {
"type": "array",
"minItems": 2,
"items": {
"type": "string",
"contains": ["fk", "uid"]
}
}
},
"required": [
"campaignType",
"clientid",
"select",
"attributes"
]
}
Here I want to have attributes field to have value "fk", "uid" fixed and must allow other field values with "fk" and "uid".
with this following code I am getting error while passing additonal values:-
{
"campaignType":"export",
"clientid":107311,
"select":{
"segment":[30]
},
"attributes":["uid","fk", "att1"]
}
error unmarshaling properties from json: error unmarshaling items from json: json: cannot unmarshal object into Go value of type []*jsonschema.Schema
how do I fix it?
The value of contains in your schema must be a schema:
According to your question, maybe change the "attributes" schema to:
"attributes": {
"type": "array",
"minItems": 2,
"items": [ { "const": "fk" }, { "const": "uid" } ],
"additionalItems": {
"type": "string"
}
}

JSON-schema object array validation

I have a mission to validate such a JSON message :
{
"header": {
"action": "change_time",
"taskGuid": "someTaskGuid",
"publishDate": "2012-04-23T18:25:43.511Z"
},
"data": {
"code": "f2103839",
"conditions": [
{
"conditionsType": "A",
"dateBegin": "2021-11-22T17:30:43.511Z",
"dateEnd": "2021-11-22T17:35:43.511Z"
},
{
"conditionsType": "B",
"dateBegin": "2021-11-22T17:30:43.511Z",
"dateEnd": "2021-11-22T17:35:43.511Z"
},
{
"conditionsType": "C",
"dateBegin": "2021-11-22T17:30:43.511Z",
"dateEnd": "2021-11-22T17:35:43.511Z"
}
]
}
}
I've made such a JSON-schema to achieve that :
{
"$schema": "http://json-schema.org/draft-07/schema",
"title": "Some schema",
"description": "Some schema",
"type": "object",
"required": [
"header",
"data"
],
"properties": {
"header": {
"type": "object",
"required": [
"action",
"taskGuid",
"publishDate"
],
"properties": {
"action": {
"enum": [
"create_work_order",
"change_time",
"cancel_work"
]
},
"taskGuid": {
"type": "string"
},
"publishDate": {
"type": "string",
"format": "date-time"
}
}
},
"data": {
"type": "object",
"required": [
"code",
"conditions"
],
"properties": {
"code": {
"type": "string"
},
"conditions": {
"type": "array",
"items": [
{
"conditionsType": "object",
"properties": {
"type": {
"enum": [
"A",
"B",
"C"
]
},
"dateBegin": {
"type": "string",
"format": "date-time"
},
"dateEnd": {
"type": "string",
"format": "date-time"
}
},
"required": [
"conditionsType",
"dateBegin",
"dateEnd"
]
}
]
}
}
}
}
}
The conditions array will consist of 1-3 objects described by items. Each object should have a unique conditionsType.
I'm checking validation with this instrument - https://www.jsonschemavalidator.net/
The problem is that this schema does validate the message, But only the first object of array is processed as de. For instance, such a JSON is validated as well (see "conditions" object #2):
{
"header": {
"action": "change_time",
"taskGuid": "someTaskGuid",
"publishDate": "2012-04-23T18:25:43.511Z"
},
"data": {
"code": "f2103839",
"conditions": [
{
"conditionsType": "A",
"dateBegin": "2021-11-22T17:30:43.511Z",
"dateEnd": "2021-11-22T17:35:43.511Z"
},
{
"conditionsType": 123,
"dateBegin": [1,2,3],
"dateEnd": 1
},
{
"conditionsType": "C",
"dateBegin": "2021-11-22T17:30:43.511Z",
"dateEnd": "2021-11-22T17:35:43.511Z"
}
]
}
}
Is that actually the right direction I've chosen for this task ?
Two things. You have a typo in your items schema where you actually want to have type and not conditionsType. Secondly, if the items keyword is an array, the items of the array are validated against the schemas in this order. You want to have the items keyword as a single schema which is then applied to all items. Your corrected schema for copy-paste:
{"$schema":"http://json-schema.org/draft-07/schema","title":"Some schema","description":"Some schema","type":"object","required":["header","data"],"properties":{"header":{"type":"object","required":["action","taskGuid","publishDate"],"properties":{"action":{"enum":["create_work_order","change_time","cancel_work"]},"taskGuid":{"type":"string"},"publishDate":{"type":"string","format":"date-time"}}},"data":{"type":"object","required":["code","conditions"],"properties":{"code":{"type":"string"},"conditions":{"type":"array","items":{"type":"object","properties":{"conditionsType":{"enum":["A","B","C"]},"dateBegin":{"type":"string","format":"date-time"},"dateEnd":{"type":"string","format":"date-time"}},"required":["conditionsType","dateBegin","dateEnd"]}}}}}}

Filter for array type of field in loopback

I am using loopback v3 with mongodb database and implementing filter for array type of field.
inq operator is not working.
I have an array of object like below
[
{
"name":"name1",
"title": "title1",
"category": ["a", "b","c"]
},
{
"name":"name2",
"title": "title2",
"category": ["x", "y","z"]
},
{
"name":"name3",
"title": "title3",
"category": ["b", "d","e"]
}
]
now i want a list where category containing "b"
So i am using below filter method
filter: {where:{category:{inq:["b"]}}}
I think inq does n't work for this case.it gives empty response.
Output : [ ]
how can i get my desired output.
Desired output:
[
{
"name":"name1",
"title": "title1",
"category": ["a", "b","c"]
},
{
"name":"name3",
"title": "title3",
"category": ["b", "d","e"]
}
]
below is my properties
"properties": {
"name": {
"type": "string"
},
"title": {
"type": "string"
},
"category": {
"type": [
"string"
]
}
},
Please suggest.
Thanks
for me the above scenario works fine. Although in your code the array closing brackets should be ] instead of }, just pointing out something I found in your code.
How did you setup your model for this?
"properties": {
"name": {
"type": "string"
},
"title": {
"type": "string"
},
"category": {
"type": [
"string"
]
}
},
Does your model properties look like this ?
"role": {
"type": "array",
"default": [
"patient"
]
}
let filter = {role:{in:['doctor']}}
this.find({
where: filter
}, cb);

Pushing Values from JSON Object into Array

I have the following object in my AngularJS Controller:
{"team":"1","name":"abc","age":"20"},
{"team":"1","name2":"def","age2":"21"},
{"team":"2","name":"ghi","age":"22"},
{"team":"2","name2":"jkl","age2":"23"},
I need to group the items into one array object, by the team key.
{
"team": [
{"name1":"abc","age1":"20", "name2":"def", "age2":"21"},
{"name1":"ghi","age1":"22", "name2":"jkl", "age2":"23"}
]
}
So I can later use the information like $scope.data.team[1].name2
EDIT: One Team always consists of 4 players by the way.
How would I do this?
edit: working plunkr for your needs: http://plnkr.co/edit/zxoOYV?p=preview
you should rearrange your structure. i.e. you could go for something like this:
{"team": [
{"players": [
{"name" : "abc", "age": 20},
{"name" : "def", "age": 34},
]},
{"players": [
{"name" : "abc", "age": 20},
{"name" : "def", "age": 34},
]}
]}
if you use this structure in your controller:
$scope.team = {...}
and use it in your html like:
<div ng-controller="TeamController">
<div ng-repeat="players in team">
<div ng-repeat="player in players">
<div>Name: {{player.name}}</div>
<div>Name: {{player.age}}</div>
</div>
</div>
</div>
so, for your example, i got the angular-schema-form working.
with the above structure the schema looks like this:
[
{
"type": "help",
"helpvalue": "<h4>Tabbed Array Example</h4><p>Tab arrays can have tabs to the left, top or right.</p>"
},
{
"key": "team",
"type": "tabarray",
"add": "New",
"remove": "Delete",
"style": {
"remove": "btn-danger"
},
"title": "value.name || 'Team '+$index",
"items": [
{
"key": "team[].players",
"title": "Players",
"items": [
{
"key": "team[].players[].name",
"title": "Name"
},
{
"key": "team[].players[].age",
"title": "Age"
}
]
}
]
},
{
"type": "submit",
"style": "btn-default",
"title": "OK"
}
]
and the corresponding schema:
{
"type": "object",
"title": "Team",
"properties": {
"team": {
"type": "array",
"items": {
"type": "object",
"properties": {
"players": {
"type": "array",
"maxItems": 4,
"items": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "integer"
}
},
"required": [
"name",
"age"
]
}
}
},
"required": [
"players"
]
}
}
}
}

Resources