Constraints​

exclusive Enforce uniqueness among all instances of the containing type
expression Custom constraint expression
one_of A list of allowable values
max_value Maximum value numerically/lexicographically
max_ex_value Maximum value numerically/lexicographically (exclusive range)
max_len_value Maximum length (strings only)
min_value Minimum value numerically/lexicographically
min_ex_value Minimum value numerically/lexicographically (exclusive range)
min_len_value Minimum length (strings only)
regexp Regex constraint (strings only)

constraint

std::expression

Constraints - 图1

std::expression on (expr)

Arbitrary constraint expression.

Example of using an expression constraint to create a custom scalar:


1. scalar type starts_with_a extending str {
2. constraint expression on (__subject__[0] = 'A');
3. }

Example of using an expression constraint based on a couple of object properties to restrict maximum magnitude for a vector:


1. type Vector {
2. required property x -> float64;
3. required property y -> float64;
4. constraint expression on (
5. __subject__.x^2 + __subject__.y^2 < 25
6. );
7. }

constraint

std::one_of

Constraints - 图2

std::one_of(variadic members: anytype)

Specifies the list of allowed values directly.

Example:


1. scalar type Status extending str {
2. constraint one_of ('Open', 'Closed', 'Merged');
3. }

constraint

std::max_value

Constraints - 图3

std::max_value(max: anytype)

Specifies the maximum value for the subject.

Example:


1. scalar type max_100 extending int64 {
2. constraint max_value(100);
3. }

constraint

std::max_ex_value

Constraints - 图4

std::max_ex_value(max: anytype)

Specifies the maximum value (as an open interval) for the subject.

Example:


1. scalar type maxex_100 extending int64 {
2. constraint max_ex_value(100);
3. }

constraint

std::max_len_value

Constraints - 图5

std::max_len_value(max: int64)

Specifies the maximum length of subject string representation.

Example:


1. scalar type Username extending str {
2. constraint max_len_value(30);
3. }

constraint

std::min_value

Constraints - 图6

std::min_value(min: anytype)

Specifies the minimum value for the subject.

Example:


1. scalar type non_negative extending int64 {
2. constraint min_value(0);
3. }

constraint

std::min_ex_value

Constraints - 图7

std::min_ex_value(min: anytype)

Specifies the minimum value (as an open interval) for the subject.

Example:


1. scalar type positive_float extending float64 {
2. constraint min_ex_value(0);
3. }

constraint

std::min_len_value

Constraints - 图8

std::min_len_value(min: int64)

Specifies the minimum length of subject string representation.

Example:


1. scalar type four_decimal_places extending int64 {
2. constraint min_len_value(4);
3. }

constraint

std::regexp

Constraints - 图9

std::regexp(pattern: str)

Specifies that the string representation of the subject must match a regexp.

Example:


1. scalar type LettersOnly extending str {
2. constraint regexp(r'[A-Za-z]*');
3. }

See here for more details on regexp patterns.

constraint

std::exclusive

Constraints - 图10

Specifies that the link or property value must be exclusive (unique).

When applied to a multi link or property, the exclusivity constraint guarantees that for every object, the set of values held by a link or property does not intersect with any other such set in any other object of this type.

This constraint is only valid for concrete links and properties. Scalar type definitions cannot include this constraint.

Example:


1. type User {
2. # Make sure user names are unique.
3. required property name -> str {
4. constraint exclusive;
5. }

7. # Make sure none of the "owned" items belong
8. # to any other user.
9. multi link owns -> Item {
10. constraint exclusive;
11. }
12. }

Sometimes it’s necessary to create a type where each combination of properties is unique. This can be achieved by defining an exclusive constraint for the type, rather than on each property:


1. type UniqueCoordinates {
2. required property x -> int64;
3. required property y -> int64;

5. # Each combination of x and y must be unique.
6. constraint exclusive on ( (.x, .y) );
7. }

In principle, many possible expressions can appear in the on (<expr>) clause of the exclusive constraint with a few caveats:

  • The expression can only contain references to the immediate properties or links of the type.
  • No backlinks or long paths are allowed.
  • Only Immutable functions are allowed in the constraint expression.

This constraint also has an additional effect of creating an implicit index on the link or property. This means that in the above example there’s no need to add explicit indexes for the name property.

See also
Schema > Constraints
SDL > Constraints
DDL > Constraints
Introspection > Constraints