Scripting
Learn How to Use Scripting to Implement More Validation and Security Solutions
Last updated
Learn How to Use Scripting to Implement More Validation and Security Solutions
Last updated
This documentation is based on the FoodCrunch use case. Please open the link below alongside this page to understand the examples.
Introduction and Use CaseSome business rules around security and validation are hard to solve with a generic feature that works for everybody and is easy to use. The workflow system has limitations as well and you cannot write permissions that depend on the data of the content.
In general, scripting can be used to handle gaps in the Squidex feature set.
You can create scripts that run whenever a content item is created, updated, deleted, queried or when the status changes (e.g. from Draft to Published).
Scripts can be defined in the schema editor:
Go to your App (1).
Go to the Schema (2) settings.
Select the schema (3) you want to write a script for, i.e startups
in this example.
Select the Scripts (4) tab
Select the tab (5), depending on when you want the script to run, to work with the editor.
In the editor, you can define scripts for the following actions:
Query script is executed whenever a content item is queried with the API, but not when queried by the Management UI.
Prepare Query is called once for all content items of the current query. It can be used to precompute or prefetch data.
Create script is executed before a content item is created.
Change script is executed before the status of a content item is changed. When you use scheduling to change the status of a content item in the future, the script is called just before the status is changed and not when you schedule it. This can also stop your scheduling, when the script fails or it rejects the change.
Delete script is executed before a content item is deleted.
Update script is executed before a content item is updated.
Content creation and updates happen in the following order:
The content item is loaded. If it does not exist, the API responds with 404 (NotFound).
The data from the request is validated. The API responds with 400 (BadRequest) for invalid data.
The script is executed.
The data from the request is enriched with configured default values.
The constraints, such as unique fields are checked.
This means that you have the guarantee in your scripts, that the data is always valid and that you cannot violate constraints such as unique fields when you auto-generate or change content data.
Scripts are executed for the REST endpoint as well as for the GraphQL endpoint.
Asset scripts can be defined in the settings:
Go to your App (1).
Go to the Settings (2).
Select the Asset Scripts (3) menu item.
Select the script type (4) you want to edit.
In the editor you can define scripts for the following actions:
Annotate script is executed before the metadata of an asset is changed.
Create script is executed before an asset is created.
Moved script is executed before an asset is moved to another folder.
Delete script is executed before an asset is deleted.
Update script is executed before an asset is replaced with a new file.
The scripts are executed in a Sandbox. You do not have access to the file system and only to allowed functions. Only the ES5 JavaScript syntax is implemented so far, which means you cannot use Lambda expressions, Promises or classes.
All variables are accessible over the ctx
(Context) variable.
The following fields can be used for all scripts:
The following fields can be used for content scripts:
The following fields can be used for Asset scripts:
These methods are used to make changes to the content item or to reject changes.
replace()
Content scripts only. Tells Squidex that you have made modifications to the ctx.data
object and that this change should be applied to the content.
disallow()
Tells Squidex that this operation is not allowed and that a 403 (Forbidden)
status code should be returned. The user will see an alert in the Management UI.
reject(reason)
Tells Squidex that this operation is not valid and that a 400 (BadRequest)
status code should be returned. The user will see an alert in the Management UI.
complete()
Tells Squidex that the script should complete successfully.
Squidex provides a set of general helper functions for scripting and rule formatting.
Scripting Helper MethodsIn addition to that, there are also methods that are only available for scripting.
getJSON(url,
callback, headers?)
postJSON(url, body,
callback, headers?)
putJSON(url, body,
callback, headers?)
patchJSON(url, body,
callback, headers?)
deleteJSON(url,
callback, headers?)
Makes a request to the defined URL. If the request succeeds with a HTTP response status code (2XX) and a valid JSON response is returned the callback is invoked and the JSON response is passed to the callback as a JSON object. The script fails otherwise. You can also pass in an object with headers
getReferences(ids,
callback)
Queries the content items with the specified IDs and invokes the callback with the resulting content items when the request has been completed. If the current user does not have permissions to read the content items, the callback is invoked with an empty array.
getReference(id,
callback)
Queries the content item with the specified ID and invokes the callback with an array that includes the resulting content item when the request has been completed. If the current user does not have permissions to read the content item, the callback is invoked with an empty array.
getAssets(ids,
callback)
Queries the assets with the specified IDs and invokes the callback with the resulting assets when the request has been completed. If the current user does not have permissions to read assets, the script will fail.
getAsset(id,
callback)
Queries the asset with the specified ID and invokes the callback with an array that includes the resolved asset when the request has been completed. If the current user does not have permissions to read assets, the script will fail.
getAssetV2(id,
callback)
Queries the asset with the specified ID and invokes the callback with a a single asset when the request has been completed. If the current user does not have permissions to read assets, the script will fail.
getAssetText(asset,
callback)
Takes the specified asset and computes the text. The asset can not be larger than 4 MB, otherwise an error is returned.
getAssetBlurHash(asset,
callback)
Takes the specified asset and computes the blur hash. Read more: https://blurha.sh/
translate(
text,
targetLanguage,
callback,
sourceLanguage?)
Translates a given text to the target language and invokes the callback with the translated text when completed. The source language is usually detected automatically, but can be passed in and usually provides better results.
generate(prompt,
callback)
Generates content described by the prompt using OpenAI or other services and invokes the callback with the generated text.
If you want to understand your data structure and the context object, you can just write it to a string field.
Store in a separate field if another field has a valid value:
Calculate the slug for a content title automatically:
Calculate the number of words in a Markdown field:
Calculate the number of characters in a HTML field:
You can use the getJSON
function to enrich the content with data from external services. This example is a little bit more complicated that the other examples above, but let's jump into the code first:
When you make an asynchronous call to another service or content, the script engine cannot stop the script automatically. Therefore, it is very important to finish the script with a call to replace()
, even if you do not make a change to the content data.
There are some existing restrictions:
You cannot include external libraries.
You cannot make calls to external services, except getJSON
.
Scripts will timeout after 200ms of CPU execution.
Scripts will timeout after 5 seconds of total execution, e.g. waiting for external services with getJSON
.