Repository navigation
Anatomy of a rules file
A PBI Inspector rules file is written in JSON. Rules can be written against any JSON file such as the Power BI report layout file or any Power BI theme file. A common use case is to write rules against the report layout file so the initial iterations of this documentation will focus on this. To test the report layout file we need to define a single "reportLayout" entry in the "pbiEntries" array as follows:
{
"pbiEntries": [
{
"name": "reportLayout",
"pbixEntryPath": "Report/Layout",
"pbipEntryPath": "report.json",
"contentType": "json",
"codepage": 1200,
"rules": []
}
]
}
The properties defined for the pbiEntry shown above (i.e. pbixEntryPath, pbipEntryPath, contentType and codepage) will not need to be modified from one rule file to another so I will skip a description of them for now and focus on the things you really want and need to know i.e. how custom rules are defined.
A custom rule is defined as per the following example:
{
"name": "Reduce the number of visible visuals on the page",
"description": "Reports a test fail if the rule's maximum number of visible visuals on the page is exceeded. By default the base rules file specifies 20 as the maximum, set the paramMaxVisualsPerPage parameter to change this. To disable this rule, mark it as disabled in the base rules file.",
"logType": "warning",
"disabled": false,
"forEachPath": "$.sections[*]",
"forEachPathName": "$.name",
"forEachPathDisplayName": "$.displayName",
"path": "$.visualContainers[*].config",
"pathErrorWhenNoMatch": false,
"test":[]
}
- name: the rule's name.
- description: the rule's description.
- logType: warning|error. Should the rule's test fail, this attributes determines whether the result should be logged as a warning or an error.
- disabled: true|false. A simple way to "turn off" the rule so it doesn't run.
- forEachPath: You will likely often want to loop through report elements and run a test on each of them, for example report pages or report visuals. The forEachPath property allows for a JSON Path to be defined that points to an Json array of to be looped through.
- forEachPathName: A JSON Path to the loop node's unique name which is displayed in the rule's results, for example the unique report page's name.
- forEachPathDisplayName: A JSON Path to the loop node's unique name which is displayed in the rule's results, for example the report page's display name.
- path: The JSON Path to the child nodes to be tested, for example the report page's visuals.
- pathErrorWhenNoMatch: true|false. Determines if an error should be raised in case the JSON Paths defined above do not match any JSON nodes.
A test is defined in three parts as follows:
"test": [
{The logic part},
{The data mapping part},
{The expected result part}
]
PBI Inspector uses a JsonLogic implementation to allow for the expression of complex logic itself written in JSON format. In turns out to be ideally suited to the task of testing the Power BI report layout (or theme) JSON definition. See https://jsonlogic.com/ for more details.
This is the part that "connects" the Power BI report JSON returned by the path property (defined above) to the logic part. This is defined as a JsonPointer path to the json node. In many cases, we'll just want to pass through the JSON node(s) returned by the path property described above, for example the report page's visuals. For such a simple pass through, a simple dot "." can be used.
The logic part will return a value, this could be true, false, a string, a number or an array. The rule result will be logged as true if it matches the expected result and false otherwise.
Here is a rule example that checks that charts are wider than they are tall. It's not a particularly useful rule but it's quite easy to understand and helps illustrate the concepts described above.
{
"name": "Charts wider than tall",
"description": "Want to check that your charts are wider than tall?",
"disabled": false,
"logType": "warning",
"forEachPath": "$.sections[*]",
"forEachPathName": "$.name",
"forEachPathDisplayName": "$.displayName",
"path": "$.visualContainers[*].config",
"pathErrorWhenNoMatch": false,
"test": [
{
"any": [
{
"var": "visualsConfigArray"
},
{
"<=": [
{
"var": "layouts.0.position.width"
},
{
"var": "layouts.0.position.height"
}
]
}
]
},
{
"visualsConfigArray": "."
},
false
]
}
Let's explain how the above example works:
- The foreachPath property is set to "$.sections[*]" which returns (and loops through) an array of all report pages.
- The path property is set to "$.visualContainers[*].config" which returns an array of all visual definitions for the current page iteration.
- We then map the root of the visuals configurations array to a "visualsConfigArray" variable in the rule's data mapping part using the "." notation.
- In the rule's logic part, we use the "any" JsonLogic operator to find any visual in the array with a width smaller or equal to its height. The "var" JsonLogic operator accepts a JsonPointer path to find the width and height properties within the visual's config json node.
- Finally in the rule's expected result part, we specify false as we do not expect any visual to fail the test. If the any JsonLogic finds a visual on a page with a width smaller or equal to its height then the rule will return true for the report page iteration and report either an error or a warning depending on the value of its logType property.
A true or false test return value is of limited value however as we don't have any information about which visuals failed. What would be a lot more useful would be to return an array of unique visual names that fail the test. The example below demonstrates how to do this by using the JsonLogic map operator. In this case we filter the visuals' config array and return any visuals that are taller than wide, we then use the map operator to select each of those visuals' name. In the rule's expected result part, we specify an empty array (i.e. []) so that any returned populated array fails the test.
{
"name": "Charts wider than tall",
"description": "Want to check that your charts are wider than tall?",
"disabled": false,
"logType": "warning",
"forEachPath": "$.sections[*]",
"forEachPathName": "$.name",
"forEachPathDisplayName": "$.displayName",
"path": "$.visualContainers[*].config",
"pathErrorWhenNoMatch": false,
"test": [
{
"map": [
{
"filter": [
{
"var": "visualsConfigArray"
},
{
"<": [
{
"var": "layouts.0.position.width"
},
{
"var": "layouts.0.position.height"
}
]
}
]
},
{
"var": "name"
}
]
},
{
"visualsConfigArray": "."
},
[]
]
}