Constraints
This section describes the SDL declarations pertaining to constraints.
Examples
Declare an abstract constraint:
abstract constraint min_value(min: anytype) {
errmessage :=
'Minimum allowed value for {__subject__} is {min}.';
using (__subject__ >= min);
}
Declare a concrete constraint on an integer type:
scalar type posint64 extending int64 {
constraint min_value(0);
}
Declare a concrete constraint on an object type:
type Vector {
required property x -> float64;
required property y -> float64;
constraint expression on (
__subject__.x^2 + __subject__.y^2 < 25
);
}
Syntax
Define a constraint corresponding to the more explicit DDL commands.
[{abstract | delegated}] constraint name [ ( [argspec] [, ...] ) ]
[ on ( subject-expr ) ]
[ extending base [, ...] ]
"{"
[ using constr-expression ; ]
[ errmessage := error-message ; ]
[ annotation-declarations ]
[ ... ]
"}" ;
where argspec is:
[ argname: ] {argtype | argvalue}
Description
This declaration defines a new constraint with the following options:
abstract
If specified, the constraint will be abstract.
delegated
If specified, the constraint is defined as delegated, which means that it will not be enforced on the type it’s declared on, and the enforcement will be delegated to the subtypes of this type. This is particularly useful for exclusive constraints in abstract types. This is only valid for concrete constraints.
name
The name (optionally module-qualified) of the new constraint.
argspec
An optional list of constraint arguments.
For an abstract constraint argname optionally specifies the argument name and argtype specifies the argument type.
For a concrete constraint argname optionally specifies the argument name and argvalue specifies the argument value. The argument value specification must match the parameter declaration of the abstract constraint.
on ( subject-expr )
An optional expression defining the subject of the constraint. If not specified, the subject is the value of the schema item on which the concrete constraint is defined. The expression must refer to the original subject of the constraint as __subject__
. Note also that <subject-expr>
itself has to be parenthesized.
extending base [, …]
If specified, declares the parent constraints for this abstract constraint.
The valid SDL sub-declarations are listed below:
using constr_expression
A boolean expression that returns true
for valid data and false
for invalid data. The expression may refer to the subject of the constraint as __subject__
. This declaration is only valid for abstract constraints.
errmessage := error_message
An optional string literal defining the error message template that is raised when the constraint is violated. The template is a formatted string that may refer to constraint context variables in curly braces. The template may refer to the following:
$argname
– the value of the specified constraint argument__subject__
– the value of thetitle
annotation of the scalar type, property or link on which the constraint is defined.
annotation-declarations
Set constraint annotation to a given value.
︙
See also |