JSON Validator not validating the schema in Mule - mulesoft

I am have a following JSON schema which I am trying to validate using JSON Schema validator
JSON Schema:
{
"$schema": "http://json-schema.org/draft-07schema#",
"description": "Create Orders Schema",
"type": "object",
"properties": {
"street_address": {
"type": "string"
},
"country": {
"default": "United States of America",
"enum": ["United States of America", "Canada"]
}
},
"if": {
"properties": { "country": { "const": "United States of America" } }
},
"then": {
"properties": { "postal_code": { "pattern": "[0-9]{5}(-[0-9]{4})?" } }
},
"else": {
"properties": { "postal_code": { "pattern": "[A-Z][0-9][A-Z] [0-9][A-Z][0-9]" } }
}
}
When I am passing the wrong postal_code pattern for country "Canada", it is not validating and it passes through.
Invalid payload:
{
"street_address": "24 Sussex Drive",
"country": "Canada",
"postal_code": "10000"
}
Am I missing something or JSON validator doesn't support the conditional if-then-else?
Thanks in advance

The issue is probably that the schema uses the Draft 7 JSON Schema specification. The documentation of the connector says that the validate schema operation only supports draft 3 or 4:
This connector supports JSON Schema Validation Drafts 3 and 4 only.

Related

Custom policy does not maintain previous values on properties array

I am creating a custom policy following this documentation.
I created one, and it is working. The JSON schema for the policy is shown below:
{
"title": "ACME Custom Basic Auth Policy",
"description": "Basic Authentication policy which enforces security according with custom consumer credentials",
"type": "object",
"properties": {
"users": {
"title": "users",
"type": "array",
"items": {
"type": "object",
"required": [
"username",
"password"
],
"properties": {
"username": {
"title": "User Name",
"type": "string",
"default": []
},
"password": {
"title": "User Password",
"type": "string",
"#context": {
"#characteristics": [
"security:sensitive"
]
}
}
}
},
"minItems": 1
}
},
"#context": {
"#vocab": "anypoint://vocabulary/policy.yaml#",
"security": "anypoint://vocabulary/policy.yaml#"
},
"$id": "allow-dynamic-resources",
"$schema": "https://json-schema.org/draft/2019-09/schema"
}
When I go to API Manager, I can configure the values on first attempt, but when I go back to change the values, they do not appear.
This happens only when I configure an array. If I configure as an object, it works. How can I fix this?

How to create a typical country/province (or state) dependency dropdowns with react-jsonschema-forms?

I'm trying to code an example that shows two dropdowns: one for countries, and one for related states/provinces. When you choose a country on the first dropdown, the values of the second dropdown should be updated to display only the provinces/states of the selected country. Usually this is accomplished using some sort of Ajax call, but in this case the requirement is to try it purely with react-jsonschema-form. Is this actually possible ? I have seen several examples using dependencies / references, but none solving this particular scenario.
So far I came up with this: https://codesandbox.io/s/fervent-cherry-lokjk?file=/src/App.js
{
"definitions": {
"Countries": {
"title": "Country",
"type": "object",
"properties": {
"country": {
"type": "string",
"enum": [
"CANADA",
"USA",
"MEXICO"
]
}
},
"dependencies": {
"country": {
"oneOf": [
{
"properties": {
"country": {
"enum": [
"CANADA"
]
},
"province": {
"type": "string",
"title": "Province",
"enum": [
"AB",
"BC",
"MB"
]
}
}
},
{
"properties": {
"country": {
"enum": [
"USA"
]
},
"province": {
"type": "string",
"title": "State",
"enum": [
"AL",
"AK",
"AR"
]
}
}
},
{
"properties": {
"country": {
"enum": [
"MEXICO"
]
},
"province": {
"type": "string",
"title": "State",
"enum": [
"AGS",
"BC",
"BCS"
]
}
}
}
]
}
}
}
},
"title": "Demo for countries",
"type": "object",
"properties": {
"currentCountry": {
"$ref": "#/definitions/Countries",
"title": "Select country / province / state"
}
}
}
which basically can render a Country dropdown and a State/Province dropdown. When selecting a country, the corresponding state/province dropdown is updated correctly with the new set of enum values.
However, with this approach, the form will submit a field named "currentCountry" with two properties inside, the country and the province
currentCountry: { country: "USA" , province: "AL" }
but of course the idea is to submit "country" and "province" as two different form fields, without the need to be wrapped.
So, does anyone know how to achieve this typical use case ? Is there any best approach to it using JSON schemas ?
Maybe having to code a custom widget ? (I would like to avoid that if possible)
Thanks in advance !
After playing a bit more, seems I found the solution. A correct schema that does exactly what is needed is this one:
{
title: "Demo for countries",
type: "object",
properties: {
country: {
type: "string",
title: "Country",
enum: ["CANADA", "USA", "MEXICO"]
}
},
dependencies: {
country: {
oneOf: [
{
properties: {
country: {
enum: ["CANADA"]
},
province: {
type: "string",
title: "Province",
enum: ["ALBERTA", "BRITISH COLUMBIA", "MANITOBA"]
}
}
},
{
properties: {
country: {
enum: ["USA"]
},
province: {
type: "string",
title: "State",
enum: ["ALABAMA", "OREGON", "ARKANSAS"]
}
}
},
{
properties: {
country: {
enum: ["MEXICO"]
},
province: {
type: "string",
title: "State",
enum: ["JALISCO", "BAJA CALIFORNIA", "CHIAPAS"]
}
}
}
]
}
}
}
and the above codesandbox example has been updated to reflect this change.
Happy to know this scenario can be achieved with JSON schemas !

