Search
ctrl/
Ask AI
Light
Dark
System

Constraints

This section describes the SDL declarations pertaining to constraints.

Declare an abstract constraint:

Copy
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:

Copy
scalar type posint64 extending int64 {
    constraint min_value(0);
}

Declare a concrete constraint on an object type:

Copy
type Vector {
    required x: float64;
    required y: float64;
    constraint expression on (
        __subject__.x^2 + __subject__.y^2 < 25
    );
}

Define a constraint corresponding to the more explicit DDL commands.

[{abstract | delegated}] constraint name [ ( [argspec] [, ...] ) ]
    [ on ( subject-expr ) ]
    [ except ( except-expr ) ]
    [ extending base [, ...] ]
"{"
    [ using constr-expression ; ]
    [ errmessage := error-message ; ]
    [ annotation-declarations ]
    [ ... ]
"}" ;

where argspec is:

[ argname: ] {argtype | argvalue}

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__. The expression must be Immutable, but may refer to __subject__ and its properties and links.

Note also that <subject-expr> itself has to be parenthesized.

except ( exception-expr )
An optional expression defining a condition to create exceptions

to the constraint. If <exception-expr> evaluates to true, the constraint is ignored for the current subject. If it evaluates to false or {}, the constraint applies normally.

except may only be declared on object constraints, and is otherwise follows the same rules as on, above.

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 the title annotation of the scalar type, property or link on which the constraint is defined.

If the content of curly braces does not match any variables, the curly braces are emitted as-is. They can also be escaped by using double curly braces.

annotation-declarations

Set constraint annotation to a given value.