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

Constraints - 图2

Constraints - 图3

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 - 图4

Constraints - 图5

Constraints - 图6

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 - 图7

Constraints - 图8

Constraints - 图9

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 - 图10

Constraints - 图11

Constraints - 图12

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 - 图13

Constraints - 图14

Constraints - 图15

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 - 图16

Constraints - 图17

Constraints - 图18

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 - 图19

Constraints - 图20

Constraints - 图21

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 - 图22

Constraints - 图23

Constraints - 图24

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 - 图25

Constraints - 图26

Constraints - 图27

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 - 图28

Constraints - 图29

Constraints - 图30

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

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;
  4. # Each combination of x and y must be unique.
  5. constraint exclusive on ( (.x, .y) );
  6. }

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