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

RequestResponse

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

To know which libraries to include, see Context properties (property types). The context property TargetType indicates the type of Target. The possible values of TargetType are EntityEntityDefinitionPolicy and DataSource.

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

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

    RequestResponse
    
    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.

    RequestResponse
    
    var entity = Context.Target as IEntity;
    
    
    Warning

    The Target object type depends on the Trigger's objectives (for example, 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.

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

    RequestResponse
    
    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.

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

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

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

    RequestResponse
    
    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 API reference (Stylelabs.M.Sdk.Exceptions).

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

    RequestResponse
    
    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.

Do you have some feedback for us?

If you have suggestions for improving this article,