Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Runtime Validation

When an HTTP request is made to the Cloesce Router, the incoming data will first be matched to an existing API implementation, and then validated against the respective API defined in the Cloesce Schema.

Each type is validated in accordance with the rules defined in the Type Reference. If any validation errors occur, a 400 Bad Request response will be returned with details about the validation errors.

In addition to this, several Validator Tags are also supported for more complex validation scenarios.

Overview

Validator Tags can be applied to any field (i.e. it follows the syntax field: type) in a Model, API parameter, or Data Source parameter.

A foreign key field will automatically inherit all validators from the field it references. For example:

model User {
    primary {
        [gt 0]
        id: int
    }
}

model Post {
    primary {
        id: int
    }

    foreign (User::id) {
        user_id
    }
}

In the above code, the user_id field in the Post Model will automatically have the [gt 0] validator applied to it, since it is a foreign key referencing the id field in the User Model.

Numerical Validators

These validators apply to the int and real types:

ValidatorDescription
[gt value]Value must be greater than value
[gte value]Value must be greater than or equal to value
[lt value]Value must be less than value
[lte value]Value must be less than or equal to value
[step value]Value must be a multiple of value (where value must be an integer)

String Validators

These validators apply to the string type:

ValidatorDescription
[len value]String length must be exactly value, where value is a non-negative integer
[minlen value]String length must be at least value, where value is a non-negative integer
[maxlen value]String length must be at most value, where value is a non-negative integer
[regex r]String must match the regular expression r

Cloesce uses Rust’s regex crate for regular expression validation and evaluation. A regex pattern is provided as a regex literal:

[regex /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/]
email: string