Declarations in rules files

The declarations at the start of a rules file apply to the whole file:

  • API endpoint declarations name the external services from which rules can receive information that is not in the notice.

  • Dynamic function declarations define functions whose result comes from such a service.

  • Global variables hold values that the rules of every validation stage can use.

The declarations come after the include directives at the start of the file, if there are any, and before the first validation stage. The API endpoint declarations come first, then the dynamic function declarations and global variables, in any order.

API endpoint declaration

An API endpoint declaration gives a name to an external service (an API) that dynamic functions call during the validation of a notice. It can also give the address of that service.

Syntax
ENDPOINT "<endpoint-name>";
ENDPOINT "<endpoint-name>" AT "<url>";
<endpoint-name>

the name of the endpoint, as a text literal. It starts with a letter, followed by letters, digits, hyphens (-) and underscores (_). default is the name of the endpoint that the dynamic functions call when they do not name an endpoint.

<url>

optional. The address of the service, as a text literal.

Some rules need information that is not in the notice under validation, for example whether an identifier refers to a known organisation or to an earlier notice. The validation receives such information from a service. An API endpoint declaration names that service, and dynamic functions refer to it by this name (see Dynamic function declaration).

The address given with AT is a default. The environment in which notices are validated can give another address for the endpoint, for example the address of a test service. When the declaration gives no address, the environment must give one. Otherwise, every call to the endpoint fails.

You can write the text literals between double quotes or single quotes (see Text literal). You write the keywords ENDPOINT and AT in upper case.

Restrictions

  • API endpoint declarations appear only at the start of the rules file: after the include directives at the start, if there are any, and before the dynamic function declarations and global variables. They cannot appear in a validation stage.

  • Two endpoints cannot have the same name.

Examples

ENDPOINT "default" AT "https://api.example.org/eforms";

Declares the default endpoint together with its address.

ENDPOINT "business-registers";

Declares an endpoint without an address. The environment must give the address when it validates notices.

EFX-1 had no API endpoints. Instead, its expressions could refer to fields of another notice, identified by its notice identifier (notice(…)). EFX-2 no longer has such references. Rules that need information from other notices use dynamic functions.

Dynamic function declaration

A dynamic function declaration declares a function whose result comes from a call to an API endpoint during the validation of a notice. With dynamic functions, rules can use information that is not in the notice, for example information about other notices or about organisations.

Syntax
LET dynamic:?<function-name>(<parameters>) CALL API;
LET dynamic:?<function-name>(<parameters>) CALL API "<endpoint-name>";
LET dynamic:?<function-name>(<parameters>) CALL API "<endpoint-name>" ON ERROR WARN "<label-identifier>";
LET dynamic:?<function-name>(<parameters>) CALL API "<endpoint-name>" ON ERROR REJECT "<label-identifier>";
<function-name>

the name of the function (see Identifiers). Calls to the function write the name after a question mark, as for user-defined functions.

<parameters>

optional. Parameter declarations separated by commas, each made of a type and a variable name, for example text:$id or text*:$ids. You write them as in a user-defined function declaration. You must write the parentheses, also when the function has no parameters.

<endpoint-name>

optional. The name of a declared API endpoint, as a text literal. Without it, the function calls the endpoint named default.

<label-identifier>

optional. The identifier of the label that gives the message reported when a call fails, as a text literal, for example "auxiliary|text|register-unavailable".

The ON ERROR part is optional. It can also follow CALL API directly when no endpoint name is given, and a comma can come before it, for example CALL API, ON ERROR WARN.

A call to a dynamic function sends the values of the arguments to the endpoint. The endpoint answers whether the condition that the function stands for is met. So the result of a dynamic function is an indicator. The translator checks the arguments of a call against the parameters, as for any function: there must be one argument for each parameter, of the declared type.

You can call a dynamic function in two places (see Dynamic function call):

  • in the expressions of a rule, that is, in its WHEN, ASSERT, REPORT or OTHERWISE clause;

  • as the value of a dynamic variable, LET dynamic:$<variable-name> = ?<function-name>(<arguments>), declared as a global variable, as a stage variable or in a WITH clause (see Dynamic variable). The variable holds the result of the call, and you can use it in rules like an indicator.

A call fails when the validation cannot reach the endpoint, when it knows no address for it, or when the response is not a valid result. The result of a failed call is neither true nor false. When the conditions of a rule depend on a failed call, directly or through variables, the validation does not check the rule for that occurrence. Instead, it reports a finding about the failed call. The ON ERROR part sets the severity of this finding:

  • REJECT: the finding has the severity ERROR, so the notice fails validation. This is the default when there is no ON ERROR part.

  • WARN: the finding has the severity WARNING, so the notice does not fail validation because of it.

