---
title: "Action script with a validation trigger"
slug: "action-script-with-a-validation-trigger"
abstract: "Web script example that validates an asset's properties during asset creation or modification."
---
# Action script with a validation trigger

This example shows an action web script that verifies that an asset satisfies specific criteria, such as having required fields or valid relationships, before allowing it to be saved or published. The script is connected to a validation trigger that verifies data integrity before committing changes.

> [!note]
> **Before you begin**
> -   Ensure the asset or assets referenced in the script exist in the Content Hub schema.
>
> -   Define any required properties, such as `Title` or `Description`, used in the script logic.
>
> -   Verify that users or roles executing the script have the necessary permissions to create or modify the referenced entities.

## Script

```csharp
using System.Linq;

var webExtensions = new[] { ".jpg", ".jpeg", ".png", ".gif" };

var entity = Context.Target as IEntity;

var filename = await entity.GetPropertyValueAsync<string>("FileName");
if (string.IsNullOrEmpty(filename)) return;

var extension = GetExtension(filename)?.ToLowerInvariant();

if (string.IsNullOrEmpty(extension)) return;

if (!webExtensions.Contains(extension))
{
  throw new ValidationException(
    "The asset is not valid.",
    new ValidationFailure("The file's extension must be the extension of a valid web filetype.", filename));
}

string GetExtension(string path)
{
  var tokens = path.Split('.');
  if (tokens.Length > 1)
  {
    return "." + tokens[tokens.Length - 1];
  }
  return null;
}

```

## Script explanation

This section steps through the script in execution order, explaining each part. The numbered items describe the sequence, not instructions to perform.

1. Include the libraries to use in the script. To understand which libraries to include, see [Context properties](https://doc.sitecore.com/ch/en/developers/cloud-dev/script-properties.html "Script properties") (property types).

    ```csharp
    using System.Linq;
    ```

2. Define valid web extensions. If an asset's file extension is not in this list, then its filename is not considered valid.

    ```csharp
    var webExtensions = new [] { ".jpg", ".jpeg", ".png", ".gif" };
    ```

3. Retrieve the `Target` object from the `Context` and casts it to `IEntity`. `Target` is the asset being created or modified.

    ```csharp
    var entity = Context.Target as IEntity;
    ```

    > [!warning]
    > The `TargetType` context property defines the type of `Target`, which can be `Entity`, `EntityDefinition`, `Policy`, or `DataSource`. The `Target` object type depends on the trigger's objectives, such as entity, entity definition, or policy. Since this trigger is for entity creation and entity modification, `Target` must be cast to `IEntity`.

4. Retrieve the `Filename` property from the `Target` object using `GetPropertyValueAsync`.

    ```csharp
    var filename = await entity.GetPropertyValueAsync<string>("FileName");
    ```

    > [!note]
    > The scripting API uses lazy loading. `GetPropertyValueAsync` checks whether the property is already loaded. If it's not, the script will load it for the user.

5. If the asset doesn't have a filename, the script exits.

    ```csharp
    if (string.IsNullOrEmpty(filename)) return;
    ```

6. The `GetExtension` function, which is defined at the end of the script, extracts the file extension from the file path. The script then uses `GetExtension` to obtain the extension from the filename. It splits the path by periods (`.`) and returns the last segment prefixed with a dot (`.`) if there are multiple segments. If no extension is found, it returns `null`.

    ```csharp
    string GetExtension(string path)
    {
      var tokens = path.Split('.');
      if (tokens.Length > 1)
      {
        return "." + tokens[tokens.Length - 1];
      }
      return null;
    }
    ```

7. Retrieve the extension from the filename using `Path.GetExtension (System.IO)`.

    ```csharp
    var extension = GetExtension(filename)?.ToLowerInvariant();
    ```

8. If the filename does not have an extension, the script exits.

    ```csharp
    if (string.IsNullOrEmpty(extension)) return;
    ```

9. If the extension is not a valid web extension, the script throws a `ValidationException`.

    ```csharp
    if (!webExtensions.Contains(extension))
    {
      throw new ValidationException(
        "The asset is not valid.",
        new ValidationFailure("The file's extension must be the extension of a valid web filetype.", filename));
    }
    ```

    > [!note]
    > For more information about `ValidationException` and `ValidationFailure`, as well as a list of the available custom exceptions, see the [SDK reference](https://doc.sitecore.com/ch/en/developers/api-reference/sdk--stylelabs.m.sdk.exceptions.html).

## Setup

1. [Create](https://doc.sitecore.com/ch/en/developers/cloud-dev/work-with-scripts.html "Work with scripts"), publish, and enable an *Action* script.
2. [Create an action](https://doc.sitecore.com/ch/en/developers/cloud-dev/create-an-action.html "Create an action") of type *Action script* and link it with the script.
3. [Create a new trigger](https://doc.sitecore.com/ch/en/developers/cloud-dev/create-a-trigger.html "Create a trigger") and set the trigger's objective to *Entity creation* and *Entity modification*.
    - In the trigger conditions, add the entity definition *File (M.File)* then add the condition *Filename has changed*.
    - In the trigger actions, add the new action under **Validation actions**.
4. Save and activate the trigger.