Advanced validation script structure
The advanced script structure contains up to three sections, which are all contained in a rule
.
rule { given { } when { } then { } }
rule { given { definitions = attributes['Definition'] } when { isEqual(type.id,\ '00000000-0000-0000-0000-000000011001') } then { isNotEmpty(definitions, \ message: "The asset ${name} \ in domain ${vocabulary.name} must have at least one definition") } }
Given-section
You can use this optional section to define variables that you can re-use in the rest of the script. Variables can make the rest of the validation script more readable. It allows you to separate data creation from the actual constraint-checking validation logic.
Each line of the given-section contains a variable name, an equal sign and an asset property. More info: Asset model.
The following given-section creates the variable definitions , which contains a list of the values of the Definition attributes.
given {
definitions = attributes['Definition']
}
When-section
You can use this optional section if certain conditions have to be met before the rule is executed. If you do not use this section, the validation logic is applied to all assets.
If an asset does not meet these conditions, the validation logic is not executed and the outcome of the rule is valid.
If the conditions are met, the validation logic is executed. The outcome of the rule will then depend on the actual validation logic in the then-section.
Each line of the when-section typically contains:
- A validation function. More info: Validation functions.
- An attribute from the given-section, an asset property or a constant value that you entered in the script.
The following when-section only allows assets to be validated if they are of a specific asset type.
when {
isEqual(type.id,'00000000-0000-0000-0000-000000011001')
}
Then-section
The then-section contains the pure validation logic. More information: Validation logic.