logo
logo

Action script example (Validation phase)

This is an example of an Action script to be executed by a Trigger in the Validation phase (In process) when an asset is created or modified. It validates if an asset's filename has a valid Web extension.

Use case

  1. A user creates or modifies an asset.
  2. If the asset's filename does not have a valid Web extension, the application throws an exception.

Script

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

  1. Include the libraries to be used in the script.

    using System.Linq;
    Tip

    To know which libraries to include, please refer to the Context properties section (property types).

  2. Define the list of Web extensions. If an asset's filename extension is not in the list, it is not a valid filename.

    var webExtensions = new [] { ".jpg", ".jpeg", ".png", ".gif" };
  3. Retrieve the Target object from the Context and cast it to IEntity. Target is the asset involved in the creation or modification event.

    var entity = Context.Target as IEntity;
    Warning

    The Target object type depends on the Trigger's objectives (e.g. entity, entity definition, policy). In this case, the Trigger's objectives are Entity creation and Entity modification. Hence, we need to cast Target to IEntity.

    Tip

    The context property TargetType indicates the type of Target. The possible values of TargetType are Entity, EntityDefinition, Policy and DataSource. For more information about context properties, please refer to the Context properties section.

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

    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, it will load it for the user.

  5. If the asset does not have a filename, the script exits.

    if (string.IsNullOrEmpty(filename)) return;
  6. Get the extension from the filename using Path.GetExtension (System.IO).

    var extension = GetExtension(filename)?.ToLowerInvariant();
  7. If the filename does not have an extension, the script exits.

    if (string.IsNullOrEmpty(extension)) return;
  8. If the extension is not a valid Web extensions, the script throws a Validation exception.

    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, please refer to the SDK API reference guide (Stylelabs.M.Sdk.Exceptions).

  9. This is an internal method used to get the extension of the filename.

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

Setup

  1. Create, publish and enable an Action script.

  2. Create an action of type Action script and link it with the script.

  3. Create a new trigger and set the trigger's objective to Entity creation and Entity modification.

    • In the trigger conditions, add the entity definition Asset then add a new condition.
    • In the trigger actions, add the action under Validation actions.
  4. Save and enable the trigger.

Can we improve this article ? Provide feedback