Table of Contents

Entity validation rule

What are entity validation rules ?

Entity validation rules are used to check the validity of the properties of an entity in the server application. They are executed before saving but after the Saving event rules. If an entity validation rule returns an error, the save is cancelled. Because they are applied at the entity level, entity validation rules cannot be bypassed and are always executed.

Warning

Due to identification difficulties with EF Core, validation rules are not executed on entities with only an Image property set to null.

Writing the code of an entity validation rule

In this article, we will focus on the available properties.

You can either write the C# code directly in the Neos Studio code editor or in Visual Studio (after the entity validation rule has been saved once). See more about adding/editing server code.

Example with a CheckDiscount entity validation rule :

/// <inheritdoc/>
public override IValidationRuleResult Execute()
{
    if (Item.Discount < 0 || Item.Discount > 100)
    {
        return Error(GroupeIsa.Northwind.Domain.Properties.Resources.Sales.DiscountMustBeBetweenZeroAndHundred);
    }

    return Success();
}

Item property

The Item represents entity being validated.

Returned Result

The ExecuteAsync or Execute methods return a IValidationRuleResult.

You must use one of the following methods to obtain an instance of the result to return :

  • Error(string message) : returns an error result containing the message passed as parameter. This prevents the entity from being saved.
  • Success() : returns a successful result allowing the entity to be saved.

Rules properties

  • Name : name of the entity validation rule, used to name the corresponding C# class
  • Business assembly : business assembly that will contain the C# class representing the entity validation rule. The chosen assembly must belong to the entity validation rule module or one of its children.
  • Show Application layer assemblies : the source element of your code is in the "Domain" layer. This layer is the core of your business and we recommend placing all the code associated with the elements of this layer there. Placing your code in an "Application" layer assembly is only justified if your code uses elements from this layer (entity views, data objects, server methods, notifications...). See more about domain and application layers.
  • Asynchronous execution : this option must be checked when you execute asynchronous code. Otherwise, unchecking this option makes the code more readable by preventing the method from returning a "Task" object. If necessary, the signature of the generated method can always be changed afterwards.

Advanced options :

  • Trigger conditions : indicates whether the rule is launched when an item is created, modified and/or deleted.
Note

If you want to enable or disable the entity validation rule, you can use the switch button next to the name of the validation rule.

Renaming the rule

To rename the validation rule, click on the name of the rule or the button. The validation rule is reloaded before opening the modification popup to ensure that any changes made in Visual Studio are taken into account.