How do I write the data for this JSON Schema? - json

I was trying to use an online fake data generator to generate sample data from my json schema but it did not accept my schema for some reason but I don't see anything wrong with it. I want to do it manually but I'm not sure if I did it correctly. Does the data below match the schema?
Schema
{
"$schema": "http://somesite.com",
"description": "mychema",
"type": "object",
"properties": {
"Form": {
"description": "form properties",
"type": "object",
"properties": {
"formproperty": {
"description": "test value",
"type": "string"
}
}
},
"FormElements": {
"description": "form elements and their properties",
"type": "array",
"items": {
"title": "Form Element",
"type": "object",
"properties": {
"id": {
"description": "The unique identifier",
"type": "string"
},
"positionX": {
"description": "The X coordinate",
"type": "number"
},
"positionY": {
"description": "The Y coordinate",
"type": "number"
},
"elementSpecificProperties": {
"description": "unique properties",
"type": "object",
"oneOf": [
{ "$ref": "#/definitions/Label" }
]
}
}
}
}
},
"definitions": {
"Label": {
"type": "object",
"properties": {
"value": {
"description": "",
"type": "string"
}
}
}
}
}
Data
{
"Form":{
"formproperty": "test"
},
"FormElements":[
{
"id": "1",
"positionX": 20,
"positionY": 15,
"value": "testing1"
},
{
"id": "2",
"positionX": 5,
"positionY": 12,
"value": "testing2"
}
]
}

I can confirm that your schema is valid and that the data is valid against the schema. However, based on your schema, I think what you meant is this.
{
"Form": {
"formproperty": "test"
},
"FormElements": [
{
"id": "1",
"positionX": 20,
"positionY": 15,
"elementSpecificProperties": {
"value": "testing1"
}
},
{
"id": "2",
"positionX": 5,
"positionY": 12,
"elementSpecificProperties": {
"value": "testing2"
}
}
]
}

Related

Issues creating Json schema with enums

I am new to JSON and I am trying to create a JSON schema for a JSON object. There are tools online that can easily create JSON schema from a JSON object but my JSON seems to be complicated and needs some tweaking, but I am not sure how
example json
{
"iD": 123456789,
"balance": {
"currency": "Pounds",
"balance": 260.25,
"date": "2022-03-03"
},
"Permissions": {
"granted": [
"canLoginWithPassword",
"canGame",
"canWithdraw"
],
"failedConditions": [
{
"name": "we move",
"description": "Deposit Block",
"details": {
"reason": "Deposit Block"
},
"denied": [
"canDeposit"
]
}
]
}
The problem I am having is not being able to represent the granted section and denied section with enumerations. The granted and denied sections can have one or all of the privileges
Privileges
"canLoginWithPassword",
"canGame",
"canWithdraw",
"canDeposit"
From reading your question, I think the items, uniqueItems and enum keywords are the way to go for your schema:
{
"type": "object",
"$schema": "http://json-schema.org/draft-06/schema#",
"description": "JSON schema generated with JSONBuddy https://www.json-buddy.com",
"properties": {
"Permissions": {
"$ref": "#/definitions/Permissions"
},
"balance": {
"$ref": "#/definitions/balance"
},
"iD": {
"type": "integer"
}
},
"definitions": {
"privileges": {
"type": "array",
"uniqueItems": true,
"items": {
"type": "string",
"enum": [
"canLoginWithPassword",
"canGame",
"canWithdraw",
"canDeposit"
]
}
},
"details": {
"type": "object",
"properties": {
"reason": {
"type": "string"
}
}
},
"Permissions": {
"type": "object",
"properties": {
"failedConditions": {
"type": "array",
"items": {
"type": "object",
"properties": {
"denied": {
"$ref": "#/definitions/privileges"
},
"description": {
"type": "string"
},
"details": {
"$ref": "#/definitions/details"
},
"name": {
"type": "string"
}
}
}
},
"granted": {
"$ref": "#/definitions/privileges"
}
}
},
"balance": {
"type": "object",
"properties": {
"balance": {
"type": "number"
},
"currency": {
"type": "string"
},
"date": {
"type": "string"
}
}
}
}
}

validate one of the Array object in json schema conditionally

In below json is valid only if type is mobile and endDate is empty/null, else validation need to fail. Here type can have any values but my validation only on mobile type.
Valid json:
{
"contacts": [
{
"type": "mobile",
"endDate": "",
"number": "1122334455"
},
{
"type": "home",
"endDate": "",
"number": "1111122222"
},
{
"type": "mobile",
"endDate": "12-Jan-2017",
"number": "1234567890"
},
]
}
Invalid json: (since contacts don't have a valid mobile number)
{
"contacts": [
{
"type": "mobile",
"endDate": "12-Jan-2021",
"number": "1122334455"
},
{
"type": "home",
"endDate": "",
"number": "1111122222"
},
{
"type": "mobile",
"endDate": "12-Jan-2017",
"number": "1234567890"
},
]
}
Schema which i tried
{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"type": "object",
"properties": {
"contacts": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"properties": {
"type": {
"type": "string"
},
"endDate": {
"type": [
"string",
"null"
]
},
"number": {
"type": "string"
}
},
"anyOf": [
{
"if": {
"properties": {
"type": {
"const": "mobile"
}
}
},
"then": {
"properties": {
"endDate": {
"maxLength": 0
}
}
}
}
]
}
}
}
}
could anyone provide me a right schema for the above json, attaching the example code here, this is valid json but getting error. Invalid json example is here why because type mobile don't have empty endDate.
Thanks in advance.
I found the answer some how, we need not to use anyOf or oneOf. The right one is contains. place of the contains also a mater. working example is here
Here is correct schema
{
"type": "object",
"properties": {
"contacts": {
"type": "array",
"minItems": 1,
"contains": {
"type": "object",
"properties": {
"type": {
"const": "mobile"
},
"endDate": {
"type" : ["string", "null"],
"maxLength": 0
}
},
"required": ["type"]
},
"items": {
"type": "object",
"properties": {
"type": {
"type": "string"
},
"endDate": {
"type": [
"string",
"null"
]
},
"number": {
"type": "string"
}
}
}
}
}
}
Your schema defines properties > data but your instance data uses contacts rather than data. Changing either fixes your problem. You have otherwise done this correctly.
(If you only have one type to check, you do not need to wrap your if/then schema in an anyOf.)

