EMF Validation for Datatype constraints

August 26, 2014 | 2 min Read

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:

  https://eclipsesource.com<https://lh4.googleusercontent.com/DIBxeGWSHz4HaF4TYgEQ2hZvr8eDN96ksXmlhpo9ZJFw6u_poEokPU32N1WP4WdPBGy9jlemUhNaFNKyO0BkHfXVaJD5RllKGNso_wxX6trq8wWvbBnuxi4tykugSk8pvg> does not exist

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:

https://eclipsesource.com<https://lh6.googleusercontent.com/wXiRcFC_9QUbhXs02SvaO_QJ2ppkOMMEy-NpcLd0J3ifVQCrLxuVqR5-x2vKnQa8bW5sbzGyd8yiU5-qHi3ECzWNw5y8Mfe2Zjs1CK-T73ElWJs8b1mSkqqyIbZbMMfRwg> does not exist

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

  https://eclipsesource.com<https://lh6.googleusercontent.com/SE-pT_JPAXe2qHq8wBrdAgnUjmeQr7jhjvYKp493G-buLw1y8Rp5oJR_SdmIsNXCGgQ2g3AK0LYrSEpYwoErU5DvTHA-EDtBMkxwtfxpaUdnP57zJlDX4VtW8kukpOe5GQ> does not exist

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

https://eclipsesource.com<https://lh5.googleusercontent.com/FqV06_mYhACiAGdy0LcBV9MG_vG0viTE1_4uBXuaYxk2GAdCaCNNR0AZ34Enqti8g_LBSVb5rfQ6OtLpPNOf21kWZMJjhVWLI25_2j7zyDRC2EuE4SSVtjgrGiQK3r-mRQ> does not exist

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

https://eclipsesource.com<https://lh3.googleusercontent.com/Me2MiV4xB_d9qxPZ-iAqz9kDt0-kDTJauuBjXvHuQzzMGf3xDwICTDMn95u9Wt1m2EJvTSQQzIibtlusJ5qVUfz5SE612n4p0bJd-Bsgr0Qqi18CyMVviZqZsP4popl1Mw> does not exist

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:

  https://eclipsesource.com<https://lh3.googleusercontent.com/qUoo6EZAKxfkNRALDMif6NKwfxu0UxJ0udEczNmM5eQWhTxkBCajscObN1fxXruLpSb8roes9jTck3AEKPVa5eS9bprH5syidYoYwAt7J0_EEH0u1UNaUwrh_X1K7PbJFw> does not exist The complete DataType definition looks like this:

https://eclipsesource.com<https://lh5.googleusercontent.com/BEu5OoFMw5zOi30ztBwtZ0esgUXCgzSi_nTYLYGa5YnjVwxiAYa6RHeJf_V0trl0vJ9g1vsRAzIk9MIgDE4d2aZt-KpO5wR1MQVwnOz4jegND0xCPKjgLxde7VAj2WebQg> does not exist

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

https://eclipsesource.com<https://lh6.googleusercontent.com/CY6e4VaaxxfdjlcPM418suPeUXJ-xU6PPXgstRtXtQFyGCb4xsbyDJQxmQraaaCBxPBwVNyrJWnTSLF-T0bodXEHVhjUY8x1BZ3STm5SM1nfmYcQLCTlI0Le-DEAsLbS4A> does not exist

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:

https://eclipsesource.com<https://lh4.googleusercontent.com/NCAqzleMBHqNzRI00U_Lj1be5h6QKqpGpxjMGEtpeR_LA9G8ogUyrbXNqgnVKnTVQ_Cip-xVekRugC7YHRaaQ1NIEW1dvCNUdTz-YgeTzl21ZNGIwNJ7vMErei1p5TCOTg> does not exist

You can find the complete list of possible keys in the documentation. 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, Maximilian & Philip

Jonas Helming, Maximilian Koegel and Philip Langer co-lead EclipseSource. They work as consultants and software engineers for building web-based and desktop-based tools. …