Loop each element of a stored Procedure ResultSet in PowerAutomate(SQL Server)

I want to add an action to my Flow which loops through all the items in a ResultSet and adds them to an SQL Server Table.
The Result set is retrieved using a stored procedure like shown here:
the return values look like this:
I can access the "ReturnCode" if using an SQL Server "Insert Row" Action like this:
but I have no idea how to perform the insert action for each element in this ResultSet, since I cannot access it when using a foreach Action:
forgive me if the solution is obvious, I'm new to PowerAutomate and can't quite figure out how return types are processed. Also if any required information is missing, please tell me so I can add it here.
Your response looks like a JSON to me so, using that you could use the "Parse JSON" cction in flow to create an "object" that you can use its properties.
For example:
Your JSON response:
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": [
"GML",
"XML"
]
},
"GlossSee": "markup"
}
}
}
}
}
When you paste that in the "Generate from Template" dialog and click "Done", you will get a schema like the one below:
{
"type": "object",
"properties": {
"glossary": {
"type": "object",
"properties": {
"title": {
"type": "string"
},
"GlossDiv": {
"type": "object",
"properties": {
"title": {
"type": "string"
},
"GlossList": {
"type": "object",
"properties": {
"GlossEntry": {
"type": "object",
"properties": {
"ID": {
"type": "string"
},
"SortAs": {
"type": "string"
},
"GlossTerm": {
"type": "string"
},
"Acronym": {
"type": "string"
},
"Abbrev": {
"type": "string"
},
"GlossDef": {
"type": "object",
"properties": {
"para": {
"type": "string"
},
"GlossSeeAlso": {
"type": "array",
"items": {
"type": "string"
}
}
}
},
"GlossSee": {
"type": "string"
}
}
}
}
}
}
}
}
}
}
}
Depending on the JSON response you, you might want to rename a few properties in your schema or even change their type - that will help you to see the properties in you next action:
EDIT: Your objects of type array (from the schema) can be used in the Foreach loop

JSON Schema draft 4 with unordered array

I would like to create schema which allows me to add multiple addresses when the following is true:
1)Correspondence address must be there
2)Residence address must be there
3)Other types of addresses could be there
4) They can appear in any order
Is this possible to solve with JSON schema? I heard about "contains" in draft 6. But since we are using Altova XML SPY (2018), which is only supporting draft 4, for schema creation, I would like to know how to solve this in draft 4. Are you aware of any good editor for draft 6?
I read JSON schema to enforce array contents but cannot find the answer there. I also read https://gist.github.com/anonymous/7359001 which explains how to have "contains" in draft 4, but was unable to apply it to my case.
So if you can guide me, how to use it for the following schema(which fullfills my requirements except number 4), I would be very thankfull.
{
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "Comment describing your JSON Schema",
"type": "object",
"properties": {
"addresses": {
"type": "array",
"items": [
{
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": [
"Corespondence"
]
},
"otherData": {}
},
"required": [
"type",
"otherData"
]
},
{
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": [
"Residence"
]
},
"otherData": {}
},
"required": [
"type",
"otherData"
]
}
],
"additionalItems": {
"$ref": "#/definitions/Address"
}
}
},
"definitions": {
"Address": {
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": [
"Corespondence",
"Residence",
"Other"
]
},
"otherData": {}
},
"required": [
"type",
"otherData"
]
}
}
}
draft-06 has "contains" keyword allowing to test the presence of items matching the schema in the array.
Alternatively, instead of heterogenous array you could use a map with difference address types defined in different properties, so your data could be:
{
addresses: {
correspondence: {/*...*/},
residence: {/*...*/}
// etc...
}
}
The reason contains was added to draft-06 is that it is not possible to accomplish the same thing with draft-04 keywords.
There are several implementations supporting draft-06 on the software page: http://json-schema.org/implementations.html

ElasticSearch - Find matches with exact value

I'm new working with elastic search and I'm trying to make a simple query work. I'm using elasticseach 2.3. I can't use a newer version I'm limited by another technology I'm using.
Basically I have News stored in the database with a title, a source and a publication date.
The query I'm trying to make should search for all the news that contain a certain keyword, come from some sources A or B and have a publication date in the range given.
So far I have this:
{
"query":{
"bool":{
"must":[
{
"bool":{
"should":[
{
"match":{
"source":"SOURCE_A"
}
},
{
"match":{
"source":"SOURCE_B"
}
},
{
"match":{
"title": "keyword"
}
}
]
}
}
],
"filter":{
"range":{
"publication_date":{
"gte":"DATE_FROM",
"lte":"DATE_TO"
}
}
}
}
}
}
The problem is that if a given source starts exactly the same as another source (for example: "SOURCE" and "SOURCE ABC") they are both included in the result. I would like to match exactly the same source.
Can anyone point me in the right direction?
Thanks!
The index is being created by Django Haystack but given its limitations I need to query the database myself. The index mapping is the following:
{
"myindex": {
"mappings": {
"modelresult": {
"properties": {
"django_ct": {
"type": "string",
"index": "not_analyzed",
"include_in_all": false
},
"django_id": {
"type": "string",
"index": "not_analyzed",
"include_in_all": false
},
"id": {
"type": "string"
},
"publication_date": {
"type": "date",
"format": "strict_date_optional_time||epoch_millis"
},
"source": {
"type": "string",
"analyzer": "ascii_analyser"
},
"summary": {
"type": "string",
"analyzer": "ascii_analyser"
},
"text": {
"type": "string",
"analyzer": "ascii_analyser"
},
"title": {
"type": "string",
"analyzer": "ascii_analyser"
},
"url": {
"type": "string",
"analyzer": "ascii_analyser"
}
}
}
}
}
}

Resources