@regex annotation

The @regex annotation is used to specify a regex pattern for permitted values of attributes.

Syntax

Value annotation syntax
<type label> value <value type> @regex("<regular expression>");
Ownership annotation syntax
<type label> owns <attribute type label> @regex("<regular expression>");

The @regex annotation can be defined for either an attribute type’s value type or an ownership.

The @range annotation accepts a single string argument containing a regular expression.

Usage

The @regex annotation adds a constraint on values of attributes of a given attribute type to be valid strings according to the regular expression.

Unresolved include directive in modules/ROOT/pages/annotations/regex.adoc - include::3.x@reference::typeql/annotations/values.adoc[]

#!test[schema]
define
  attribute email value string @regex("^.*@\w+\.\w+$");

Unresolved include directive in modules/ROOT/pages/annotations/regex.adoc - include::3.x@reference::typeql/annotations/values.adoc[] In the following example, an instance of the phone type can contain any string value, but a user can have (has) only the phone s with the suitable values.

#!test[schema]
define
  entity user owns phone @regex("^\d{10,15}$");
  attribute phone value string;

Subtyping

The regex constraint put on an attribute types' value type is inherited. Unresolved include directive in modules/ROOT/pages/annotations/regex.adoc - include::3.x@reference::typeql/annotations/values.adoc[]

A subtype can introduce another regex constraint to combine it with the inherited one and create a subset of allowed values based on the intersection of the specified regular expressions.

Subtyping for ownerships

The regex constraint put on an ownership should be respected by all the sub attribute types owned by this owner. This way, any instance of any subtype of the phone owned by any instance of any subtype of the user should comply to the defined regex.

Ownerships of these subtypes can have their own regex constraints, and they will be respected similarly down the inheritance line.

Limitations

Is only compatible with string value type.