In MS Power Automate, how to populate dynamic property dropdown value using the result from another action?

I created an action in MS Power Automation (Flow) that I want to dynamically generate the dropdown values. I follow the documentation and examples listed here:
Documentation: https://learn.microsoft.com/en-us/connectors/custom-connectors/openapi-extensions#use-dynamic-values
Example: https://github.com/microsoft/PowerPlatformConnectors/blob/97c0317f96dc1d9601ef2d0e76f826e83bd14351/connectors/Planner/apiDefinition.swagger.json
I setup my flow exactly as described in the official documents:
"/MyFlow/MyAction": {
"post": {
"description": "This is my action",
"operationId": "MyAction",
"parameters": [
{
"description": "Please select from the dropdown",
"in": "header",
"name": "DropdownSelector",
"required": true,
"type": "string",
"x-ms-summary": "Dropdown Selector",
"x-ms-dynamic-values": {
"operationId": "MyList",
"value-collection": "list",
"value-path": "ID",
"value-title": "Name",
"parameters": {
"Filter": {
"parameter": ""
}
}
}
}
],
"responses": {
"200": {
"description": "default",
"schema": {
"properties": {
"Selected ID": {
"description": "Selected ID",
"type": "string"
},
"Selected Name": {
"description": "Selected Name",
"type": "string"
}
},
"type": "object"
}
}
},
"summary": "Select from dropdown"
}
},
Here is the action that the list can get it's values from:
"/MyFlow/MyList": {
"post": {
"responses": {
"default": {
"description": "default",
"schema": {
"type": "object",
"properties": {
"list": {
"type": "array",
"items": {
"type": "object",
"properties": {
"Name": {
"type": "string",
"description": "Name"
},
"ID": {
"type": "string",
"description": "ID"
}
}
},
"description": "list"
}
}
}
}
},
"summary": "My List",
"description": "My List Description",
"operationId": "MyList",
"parameters": [
{
"name": "body",
"in": "body",
"required": false,
"schema": {
"type": "object",
"properties": {
"Filter": {
"type": "string",
"description": "Filter"
}
},
"required": [
]
}
}
]
}
}
The results for my list come back in this format and I have checked and it is correct:
{
"list": [
{
"Name": "Hello world",
"ID": "1"
}
]
}
Seem like everything is setup correctly, but it always show a blank dropdown, what am I doing wrong or missing?
Turns out my input parameter for the action to get API is incorrect. I'll leave this question here for future references.

Json schema validation for property that can only be set once on an object inside an array

Is it possible to validate the json data below, so that "info" can only be filled in when "name" is "a" otherwise it must be empty or null?
[
{
"name": "a",
"info": "this is mandatory"
},
{
"name": "b",
"info": "validation must fail"
}
]
the JSONSchema
{
"title": "Array of things",
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string",
"enum": [
"a",
"b"
]
},
"info": {
"type": "string"
}
}
}
}
The json in an online editor
try this:
{
"title": "Array of things",
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string",
"enum": ["a", "b"]
},
"info" : {
"type": ["string", "null"]
}
},
"required": ["name"],
"oneOf": [
{
"properties": {
"name": {"enum": ["a"] }
},
"required": ["info"]
},
{
"properties": {
"name": {"enum": ["b"] },
"info": {"enum": [null]}
}
}
]
}
}

How to use JSON schema validation against JSON document having arrays?