The label identifier gives the message of the finding. Without a label identifier, the message is the label rule|text|api-error for REJECT, and rule|text|api-warning for WARN.

The validation can check rules that depend on dynamic functions, directly or through variables, only when it can reach the API endpoints. So a validation can run with these rules or without them.

You write the keywords LET, CALL, API, ON, ERROR, WARN and REJECT in upper case, and dynamic in lower case.

Restrictions

  • Dynamic function declarations appear only at the start of the rules file, after the API endpoint declarations and before the first validation stage. They cannot appear in a validation stage.

  • You must declare the endpoint. This also applies to the endpoint default, when no endpoint name is given.

  • You can call a dynamic function only in the clauses of a rule, or as the value of a dynamic variable. You cannot call it in the value of any other variable.

  • The value of a dynamic variable is exactly one call to a dynamic function. It cannot be a larger expression.

Examples

ENDPOINT "default" AT "https://api.example.org/eforms";
LET dynamic:?is-known-organisation(text:$id) CALL API;

A function with one text parameter, which calls the default endpoint. The validation reports a failed call as an error, with the default message.

ENDPOINT "business-registers";
LET dynamic:?is-registered-company(text:$id, text:$country) CALL API "business-registers" ON ERROR WARN "auxiliary|text|register-unavailable";

A function with two parameters, which calls the endpoint business-registers. The validation reports a failed call as a warning, with the message of the label auxiliary|text|register-unavailable.

---- STAGE 5 ----

WITH ND-Root
  ASSERT ?is-known-organisation(BT-00-Identifier)
    AS ERROR R-D4K-P9M
    FOR BT-00-Identifier
    IN *;
  WHEN BT-00-Indicator == TRUE
    ASSERT ?is-registered-company(BT-00-Identifier, BT-00-Code)
      AS WARNING R-F5V-T6B
      FOR BT-00-Identifier
      IN *;

Two rules that call the functions declared above. The first rule fails when the service answers that it does not know the organisation. If the call fails, the validation does not check the first rule, and reports an error about the failed call instead.

EFX-1 had no dynamic functions. Its expressions could instead refer to fields of another notice (notice(…)). EFX-2 no longer has such references.

Global variable

A global variable is a variable declared at the start of a rules file, before the first validation stage. Every rule of the file can use it.

Syntax
LET <type>:$<variable-name> = <expression>;
LET dynamic:$<variable-name> = ?<function-name>(<arguments>);
<type>

the data type of the variable: text, number, indicator, date, time or duration, followed by * for a sequence (see Variable declaration).

<variable-name>

the name of the variable (see Identifiers).

<expression>

an expression of the declared type, which gives the value of the variable.

<function-name>, <arguments>

a call to a dynamic function, for a dynamic variable (see Dynamic variable).

EFX computes a global variable once for each notice. Its expression has no context: EFX reads the field and node references in it from the root of the notice. So a reference to a field that can occur more than once gives the values of all its occurrences in the notice.

You can use a global variable in every validation stage of the rules file: in stage variables, in WITH clauses and in rules. You can also use it in the global variables that follow it. You can mix global variables and dynamic function declarations in any order, as long as you declare each name before you use it.

Restrictions

  • Global variables appear only at the start of the rules file, after the API endpoint declarations and before the first validation stage.

  • You cannot use the name of a global variable again for another global variable, for a stage variable or for a variable declared in a WITH clause.

Examples

LET number:$max-number = 1000;
LET text*:$accepted-codes = ['code1', 'code2', 'code3'];
LET date:$earliest-start = 2024-01-01Z;
LET number:$text-count = count(BT-13-Text);

Four global variables. The last one counts the occurrences of BT-13-Text in the whole notice.

---- STAGE 2a ----

WITH ND-Root
  ASSERT BT-00-Code in $accepted-codes
    AS ERROR R-Y2N-G7S
    FOR BT-00-Code
    IN *;
  ASSERT BT-00-StartDate >= $earliest-start
    AS WARNING R-W1D-J2Y
    FOR BT-00-StartDate
    IN *;

---- STAGE 2b ----

WITH ND-SubNode
  ASSERT $text-count <= $max-number
    AS ERROR R-M3C-U8N
    FOR BT-13-Text
    IN *

Rules in two validation stages that use the global variables declared above.

LET dynamic:$buyer-known = ?is-known-organisation(BT-00-Identifier);

A global dynamic variable, which holds the result of one call to the dynamic function declared in Dynamic function declaration.

EFX-1 had no rules files, so it had no global variables in them either.