Constraints
This section assumes a basic understanding of EdgeQL.
Constraints gives users fine-grained control over which data is considered valid. The can be defined on properties, links, object types, and custom scalars.
Built-in constraints
For convenience, EdgeDB provides some pre-defined constraints. Click the name of a given constraint for the full documentation.
Enforce uniqueness among all instances of the containing type | |
Custom constraint expression | |
A list of allowable values | |
Maximum value numerically/lexicographically | |
Maximum value numerically/lexicographically (exclusive range) | |
Maximum length (strings only) | |
Minimum value numerically/lexicographically | |
Minimum value numerically/lexicographically (exclusive range) | |
Minimum length (strings only) | |
Regex constraint (strings only) |
The expression
constraint is used to define custom constraint logic. Inside custom constraints, the keyword __subject__
can used to reference the value being constrained.
Constraints on properties
The constraint below uses the built-in len() function, which returns the length of a string.
type User {
required property username -> str {
# as custom constraint
constraint expression on (len(__subject__) < 25);
# with built-in
constraint min_len_value(25);
};
}
Constraints on object types
Constraints can be defined on object types. This is useful when the constraint logic must reference multiple links or properties.
Inside an object type declaration, you can omit __subject__
and simple refer to properties with the leading dot notation (e.g. .<name>
).
type ConstrainedVector {
required property x -> float64;
required property y -> float64;
constraint expression on (
.x ^ 2 + .y ^ 2 <= 25
);
}
Constraints on links
When defining a constraint on a link, __subject__
refers to the link itself. This is commonly used add constraints to link properties.
type User {
property name -> str;
multi link friends -> User {
single property strength -> float64;
constraint expression on (
__subject__@strength >= 0
);
}
}
Constraints on custom scalars
Custom scalar types can be constrained.
scalar type username extending str {
constraint regexp(r'^[A-Za-z0-9_]{4,20}$');
}
Note: you can’t use exclusive constraints on custom scalar types, as the concept of exclusivity is only defined in the context of a given object type.
Use expression constraints to declare custom constraints using arbitrary EdgeQL expressions. The example below uses the built-in str_trim() function.
scalar type title extending str {
constraint expression on (
__subject__ = str_trim(__subject__)
);
}
︙
See also |