I'm trying to generate a form which will have multiple vehicles and each vehicle should have multiple people inside it.
I tried to do it by using an array inside another array. But for some obscure reasons it's not working.
This is what I want:
http://i.imgur.com/ZB2kCa1.png
This is what I have (so far):
Form:
[
{
"key": "vehicles",
"items": [
"['vehicles'][]['plate-number']",
"['vehicles'][]['color']",
{
"key": "people",
"items": [
"['vehicles'][]['people'][]['name']"
]
}
]
}
]
Schema:
{
"type": "object",
"properties": {
"vehicles": {
"type": "array",
"items": {
"type": "object",
"properties": {
"plate-number": {
"title": "Plate number",
"type": "string"
},
"color": {
"title": "Color",
"type": "string"
},
"people": {
"type": "array",
"items": {
"type": "object",
"properties": {
"title": {
"type": "string",
"enum": ["dr","jr","sir","mrs","mr","NaN","dj"]
},
"name": {
"title": "Name",
"type": "string"
}
}
}
}
}
}
}
}
}
Edit:
stefankmitph's answer solvers my problem. Thank you!
But something weird is happening: a new object person is added at the same level of vehicles. Also, when I fill a person's information and then delete this person the models is not updated.
The schema you provide does not add a property 'gender' (as shown in your picture link). So I took 'title' instead of 'gender':
[
{
"key": "vehicles",
"items": [
"vehicles[].plate-number",
"vehicles[].color", {
"key": "people",
"type": "array",
"title": "People",
"items": [
"vehicles[].people[].name",
"vehicles[].people[].title"
]
}
]
}
]
I hope this is what you're looking for!
Note: Tested with Schema Form Example Page
Related
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"
}
}
I read the specification. But, I didn't find any information about how to use the readOnly of Array. And a single readonly property is not enough to cover different scenarios.
An example array schema like below:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.com/product.schema.json",
"title": "Products",
"type": "array",
"items": {
"type": "object",
"properties": {
"productId": {
"description": "The unique identifier for a product",
"type": "integer"
},
"productName": {
"description": "Name of the product",
"type": "string"
},
"price": {
"description": "The price of the product",
"type": "number"
}
}
}
}
Is it possible to achieve the following objectives?
The array is not allowed to add/remove any item of the array. However, it's allowed to update any item of the array.
The array is not allowed to update any item of the array. However, it's allowed to add/remove item of the array. Once an item is added to the array, the only action I can take is "remove".
readOnly is too blunt an instrument to describe those kinds of constraints. However, with a small change, you can express it with JSON Hyper-Schema. You would have to model the individual products and the list of products separately. Unlike readOnly which says what you can't do, links say what you can do. Here's what it would look like for each of the cases you identified.
The array is not allowed to add/remove any item of the array. However, it's allowed to update any item of the array.
{
"$schema": "http://json-schema.org/draft-07/hyper-schema#",
"$id": "http://example.com/product-list.schema.json",
"type": "array",
"items": { "$ref": "product.schema.json" }
}
{
"$schema": "http://json-schema.org/draft-07/hyper-schema#",
"$id": "http://example.com/product.schema.json",
"type": "object",
"properties": {
"productId": { "type": "integer" },
"productName": { "type": "string" },
"price": { "type": "number" }
},
"links": [
{ "rel": "edit", "href": "/product/{productId}" }
]
}
The array is not allowed to update any item of the array. However, it's allowed to add/remove item of the array. Once an item is added to the array, the only action I can take is "remove".
{
"$schema": "http://json-schema.org/draft-07/hyper-schema#",
"$id": "http://example.com/product-list.schema.json",
"type": "array",
"items": { "$ref": "product.schema.json" },
"links": [
{
"rel": "create",
"href": "/products",
"schema": { "$ref": "product-create.schema.json" }
}
]
}
{
"$schema": "http://json-schema.org/draft-07/hyper-schema#",
"$id": "http://example.com/product.schema.json",
"type": "object",
"properties": {
"productId": { "type": "integer" },
"productName": { "type": "string" },
"price": { "type": "number" }
},
"links": [
{ "rel": "delete", "href": "/product/{productId}" }
]
}
I need to save JSON parsed data to Cosmos Db, HTTP trigger works as it should as well as parsing but getting Partition key [my_dynamic_key_value] is invalid.
Did anyone have a similar issue?
I have found this article link but still getting the same error.
Thanks
EDIT 1
This is the flow for adding item to DB
Schema:
{
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"Groups": {
"type": "array",
"items": {
"type": "string"
}
},
"JobName": {
"type": "string"
},
"Link": {
"type": "string"
},
"MinSalary": {
"type": "string"
},
"MaxSalary": {
"type": "string"
},
"Hours": {
"type": "string"
},
"WorkPattern": {
"type": "string"
},
"Details": {
"type": "array",
"items": {
"type": "object",
"properties": {
"Name": {
"type": "string"
},
"Detail": {
"type": "string"
}
},
"required": [
"Name",
"Detail"
]
}
}
},
"required": [
"id",
"Groups",
"JobName",
"Link",
"MinSalary",
"MaxSalary",
"Hours",
"WorkPattern",
"Details"
]
}
}
Here is a response:
{
"code": "BadRequest",
"message": "Partition key [1bb2d44f-a066-4fa8-8a78-0cdcea1a756c] is invalid.\r\nActivityId: 345f9a99-534b-40cb-9dc0-9863dc8c90f5, \r\nRequestStartTime: 2020-04-28T08:04:46.8249255Z, RequestEndTime: 2020-04-28T08:04:46.8249255Z, Number of regions attempted:1\r\n, Microsoft.Azure.Documents.Common/2.10.0"
}
You need to set the partition key in the double quotes. Refer to sample screen shot below
Please feel free to update fitting title for this question.
I am trying to achieve something like this. An input JSON/data JSON must have an OWNER and a CHARGE_TO Account, but these don't have to be on the same Account (I.e. one Account can be OWNER, another can be CHARGE_TO) and must not contain account with any other roles.
NOTE: Need to define JSON schema which should be simple to maintain. i.e. Should be easy to add a new role in the condition. My valid and invalid JSONs are,
*************************** VALID JSON1 *********************************
{
"user": [
{
"name": "user1",
"roles": ["OWNER"]
},
{
"name": "user2",
"roles": ["ANY_OTHER_ROLE"]
},
{
"name": "user3",
"roles": ["CHARGE_TO"]
}]
}
*************************** VALID JSON2 *********************************
{
"user": [
{
"name": "user1",
"roles": ["OWNER", "CHARGE_TO"]
},
{
"name": "user2",
"roles": ["ANY_OTHER_ROLE"]
}]
}
*************************** INVALID JSON *********************************
{
"user": [
{
"name": "user1",
"roles": ["OWNER"]
},
{
"name": "user2",
"roles": ["ANY_OTHER_ROLE"]
}]
}
A JSON is valid if it has a user with roles ("OWNER" & "CHARGE_TO") or if users with roles (user1 - "OWNER", user3-"CHARGE_TO", other users with any other role).
Below is JSON schema I tried (draft_07). THIS IS NOT A WORKING SCHEMA.
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Complex inner array",
"type": "object",
"properties": {
"user": {
"type": "array",
"contains": {
"type": "object",
"properties": {
"name": { "type": "string" },
"orderRoles": {
"type": "array",
"minItems": 1,
"items": { "type": "string" }
}
},
"oneOf": [
{ "properties": { "roles": { "enum": ["OWNER", "CHARGE_TO"] }}},
{ "properties": { "roles": { "enum": ["OWNER"] }}},
{ "properties": { "roles": { "enum": ["CHARGE_TO"] }}}
]
},
"items": {
"type": "object",
"properties": {
"name": { "type": "string" },
"orderRoles": {
"type": "array",
"minItems": 1,
"items": { "type": "string" }
}
}
}
}
}
}
Below is my working schema. This is not an elegant solution as schema becomes huge if a new role ("ADMIN") is added, i.e. schema condition would An input JSON/data JSON must have an OWNER, ADMIN and a CHARGE_TO Account, but these don't have to be on the same Account (I.e. one Account can be OWNER, one Account can be ADMIN & another can be CHARGE_TO) and must not contain account with any other roles. Please feel free to improve this. Thanks.
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Complex inner array",
"type": "object",
"allOf": [
{
"properties": {
"account": {
"type": "array",
"contains": {
"type": "object",
"properties": {
"roles": {
"type": "array",
"minItems": 1,
"contains": { "enum": ["OWNER"] }
}
}
}
}
}
},
{
"properties": {
"account": {
"type": "array",
"contains": {
"type": "object",
"properties": {
"roles": {
"type": "array",
"minItems": 1,
"contains": { "enum": ["CHARGE_TO"] }
}
}
}
}
}
},
{
"properties": {
"account": {
"type": "array",
"items": {
"type": "object",
"properties": {
"roles": {
"type": "array",
"minItems": 1,
"items":{
"enum": ["CHARGE_TO", "OWNER"]
}
}
}
}
}
}
}
]
}
You need to isolate the part of the schema that checks for a specific role. Then you can just combine them with allOf. There's as little duplication as possible and you can easily add another constraint.
{
"type": "object",
"properties": {
"user": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": { "type": "string" },
"roles": {
"type": "array",
"items": { "type": "string" }
}
}
},
"allOf": [
{ "$ref": "#/definitions/require-role-owner" },
{ "$ref": "#/definitions/require-role-charge-to" }
]
}
},
"definitions": {
"require-role-owner": {
"contains": {
"properties": {
"roles": {
"contains": { "const": "OWNER" },
"allOf": [{ "$ref": "#/definitions/not-other-role" }]
}
}
}
},
"require-role-charge-to": {
"contains": {
"properties": {
"roles": {
"contains": { "const": "CHARGE_TO" },
"allOf": [{ "$ref": "#/definitions/not-other-role" }]
}
}
}
},
"not-other-role": {
"items": { "enum": ["OWNER", "CHARGE_TO"] }
}
}
}
I have a scenario where i need to display arrays in side a named object like this:
"actions": {
"singleSelection": [
{
"chartable": false,
"label": ""
}
]
}
I have accomplished it with the following schema:
"schema": {
"type": "object",
"title": "smart_report",
"properties": {
"actions": {
"title": "Actions",
"type": "object",
"properties": {
"singleSelection": {
"title": "Action: Single selection",
"type": "array",
"maxItems": 10,
"items": {
"type": "object",
"properties": {
"field": {
"title": "Field name",
"type": "string"
},
"label": {
"title": "Label",
"type": "string",
"description": "Label will be used for column name."
},
"chartable": {
"title": "Chartable",
"type": "boolean"
}
}
}
}
}
}
}
Now am trying to set the 'notitle' flag on 'actions' in from and trying to access the properties of 'actions', but its not working as expected:
{
"key": "actions",
"notitle": true,
"properties": {
"key": "singleSelection",
"notitle": true,
"startEmpty": true
}
},
I still see title for actions as well for 'singleSelection' and 'stratEmpty' is also not set.
If you remove the titles from the schema, as well as using the notitle attribute it should work.
(In case you remove the title, but do not use the notitle attribute, the title will be displayed with the same name as the key).