EMF Validation for Datatype constraints

EMF Validation for Datatype constraints

After defining a model, it is a common next step to define validation rules. Often there is a requirement to have attributes with a restricted length or attributes with values in a specific range. Furthermore, it can be the case, that there are multiple attributes with the same restriction in different places of the model. To solve such a requirement EMF offers a simple solution: EDataTypes with Annotations.

This is a step-by-step guide how to define your own restricted DataType.

First, you need to define a DataType:


Then the properties of the DataType must be set, this includes the actual Type (the Instance Type Name) and the new Name of the custom DataType:

In the next step, you must define the constraints with an Annotation on the DataType:


In the properties of the Annotation, the source must be set to “http:///org/eclipse/emf/ecore/util/ExtendedMetaData”:

 To define the constraints, a Details Entry must be added to the annotation:

You can add multiple Details Entries, if multiple constraints must be set e.g. the lower and upper bounds of a range restriction.

The key of the Details Entry corresponds to the constraint type and the value corresponds to the constraint value:

  The complete DataType definition looks like this:

 You can now use this definition as an attribute type of any EAttribute in the model:

An attribute with such an AttributeType will automatically provide validation results, e.g. via the Diagnostician. Therefore, frameworks like EMF Forms will automatically evaluate and display them:

You can find the complete list of possible keys here. Just search for “Facet” and look at the “details key”.

For your convenience, the most important ones are:

  • pattern: allows to define a regular expression which must be met

  • totalDigits: allows to define the maximal number of digits of a number

  • minLength: allows to define the minimal length of a value

  • maxLength: allows to define the maximal length of a value

  • minExclusive: allows to define a minimal value which is still invalid

  • maxExclusive: allows to define a maximal value which is already invalid

  • minInclusive: allows to define a minimal value which is already valid

  • maxInclusive: allows to define a maximal value which is still valid

Have fun defining your DataTypes with custom restrictions!

Jonas Helming and Maximilian Koegel

Jonas Helming and Maximilian Koegel

Jonas Helming and Maximilian Kögel lead the EclipseSource office in Munich and work as a consultants and software engineers for Eclipse technologies. They are Eclipse experts with a focus in Eclipse Modeling, project leads and committers to various Eclipse projects including EMF Forms, EMFStore, Edapt, e4 and the EMF Client Platform and regular EclipseCon speakers on these topics. Furthermore they are dedicated to teach Eclipse and open source technology not only to professionals but also to talented university students in an Eclipse lab course.

Email: munich@eclipsesource.com

Jonas Helming and Maximilian Koegel

Latest posts by Jonas Helming and Maximilian Koegel (see all)

  • Philippe
    Posted at 3:22 pm, September 23, 2014

    Hi Maximilian,

    great post.

    I tried to follow your instructions. The generator works fine. I can see new adequate methods in my ModelValidation.java File.

    But there is a wired behavior. I have to wizards with ECPSWTView inside. One for creating an EObject and another for showing/editing. The creating-view doesn’t show the new validation rule (not even under DIAGNOSTIC). By the editing-view it’s there.

    Are there any restrictions where a validation-rule is ignored?

  • Philippe
    Posted at 11:56 am, September 24, 2014

    Of course the error came up somewhere else. Sorry! Now its working perfect