# assert Action

Used in [FLAT tests](https://sevenval.gitbook.io/flat/reference/testing) to assert expected results.

## Syntax

```markup
<assert>
[
  [ "$status", 200, "status is 200" ]
]
</assert>
```

The body is a JSON array of assertions. An assertion is an array itself, with up to three values:

* Expression: *as string* (see [`eval`](https://sevenval.gitbook.io/flat/reference/actions/eval)); (required)
* Expected result: a literal value (string, number, boolean, or `null`) or an object with compare flags (see below); (optional, default: `true`)
* Message: String literal to be included in output of failed tests; (optional)

Note that the expression must be a *string*. You can use template syntax, but the resulting array of assertions must still contain a string as the expression parameter.

## Examples

```markup
<assert>
[
  [ "$foo", "bar", "…" ],
  [ "$request/headers/authorization", null, "No Authorization header is set" ],
  [ "$response", "OK" ],
  [ "json-parse($response)/user", "alice" ],
  [ "$status", 201, "got 201 - created" ],
  [ "true()", true, "for sure!" ]
]
</assert>
```

## Compare Flags

The expected value (second value) can be an object that defines one or more *compare flags*:

* `file`: read wanted text from a *golden file*
* `mode`: either `text` or `json`. `json` mode  validates JSON syntax in both expected and actual result; compares JSON in compact (un-pretty) formatting.
* `contains`: a string that must be contained in the expression result
* `pattern`: a regular expression pattern (with delimiters and optional modifiers) that the expression result must match.

```markup
<assert>
[
  [ "$response", {"file": "login.golden", "mode": "json"} ]
]
</assert>

<assert>
[
  [ "$s1", {"contains": "foo bar"} ],
  [ "$s2", {"pattern": "#^[a-z ]+$#i"} ]
]
</assert>
```

The [Testing Templates](https://sevenval.gitbook.io/flat/cookbook/test-templates) recipe provides a full example.

## See also

* [Testing Templates](https://sevenval.gitbook.io/flat/cookbook/test-templates) (cookbook)
* [Testing Upstream Requests](https://sevenval.gitbook.io/flat/cookbook/test-backend) (cookbook)
* [Testing](https://sevenval.gitbook.io/flat/reference/testing) (reference)
* [`json-parse()`](https://sevenval.gitbook.io/flat/reference/functions/json-parse)
* [`json-stringify()`](https://sevenval.gitbook.io/flat/reference/functions/json-stringify)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://sevenval.gitbook.io/flat/reference/actions/assert.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
