How can I achive that enable all checkboxes in jsonschema after I check radio button? - jsonschema

Okey. I have a jsonschema as below. I am trying to get all the items (colors - checkboxes) clicked by default when the radio button "YES" is marked. On the contrary, if the "NO" button is clicked, all the colors will be unchecked.
JsonSchema
{
"title": "Item Type Filtering Form",
"description": "Form for filtering Item Types according to selected Attribute Values.",
"type": "object",
"properties": {
"colorAll": {
"type": "boolean",
"title": "Seat Color All",
"enum": [
false,
true
],
"enumNames": [
"NO",
"YES"
],
"default": true
},
"colorList": {
"type": "array",
"title": "Seat Color",
"items": {
"type": "object",
"enum": [
{
"id": 1,
"label": "RED"
},
{
"id": 2,
"label": "BLUE"
},
{
"id": 3,
"label": "GREEN"
}
],
"enumNames": [
"RED",
"BLUE",
"GREEN"
]
},
"uniqueItems": true
}
}
}
UISchema
{
"colorAll": {
"ui:widget": "radio",
"ui:options": {
"inline": true
}
},
"colorList": {
"ui:widget": "checkboxes",
"ui:options": {
"inline": true
}
}
}
I am practising it on the page https://mozilla-services.github.io/react-jsonschema-form/# but none of my tries is working how I described above...
I thought, that I can make it with "default:" keyword and put all the values into it -> JsonSchema is valided, but it didn't work.
Can somebody help me with it?

Currently It does not seems to be possible
{
"title": "Schema dependencies",
"description": "These samples are best viewed without live validation.",
"type": "object",
"properties": {
"conditional": {
"title": "Conditional",
"$ref": "#/definitions/person"
}
},
"definitions": {
"person": {
"title": "Person",
"type": "object",
"properties": {
"colorAll": {
"type": "string",
"enum": [
"No",
"Yes"
],
"default": "No"
}
},
"required": [
"colorAll"
],
"dependencies": {
"colorAll": {
"oneOf": [
{
"properties": {
"colorAll": {
"enum": [
"Yes"
]
},
"colorList": {
"type": "array",
"title": "Seat Color",
"items": {
"type": "string",
"enum": [
"RED",
"BLUE",
"GREEN",
"Yes Only",
"ABC"
]
},
"default": [
"RED",
"BLUE",
"Yes Only"
],
"uniqueItems": true
}
}
},
{
"properties": {
"colorAll": {
"enum": [
"No"
]
},
"colorList": {
"type": "array",
"title": "Seat Color",
"items": {
"type": "string",
"enum": [
"RED",
"BLUE",
"GREEN"
]
},
"uniqueItems": true
}
}
}
]
}
}
}
}
}
if you run the above in playground, the list of colors changes, but it does not select the default ones. but if you have colorList has standalone component, it selects the default ones.

