Validation script examples

These are examples of real validation scripts of validation rules.

  • An asset must have at least one definition.

    Copy code

    rule {
    isNotEmpty (attributes.'Definition'?.first(), \ message:"The Asset ${name} in domain ${vocabulary.name} \ must have at least one Definition")
    }
  • An attribute must comply to a given pattern (defined using a regular expression).

    Copy code

    rule {
    given {
    codes = attributes.'Code'
    }
    when {
    isNotEmpty (codes)
    }
    then {
    matches (codes.first(), /S\d\d\d\d\d000/,\
    message: "The asset ${name} in domain \ ${vocabulary.name} Code must start with\ an S, then 5 digits, then 000")
    }
    }
  • All assets with a related to relation must have at least one uses relation.

    Copy code

    rule {
    given {
    relatedTo = relations.'Related To'
    uses  = relations.'Uses'
    }
    when {
    isNotEmpty (relatedTo)
    }
    then {
    isNotEmpty (uses, message: "Asset ${name}\ has Related To relation, so it must \ also have a Uses relation")
    }
    }
  • An asset must comply to the following naming rule, defined by a regular expression: starts with T, followed by 4 digits, 2 zeroes and exactly one 0, 1 or 2.

    Copy code

    rule {
    matches (name, /T(\d{4})0{2}[0,1,2]/, \ message: "Asset ${name} does not comply to naming rule")
    }
  • An asset name must consist of one or more words in capitals.

    Copy code

    rule {
    matches(name, /(\p{Upper}+|\p{Space}*)*/, \ message: "Name ${name} should be words in capitals")
    }
  • All codes for a given asset must be in a different code list.

    Copy code

    rule {
    given {
    code_domains = \ relations.'Code'?.vocabulary?.name
    }
    then {
    isUnique (code_domains)
    }
    }
  • A code must be related to only one business term.

    Copy code

    rule {
    given { terms = relations.'Business Term'
    }
    when { isNotEmpty (terms)
    }
    then { isEmpty (terms[1] )
    }
    }
  • All target assets using the Transform complex relation following the from and to branch, must have the same name as the current asset.

    Copy code

    rule {
    isEvery (\ complexRelations.'Transform'.from.to.name, \ {isEqual(name, it)}, \ message: "Not all From and To names of the \ Transform relation are equal")
    }
  • The total score of each asset related to the current asset through an Impacted By relation may not exceed 100.

    Copy code

    rule {
    given {
    impacted = relations.'Impacted By'.
    totalScoresForEachRelatedAsset = impacted?.collect\ {it.attributes['Score'].sum {it.toIteger()}}
    }
    when {
    isNotEmpty (totalScoresForEachRelatedAsset)
    }
    then {
    isEvery (totalScoresForEachRelatedAsset, \ { isLessEq (it, 100)}, \ message: 'Not all related assets have a \ total score smaller or equal to 100')
    }
    }
  • When the first target asset of the current asset using the Is Grouped By relation is inactive, then the current asset must be inactive. Whether or not the current asset is active or not, is based on the boolean attribute Active.

    Copy code

    rule {
    given {
    reference = relations.'Is Grouped By'?.first()
    active = reference.attributes.'Active'?.first()
    }
    when {
    isNotEmpty (reference) && isFalse (active)
    }
    then {
    isFalse (active, message: "The node '${name}' in\ '${vocabulary.name}' should also be inactive, \ since it parent is inactive.")
    }
    }