Add or remove behaviors and rules

In addition to modifying simple scalar values, you can add and remove objects within arrays. This powerful capability means you can bulk-add new behaviors or criteria to an existing rule, or add new rules anywhere within a rule tree configuration. You can also bulk-remove any existing behaviors, criteria, or rules. This section describes all of these possibilities.

Suppose you want add a caching behavior to a rule that applies to image matches.

This rather complex JSONPath expression matches an array of behaviors. The two test expressions filter results down to sets of behaviors whose parent rule includes a fileExtension criteria that includes jpg image extensions:

$..children[?(@.criteria[?(@.name == 'fileExtension' && 'jpg' in @.options.values)].behaviors

The last segment of the search can match the behaviors array itself, or an element within the array such as behaviors[-1]. Either way, the patch's path value needs to specify an index location.

This example adds a caching behavior object. The special - index adds the object to the end of the array:

"patches": [
    {
        "op": "add",
        "path": "/rules/children/0/children/2/behaviors/-",
        "value": {
            "name": "caching",
            "options": {
                "behavior": "MAX_AGE",
                "mustRevalidate": false,
                "ttl": "30d"
            }
        }
    }
]

👍

Make sure to configure any behavior or criteria objects to do what you want before bulk-adding them. To avoid validation errors, you should copy behaviors that already work.

To remove a behavior, you need to match the entire object. This modifies the previous search to select any caching behaviors triggered by an image fileExtension match:

$..children[?(@.criteria[?(@.name == 'fileExtension' && 'jpg' in @.options.values)].behaviors[?(@.name == 'caching')]

Based on the bulk search results, a remove patch operation like this trims the caching behavior from other executing behaviors:

"patches": [
    {
        "op": "remove",
        "path": "/rules/children/0/children/2/behaviors/4"
    }
]

The ability to bulk-add objects to arrays means you can also design new self-contained rules, and add the objects to your configuration. This simple search matches the first set of rules that executes under the top-level default rule:

$.children

This sample patch appends a new rule object to the children array. The rule matches common image file extensions, then applies Image Manager. You can add rules that are as large as you want:

"patches": [
    {
        "op": "add",
        "path": "/rules/children/-",
        "value": {
            "name": "Image Manager",
            "comments": "Scale images for mobile.",
            "criteriaMustSatisfy": "all",
            "criteria": [
                {
                    "name": "fileExtension",
                    "options": {
                        "matchCaseSensitive": false,
                        "matchOperator": "IS_ONE_OF",
                        "values": [ "jpg", "gif", "jpeg", "png", "imm" ]
                    }
                }
            ]
            "behaviors": [
                {
                    "name": "imageManager",
                    "options": {
                        "enabled": true,
                        "resize": true,
                        "applyBestFileType": true,
                        "superCacheRegion": "US",
                        "cpCodeOriginal": null,
                        "cpCodeTransformed": null,
                        "advanced": false,
                        "policyTokenDefault": "adapt4mobile"
                    }
                }
            ],
            "children": []
        }
    }
]

👍

It's easy to copy any rule that you know already works the way you want, for use as the patch value. Within Property Manager, select the rule you want to bulk-add, press the View Rule JSON button, and copy the JSON source data. But keep in mind that the JSON for each rule includes any nested sub-rules. The large JSON for the top-level default rule is the same one available with PAPI's Get a rule tree operation.