I have json document which contains three main elements along with sub elements. These elements also have arrays. I validated this document with json schema but I am not sure if there is any simple way to get rid of repeating schema for arrays as the current schema is too long.
JSON Schema:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "/",
"type": "object",
"properties": {
"book": {
"id": "book",
"type": "array",
"items": [
{
"id": "0",
"type": "object",
"properties": {
"isbn": {
"id": "isbn",
"type": "string"
},
"title": {
"id": "title",
"type": "string"
},
"price": {
"id": "price",
"type": "string"
},
"categories": {
"id": "categories",
"type": "array",
"items": [
{
"id": "0",
"type": "object",
"properties": {
"category": {
"id": "category",
"type": "string"
}
}
},
{
"id": "1",
"type": "object",
"properties": {
"category": {
"id": "category",
"type": "string"
}
}
},
{
"id": "2",
"type": "object",
"properties": {
"category": {
"id": "category",
"type": "string"
}
}
}
]
},
"warehouse": {
"id": "warehouse",
"type": "object",
"properties": {
"location": {
"id": "location",
"type": "string"
},
"aisle": {
"id": "aisle",
"type": "string"
},
"shelf": {
"id": "shelf",
"type": "string"
}
}
}
},
"required": [
"isbn",
"title",
"price",
"categories",
"category",
"warehouse"
]
},
{
"id": "1",
"type": "object",
"properties": {
"isbn": {
"id": "isbn",
"type": "string"
},
"title": {
"id": "title",
"type": "string"
},
"price": {
"id": "price",
"type": "string"
},
"categories": {
"id": "categories",
"type": "array",
"items": [
{
"id": "0",
"type": "object",
"properties": {
"category": {
"id": "category",
"type": "string"
}
}
},
{
"id": "1",
"type": "object",
"properties": {
"category": {
"id": "category",
"type": "string"
}
}
},
{
"id": "2",
"type": "object",
"properties": {
"category": {
"id": "category",
"type": "string"
}
}
}
]
},
"warehouse": {
"id": "warehouse",
"type": "object",
"properties": {
"location": {
"id": "location",
"type": "string"
},
"aisle": {
"id": "aisle",
"type": "string"
},
"shelf": {
"id": "shelf",
"type": "string"
}
}
}
}
},
{
"id": "2",
"type": "object",
"properties": {
"isbn": {
"id": "isbn",
"type": "string"
},
"title": {
"id": "title",
"type": "string"
},
"price": {
"id": "price",
"type": "string"
},
"categories": {
"id": "categories",
"type": "array",
"items": [
{
"id": "0",
"type": "object",
"properties": {
"category": {
"id": "category",
"type": "string"
}
}
},
{
"id": "1",
"type": "object",
"properties": {
"category": {
"id": "category",
"type": "string"
}
}
},
{
"id": "2",
"type": "object",
"properties": {
"category": {
"id": "category",
"type": "string"
}
}
}
]
},
"warehouse": {
"id": "warehouse",
"type": "object",
"properties": {
"location": {
"id": "location",
"type": "string"
},
"aisle": {
"id": "aisle",
"type": "string"
},
"shelf": {
"id": "shelf",
"type": "string"
}
}
}
}
}
],
"required": [
"0",
"1",
"2"
]
}
},
"required": [
"book"
]
}
And JSON document:
{
"book":[
{
"isbn":"0-672-33751-7",
"title":"Unity Game Development",
"price":"$55.99",
"categories":[
{
"category":"Game Development"
},
{
"category":"Unit Game Engine"
},
{
"category":"Beginner to Intermediate"
}
],
"warehouse":{
"location":"North Warehouse",
"aisle":"A16",
"shelf":"3"
}
},
{
"isbn":"978-0-9871530-7-4",
"title":"Jquery: Novice to Ninja",
"price":"$39.95",
"categories":[
{
"category":"JavaScript"
},
{
"category":"jQuery"
},
{
"category":"Web Development"
}
],
"warehouse":{
"location":"North Warehouse",
"aisle":"W03",
"shelf":"6"
}
},
{
"isbn":"0-538-74477-4",
"title":"Programming Logic and Design",
"price":"$89.99",
"categories":[
{
"category":"Programming"
},
{
"category":"JavaScript"
},
{
"category":"Computer Logic"
}
],
"warehouse":{
"location":"South Warehouse",
"aisle":"B44",
"shelf":"1"
}
}
]
}
You've got a lot of issues with this schema. I suggest you read http://spacetelescope.github.io/understanding-json-schema/ to get a better understanding of JSON Schema. Below is the corrected schema. I'll give some brief advice, but not a lot of explanation. Go read that link for more info.
id is required to be a absolute URL. "/" is not valid
Don't use id anywhere other than the root of the document. It has some unexpected properties. It's best to just avoid it.
The items keyword should be a schema not an array of schemas. The form you are using is not intended for arrays with a single type of item.
The required keyword applies only to properties in the schema it appears in. Sub-schemas need to define their own required properties.
The required keyword does not work on arrays. Use minItems and maxItems instead.
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"book": {
"type": "array",
"items": {
"type": "object",
"properties": {
"isbn": { "type": "string" },
"title": { "type": "string" },
"price": { "type": "string" },
"categories": {
"type": "array",
"items": {
"type": "object",
"properties": {
"category": { "type": "string" }
},
"required": ["category"]
}
},
"warehouse": {
"type": "object",
"properties": {
"location": { "type": "string" },
"aisle": { "type": "string" },
"shelf": { "type": "string" }
}
}
},
"required": [
"isbn", "title", "price", "categories", "warehouse"
]
}
}
},
"required": ["book"]
}