To change the default selected values, you need to use the "onChange" property of the form (https://react-jsonschema-form.readthedocs.io/en/latest/#form-data-changes) and handle that logic on your own. Thus, you can check if the radio button was toggled to true or false, and if so, set colorList to
[
{
"id": 1,
"label": "RED"
},
{
"id": 2,
"label": "BLUE"
},
{
"id": 3,
"label": "GREEN"
}
]
or [] respectively.
Note the following warning in the doc:
WARNING: If you have situations where your parent component can re-render, make sure you listen to the onChange event and update the data you pass to the formData attribute.
Here's an example codepen I set up that manages the two properties:
https://codepen.io/anon/pen/VOjJmY
Also note that because the actual value is an object, I think you have to reuse the same object (hence the direct use of schema.properties.colorList.items.enum).
I think there is a bug with React JSON Schema Form because the checkboxes' UI state doesn't get updated in the right lifecycle or something. The state is correctly updated, but I can't the un-toggle all/toggle all effect to happen on correct state, but rather the follow toggle... Like on going from "YES" -> "NO" -> "YES" they all switch off, and then going from "YES" -> "NO" they would switch back on...

Related

How to create a new database on Notion API with the new "Status" property?

Notion API HTTP requests are not working when I add a new "Status" propoerty for a new database creation.
I've been trying for a while to figure out why, sending Notion a simple HTTP POST request with a "Status" breaks it.
If I just replace "Status" with a simple "Select" it works fine...
This is the code that I tried on my latest attempt:
{
"is_inline": true,
"parent": {
"type": "page_id",
"page_id": page_id
},
"icon": {
"type": "emoji",
"emoji": "🐟"
},
"title": [
{
"type": "text",
"text": {
"content": "Catches",
"link": null
}
}
],
"properties": {
"Name": {
"title": {}
},
"Status": {
"status": {
"options": [
{
"name": "Not started",
"color": "default"
},
{
"name": "25% Complete",
"color": "blue"
},
{
"name": "50% Complete",
"color": "blue"
},
{
"name": "75% Complete",
"color": "blue"
},
{
"name": "Done",
"color": "green"
},
{
"name": "Blocked",
"color": "red"
}
]
}
}
}
}
Status ref page on Notions docs: https://developers.notion.com/reference/property-object#status-configuration

How to add conditional required validation on nested object controls in jsonforms?

I am using version 2.5.1 with react material renderer, with custom renderers for string, number, radio, and date.
{
"type": "object",
"properties": {
"name": {
"type": "string"
},
"married": {
"type": "string",
"enum": [
"Married",
"Single"
]
},
"child": {
"type": "object",
"properties": {
"child_name": {
"type": "string"
},
"child_married": {
"type": "string",
"enum": [
"Married",
"Single"
]
},
"child_address": {
"type": "string"
},
"grandChild": {
"type": "object",
"properties": {
"grand_child_name": {
"type": "string"
},
"grand_child_married": {
"type": "string",
"enum": [
"Married",
"Single"
]
},
"grand_child_city": {
"type": "string"
},
}
}
}
}
}
}
Following are the rules we need to implement
/properties/name , /properties/married are always visible and required
If properties/married is = “Married”
then /properties/child/child_married, /properties/child/ child_name, /properties/child/
child_name are visible and required.
If properties/child/child_married = “Married”
then /properties/child/grandChild/grand_child_married,
/properties/child/grandChild/grand_child_name,
/properties/child/grandChild/grand_child_city Are visible and required.
I was able to show/hide the respective properties based on the enum values in uischema using rules.
The problem I am facing is how to conditionally add the required property to the schema at such a deeply nested level?

JSON schema conditional within array based on "oneOf" selection

I'd like to to display a form field based on a selection within an array to be able to dynamically add items based on selection.
What works:
Without array wrapped around this works. See code below:
{
"schema": {
"type": "object",
"properties": {
"accessory": {
"title": "Type",
"type": "string",
"default": "one",
"oneOf": [
{ "title": "First", "enum": ["one"] },
{ "title": "Second", "enum": ["two"] }
],
"required": true
},
"setName": {
"type": "string"
},
"Second Name": {
"type": "string",
"description": "Only displayed if 'two' is selected",
"condition": {
"functionBody": "return model.accessory === 'two';"
}
}
}
}
}
What does not work:
But as soon as I wrap an array around it the condition is not working anymore.
{
"schema": {
"type": "object",
"properties": {
"accessories": {
"title": "Accessories",
"type": "array",
"items": {
"title": "Accessory",
"type": "object",
"properties": {
"accessory": {
"title": "Type",
"type": "string",
"default": "one",
"oneOf": [
{ "title": "First", "enum": ["one"] },
{ "title": "Second", "enum": ["two"] }
],
"required": true
},
"setName": {
"type": "string"
},
"Second Name": {
"type": "string",
"description": "Only displayed if 'two' is selected",
"condition": {
"functionBody": "return model.accessory === 'two';"
}
}
}
}
}
}
}
}
I've also tried the following conditions:
"return model[arrayIndex].accessory === 'two';"
and
"return ['two'].includes(model.accessory);"
This one works:
"Second Name": {
"type": "string",
"description": "Only displayed if 'two' is selected",
"condition": "model.accessories[arrayIndex].accessory=='two'"
}

How do i define this jsonSchema rule

I have the jsonSchema like below
[{
"path": [
"General",
"label"
],
"type": "label",
"label": "Calculate Losses From Sub-Peril(s)",
"required": true
},
{
"path": [
"General",
"fire"
],
"type": "boolean",
"default": true,
"label": "Fire"
},
{
"path": [
"General",
"fireSmoke"
],
"type": "boolean",
"default": false,
"label": "Fire and Smoke"
}
],
I have the jsonSchema Rule as below. So if the user unchecks both fire and Fire and Smoke i want to fire a notification. How do i define JsonSchema Rule. Can some one fix this? I have the below rule but it gives me parser error. Atleast one checkbox has to be selected.
{
"path": [
"General",
"fire"
],
"effect": "fireNotification",
"notification": {
"type": "warning",
"message": "At least one Sub-Peril must be selected",
"notificationID": "fire",
"dismissible": "false"
},
"condition": {
"operator": "oneOf": {
"properties": {
"fire": {
"enum": [
true
]
},
"fireSmoke": {
"enum": [
true
]
}
}
}
}
}
Your JSON is invalid in two ways, both on the same line:
"operator": "oneOf": {
After "operator":, you need another opening brace ({) (and a matching closing brace later on.
The value of "oneOf": should be an array, so you need to add an opening bracket ([) there (and a matching closing bracket later on).
Further, you say "At least one checkbox has to be selected", but you are using a "oneOf" rule, not "anyOf". With "oneOf", if both properties are true, the validation will fail.

Angular6 JSON schema form for Array of Items

I am trying out Angular6 JSON form for my application and stuck in the issue of having array schema
The basic layout looks like
{
"schema": {
"type": "array",
"properties": {
"type": { "type": "string" },
"number": { "type": "string" },
}
},
"layout": [
{
"type": "array",
"items": [ {
"type": "div",
"displayFlex": true,
"flex-direction": "row",
"items": [
{ "key": "type", "flex": "1 1 50px",
"notitle": true, "placeholder": "Type"
},
{ "key": "number", "flex": "4 4 200px",
"notitle": true, "placeholder": "Phone Number"
}
]
} ]
}
],
"data": [
{ "type": "cell", "number": "702-123-4567" },
{ "type": "work", "number": "702-987-6543" }
]
}
But I am not getting the expected outcome, that is Form is prefilled with the data
[
{ "type": "cell", "number": "702-123-4567" },
{ "type": "work", "number": "702-987-6543" }
]
Refer: https://hamidihamza.com/Angular6-json-schema-form/
Based on your code there are some parts you may need to revisit.
The schema type should be either an object or boolean based on the documentation http://json-schema.org/latest/json-schema-core.html
Within the schema section, it seems that you want the type and number to be your properties of a JSON instance. Having this you can only pass one instance of data to the framework to fill in your properties because the framework cannot decide on which value to use for your property of type string.
In case of looking for having an array of type and number, you can have a property like "phone number" with the type array. below is an example from angular6-json-schema flex layout example which I think you had as your reference.
"schema": {
...
"phone_numbers": {
"type": "array",
"items": {
"type": "object",
"properties": {
"type": { "type": "string", "enum": [ "cell", "home", "work" ] },
"number": { "type": "string" }
},
"required": [ "type", "number" ]
}
And pass your data having something as follows:
"data": {
...
"phone_numbers": [
{ "type": "cell", "number": "702-123-4567" },
{ "type": "work", "number": "702-987-6543" }
],
}