Welcome again to another Logic Apps Best Practices, Tips, and Tricks. In my previous blog posts, I talked about some of the essential best practices you should use while working with Azure Logic Apps. Check out these Logic App tips and tricks!
In the last two blog posts, we explained how to specify nullable and required elements/properties inside our JSON messages. Today we will continue on the same topic, JSON Schemas. This time I will speak about another Best practice, Tips, and Tricks that you must consider while designing your business processes (Logic Apps): Specifying JSON Schema restrictions.
Specifying JSON Schema restrictions
Primarily, JSON Schema is used to validate data, similar to, in its essence, an XML Schema stands for XML messages (but don’t get me wrong, they are very different).
- A JSON Schema is a declarative language that allows you to annotate and validate JSON documents. A JSON Schema enables the confident and reliable use of the JSON data format.
- A XML Schema (XSD for XML Schema Definition, or formerly WXS for W3C XML Schema) provides a mechanism to describe the general structure of an XML instance, which means the occurring elements, their attributes, and how they can be nested.
But for me, in a simple way, they both can define the contract of the message instance.
However, remember that JSON Schema is constraints based, meaning that anything not defined (or constrained) is allowed and considered valid.
Again, we saw in the last two previous blog posts how to specify nullable and required properties inside JSON Schemas to be used inside our Logic Apps. However, the question we want to know is: what other types of restrictions/validations can we perform that are supported inside Logic Apps?
Integer or Number types
If we work with integer or number types, we can make use of the following keywords:
- Use the minimum and maximum keywords if you need to specify a value in a range of numbers. For example, the age of a person must be an integer between 18 and 65:
- minimum: A number is valid against this keyword if it is greater than or equal to the value of this keyword.
- maximum: A number is valid against this keyword if it is lower than, or equal to, the value of this keyword.
"age": { "type": "integer", "minimum": 18, "maximum": 65 }
- exclusiveMinimum: A number is valid against this keyword if it is strictly greater than the value of this keyword.
- Note: The value of this keyword can be a number (integer or float) or a boolean. I personally use this generally with a true (boolean) value, and that means that the minimum value is not allowed.
- exclusiveMaximum: A number is valid against this keyword if it is strictly lower than the value of this keyword.
- Note: The value of this keyword can be a number (integer or float) or a boolean. Once again, I personally use this generally with a true (boolean) value, and that means that the maximum value is not allowed.
"age": { "type": "integer", "minimum": 18, "maximum": 65, "exclusiveMinimum": true, "exclusiveMaximum": true }
- multipleOf: A number is valid against this keyword if the division between the number and the value of this keyword results in an integer.
- Note: The value of this keyword must be a strictly positive number (zero is not allowed).
"age": { "type": "integer", "minimum": 18, "maximum": 65, "multipleOf": 2 }
- enum: use this keyword to restrict a value to a fixed set of values.
- Note: It must be an array with at least one element, where each element is unique.
"age": { "type": "integer", "enum" : [18, 24, 44, 65] }
String type
If we work with the string type, we can make use of the following keywords:
- minLength: A string is valid against this keyword if its length is greater than or equal to the value of this keyword.
- Note: The value of this keyword must be a non-negative integer.
- maxLength: A string is valid against this keyword if its length is lower than, or equal to, the value of this keyword.
- Note. The value of this keyword must be a non-negative integer.
"countryISOCode": { "type": "string", "minLength": 2, "maxLength": 2 }
In this sample, the countryISOCode always needs to have two characters.
- contentEncoding: A string is valid against this keyword if it is encoded using the method indicated by the value of this keyword. The support values for this keyword are:
- binary – any string is valid.
- base64 – the string must be a valid base64 encoded string.
- quoted–printable – string must be a valid quoted-printable text.
"phone": { "type": "string", "contentEncoding": "base64" }
- enum: use this keyword to restrict a value to a fixed set of values.
- Note: It must be an array with at least one element, where each element is unique.
"gender": { "type": "string", "enum" : ["Masculine", "Feminine"] }
Despite official documentation describes more keywords that you can use with string types:
- pattern: A string is valid against this keyword if it matches the regular expression specified by the value of this keyword. The value of this keyword must be a string representing a valid regular expression.
- contentMediaType: A string is valid against this keyword if its content has the media type (MIME type) indicated by the value of this keyword.
However, they are not supported inside Logic Apps!
Array type
If we work with an array type, we can make use of the following keywords:
- minItems: An array is valid against this keyword if the number of items it contains is greater than or equal to the value of this keyword.
- maxItems: An array is valid against this keyword if the number of items it contains is lower than, or equal to, the value of this keyword.
"emails": { "type": "array", "minItems": 1, "minItems": 10, "items": { "type": "object", "properties": { "email": { "type": "string" } }, "required": [ "email" ] } }
- uniqueItems: An array is valid against this keyword if an item cannot be found more than once in the array.
- Note: The value of this keyword must be a boolean. However, if set to false, the keyword validation will be ignored.
"emails": { "type": "array", "uniqueItems": true, "items": { "type": "object", "properties": { "email": { "type": "string" } }, "required": [ "email" ] } }
I hope you enjoy this developer tip and stay tuned for the following Logic App Best practices, Tips, and Tricks.