Rules files

A rules file defines how to validate notices. It starts with declarations that apply to the whole file, and continues with validation stages, which contain the rules. This page describes the structure of a rules file, and the include directive, which builds a rules file from several files. For the parts of a rules file, see API endpoint declaration, Dynamic function declaration, Global variable and Validation stage.

Rules file

A rules file is a text file of EFX validation rules, grouped in validation stages. It defines how to validate notices.

Syntax
<include-directive> …
<api-endpoint-declaration> …
<global-declaration> …
<validation-stage> …

Each line of the syntax stands for any number of parts of the same kind. The parts must come in this order: include directives, then API endpoint declarations, then dynamic function declarations and global variables, then validation stages.

<include-directive>

optional. Include directives that insert content from other files, usually shared declarations (see Include directive).

<api-endpoint-declaration>

optional. Declarations that name the external services used by dynamic functions (see API endpoint declaration).

<global-declaration>

optional. Dynamic function declarations and global variables, in any order (see Dynamic function declaration and Global variable).

<validation-stage>

one or more validation stages (see Validation stage). You can also write include directives between the validation stages and after the last one.

A rules file has two parts. The first part contains the declarations, and you can use everything declared there in every validation stage of the file. The second part contains the validation stages. Each validation stage starts with a stage header and contains rule-sets. Each rule-set sets a context and contains rules (see Rule-set and Rule).

The validation of a notice against a rules file works as follows:

  1. The validation goes through the validation stages in the order in which they appear in the file.

  2. In each stage, it evaluates every rule-set for each occurrence that the context of the rule-set matches in the notice.

  3. For that occurrence, it checks each rule of the rule-set whose IN clause includes the subtype of the notice, and whose SCOPE clause, if there is one, allows the kind of validation in progress.

  4. Each rule that finds a problem adds a finding to the validation report.

You can write comments and blank lines almost anywhere in a rules file (see Comments). The only exception is in the OTHERWISE clause. Indentation and line breaks have no meaning in a rules file. By convention, you write each stage header on a line of its own. An include directive must be on a line of its own. In the SDK, rules files have the extension .efx.

Restrictions

  • A rules file must contain at least one validation stage, either directly or through an include directive. A file that another rules file only includes can contain declarations only, without a validation stage.

  • Declarations cannot appear inside or between validation stages. The only declarations allowed inside a validation stage are stage variables, at its start (see Stage variable).

  • Rules files cannot declare user-defined functions, dictionaries or callable templates. These exist in template files only.

Examples

// Declarations used by all stages
LET number:$max-number = 1000;

---- STAGE 1a ----

WITH ND-Root
  ASSERT BT-00-Text is present
    AS ERROR R-K7P-M2Q
    FOR BT-00-Text
    IN *

---- STAGE 2a ----

WITH ND-Root
  ASSERT BT-00-Number <= $max-number
    AS WARNING R-X3F-N8W
    FOR BT-00-Number
    IN 1-5, E1

A rules file with a global variable and two validation stages. Stage 1a checks that a field is present. Stage 2a checks the value of another field, for some notice subtypes only.

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

LET dynamic:?is-known-organisation(text:$id) CALL API;
LET text*:$accepted-codes = ['code1', 'code2', 'code3'];

---- STAGE 3a ----

WITH ND-Root
  ASSERT BT-00-Code in $accepted-codes
    AS ERROR R-Y2N-G7S
    FOR BT-00-Code
    IN *

---- STAGE 5 ----

WITH ND-Root
  ASSERT ?is-known-organisation(BT-00-Identifier)
    AS ERROR R-D4K-P9M
    FOR BT-00-Identifier
    IN *

A rules file that uses all kinds of declarations: an API endpoint, a dynamic function that calls it, and a global variable.

In the SDK, the rules files are in the validation folder. The main file, rules.efx, contains most of the validation stages, and inserts the others with include directives, such as #include "stage-1a.efx" on its first line.

EFX-1 had no rules files. The SDK provided the validation rules as Schematron files. For validation, the SDK used EFX-1 expressions only in the field metadata, as the conditions and assertions of constraints.

See also: Template file

Include directive

An include directive inserts the content of another file into a rules file. You use it to divide a large set of rules into several files, for example one file for each validation stage, and to share declarations between rules files.

Syntax
#include "<path>"
#include "<path>";
<path>

the path of the file to include, between double quotes. The path starts from the folder of the main rules file, that is, the file that includes the others. This is also the case when the include directive is itself in an included file.

You can end the directive with a semicolon, but you do not have to.

An include directive takes a line of its own. You can write spaces before it, and a comment after it on the same line. You write the keyword #include in lower case.

Before the translator reads a rules file, it replaces each include directive with the whole content of the file that the directive names. Included files can contain include directives in turn, and the translator replaces them in the same way. The text that results from all the replacements must form a valid rules file.

Because the translator replaces the directive with the content of the file before it reads the rules file, you can put the directive wherever that content is valid. You normally use include directives in two places:

  • at the start of the rules file, before the API endpoint declarations, to insert shared declarations;

  • among the validation stages, to insert one or more complete validation stages.

Restrictions

  • The directive must be the only content of its line, apart from spaces before it and a comment after it.

  • You must write the path between double quotes. Single quotes are not accepted.

  • The file must exist. The path cannot lead outside the folder of the main rules file.

  • A file cannot include itself, directly or through other included files.

Examples

#include "declarations.efx"

---- STAGE 1a ----

WITH ND-Root
  ASSERT BT-00-Text is present
    AS ERROR R-K7P-M2Q
    FOR BT-00-Text
    IN *

#include "stages/stage-2a.efx"   // stage 2a is kept in its own file

A rules file that inserts its declarations from declarations.efx and a validation stage from stages/stage-2a.efx. The file declarations.efx contains the declaration of the variable used in stage 2a:

LET number:$max-number = 1000;

The file stages/stage-2a.efx contains a complete validation stage:

---- STAGE 2a ----

WITH ND-Root
  ASSERT BT-00-Number <= $max-number
    AS WARNING R-X3F-N8W
    FOR BT-00-Number
    IN *;

If stages/stage-2a.efx included another file in the same folder, it would refer to it as stages/stage-2b.efx, because the path starts from the folder of the main rules file.

The include directive is new in EFX-2.