Skip to main content

Tests

To assert or validate the outcome of your input data, tests can be used. You can provide an array of tests to validate different input values. Each test can can contain one or many functions to validate the targetted value.

Usage

interfaces:
user/register:
output: http
method: POST
show_error_detail: true
actions:
- name: Input
input: a|body
assert:
error_message: "Password not strong enough"
tests:
- value: first
is_not_null: true
is_greater_than: 2
is_less_than: 20
regex: \S+
- value: last
is_not_null: true
is_greater_than: 3
is_less_than: 20
regex: \S+
- value: email
is_not_null: true
is_greater_than: 3
is_less_than: 100
regex: \S+
- value: pass
is_not_null: true
is_greater_than: 9
is_less_than: 30
- value: pass
regex: "[A-Z]"
error_message: "Password should contain one uppercase character"
- value: pass
regex: "[a-z]"
error_message: "Password should contain one lowercase character"
- value: pass
regex: "[0-9!@#]"
error_message: "Password should contain at least one number (0-9) or symbol (!,@,#)"

Tests should follow the above format. All tests should start off with the value you want to test followed by the functions you will use to test the value.

Value

The value field is to access the data from your defined action input.

Take for example the below JSON data is sent to an API interface.

{
"first": "tom",
"last": "bob"
}

To access the this input we would use a|body

Usage

actions:
- name: Input
input: a|body
assert:
error_message: "First name not long enough"
tests:
- value: first

You can access any values that are available in the body, including nested values using JavaScript like syntax. data[0].name.first

Name & Description

Optionally attach a name and/or description to each of your tests

Usage

name: assert_example
version: 0.0.1
description: Assert Example
metrics_enabled: true

interfaces:
comp:
output: http
route: details
method: POST

actions:
- name: CheckBody
input: a|body
assert:
tests:
- value: firstname
name: First Name Check
description: checks if the first name is equal to tom
is_equal_to: tom

JQ

NameDescriptionExamples
jqused for parsing JSON data
  • .id
  • [0].id

Usage

JQ Example

Consider the following payload

{
"firstname": "tom",
"isNull": null,
"num": 10,
"info": {
"thing1": "something",
"thing2": "something2"
}
"infoArr": [
{"name": "bob"}, {"name": "tomcat"}
]
}
name: jq_example
version: 0.0.1
description: JQ Example
metrics_enabled: true

interfaces:
JQ:
output: http
route: details
method: POST

actions:
- name: CheckBody
input: a|body
assert:
tests:
- value: .info.thing1
is_equal_to: something
- value: .infoArr[0].name
is_equal_to: bob

Comparison Functions

NameDescriptionExamples
is_equal_toChecks if two values are equal
  • 1 == 1 Pass
  • 0 == 1 Fail
is_not_equal_tochecks if two values are not equal
  • 1 != 1 Fail
  • 0 != 1 Pass
is_less_thanchecks if one value is less than another
  • 1 < 2 Pass
  • 2 < 1 Fail
is_less_than_or_equal_tochecks if one value is less than or equal to another value
  • 1 <= 1 Pass
  • 2 <= 1 Fail
is_greater_thanchecks if one value is greater than another
  • 1 > 0 Pass
  • 2 > 3 Fail
is_greater_than_or_equal_tochecks if one value is greater than or equal to another
  • 1 >= 1 Pass
  • 2 >= 3 Fail
is_not_nullchecks if one value is not null
  • value != null Pass
  • value == null Fail
is_nullchecks if one value is null
  • value == null PASS
  • value != null Fail
is_not_emptychecks if one value is not empty
  • value exists PASS
  • value does not exist Fail
is_uuidchecks if one value is a uuid
  • value == uuid PASS
  • value != uuid Fail
is_valid_jwtchecks if one value is a valid jwt
contains_valuechecks if one value contains another value
not_contains_valuechecks if one value does not contain another value
starts_withchecks if one value starts with a value
ends_withcheck of one value ends with a value

Examples

Comparison Examples

{
"firstname": "tom",
"isNull": null,
"num": 10,
"JWT": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJpc3MiOiJhaXJwaXBlIiwiZXhwIjoxNzEzMTAyODcwfQ.YnECqEgzBGnyHNyGaAHF4-OVsr7ROvZqsqQDkhzdlAM"
"uuid": "bb0f25dd-00e3-45fb-851f-b897c4aa5418"
}
name: comparison_example
version: 0.0.1
description: Comparison Example
metrics_enabled: true

interfaces:
comp:
output: http
route: details
method: POST

actions:
- name: CheckBody
input: a|body
assert:
tests:
- value: firstname
is_equal_to: tom
is_not_equal_to: bob
is_not_null: true
is_not_empty: true
contains_value: to
not_contains_value: v
starts_with: t
ends_with: m
- value: num
is_greater_than: 5
is_greater_than_or_equal_to: 10
- value: num
is_less_than: 20
is_less_than_or_equal_to: 10
- value: uuid
is_uuid: true
- value: isNull
is_null: true
- value: JWT
is_valid_jwt: airpipe

This will result in the below output when the API is called.

{
"data": {
"CheckBody": {
"data": {
"JWT": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJpc3MiOiJhaXJwaXBlIiwiZXhwIjoxNzEzMTAyODcwfQ.YnECqEgzBGnyHNyGaAHF4-OVsr7ROvZqsqQDkhzdlAM",
"firstname": "tom",
"isNull": null,
"jwt_claims": {
"data": null,
"exp": 1713102870,
"iss": "airpipe"
},
"num": 10,
"uuid": "bb0f25dd-00e3-45fb-851f-b897c4aa5418"
},
"message": "success",
"time.ms": 0
}
},
"metrics": {
"comparision_example.config.ms": 0
}
}

In the event of failures.

{
"data": {
"CheckBody": {
"data": {
"JWT": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJpc3MiOiJhaXJwaXBlIiwiZXhwIjoxNzEzMTAyODcwfQ.YnECqEgzBGnyHNyGaAHF4-OVsr7ROvZqsqQDkhzdlAM",
"firstname": "bob",
"isNull": "s",
"num": 10,
"uuid": "bb0f-00e3-45fb-851f-b897c4aa5418"
},
"errors": [
"firstname::is_equal_to: expected tom, got bob",
"firstname::contains_value: expected target bob does not contain to",
"t::starts_with: expected target bob to end with t",
"m::ends_with: expected target bob to end with m",
"isNull::is_null: expected to be null",
"JWT::is_valid_jwt: ExpiredSignature",
"uuid::is_uuid: expected to be a uuid"
],
"time.ms": 0
}
},
"metrics": {
"comparison_example.config.ms": 0
}
}

Regex

NameDescriptionExamples
regexutilises Rust's regular expression crate
  • "[A-Z]"
  • "[0-9!@#]"

Usage

Regex Example

Consider the following payload

{
"pass": "tom123!@#TOM",
}
name: regex_example
version: 0.0.1
description: Regex Example
metrics_enabled: true

interfaces:
REG:
output: http
route: details
method: POST

actions:
- name: CheckBody
input: a|body
assert:
tests:
- value: pass
is_not_null: true
is_greater_than: 9
is_less_than: 30
- value: pass
regex: "[A-Z]"
error_message: "Password should contain one uppercase character"
- value: pass
regex: "[a-z]"
error_message: "Password should contain one lowercase character"
- value: pass
regex: "[0-9!@#]"
error_message: "Password should contain at least one number (0-9) or symbol (!,@,#)"

You should see a similar output if the regex does not match

{
"data": {
"CheckBody": {
"data": {
"pass": "testing"
},
"errors": [
"pass::regex: Password should contain one uppercase character",
"pass::is_greater_than: expected target testing not greater than 9",
"pass::regex: Password should contain at least one number (0-9) or symbol (!,@,#)"
],
"time.ms": 0
}
},
"metrics": {
"regex_example.config.ms": 0
}
}

Bcrypt Verify

NameDescriptionExamples
bcryptverify bcrypt valuesbcrypt_verify: <hash>

Usage

Bcrypt Verify Example

Consider the following payload

{
"pass": "airpipe",
}
name: bcrypt_verifyexample
version: 0.0.1
description: Bcrypt Verify Example
metrics_enabled: true

interfaces:
BV:
output: http
route: details
method: POST

actions:
- name: CheckBody
input: a|body
assert:
tests:
- value: pass
is_not_null: true
bcrypt_verify: $2y$10$ThgA0htLEcaJwOyLx33HZ.DdNolY6pjnXTtoG4fDVswXqepbSL9yu

If the verify is successful

{
"data": {
"CheckBody": {
"data": {
"pass": "airpipe"
},
"message": "success",
"time.ms": 44
}
},
"metrics": {
"regex_example.config.ms": 44
}
}

If the verify is unsuccessful

{
"data": {
"CheckBody": {
"data": {
"pass": "airpi"
},
"errors": ["pass::bcrypt_verify: false"],
"time.ms": 44
}
},
"metrics": {
"regex_example.config.ms": 44
}
}

Success & Error Message

Optionally define success and/or error messages for each of your tests.

name: assert_example
version: 0.0.1
description: Assert Example
metrics_enabled: true

interfaces:
comp:
output: http
route: details
method: POST

actions:
- name: CheckBody
input: a|body
assert:
tests:
- value: firstname
success_message: success message
error_message: error message
is_equal_to: tom

HTTP Code On Error

NameDescriptionExamples
http_code_on_errordefine the http code to use when a test fails404, 401, 403, 500

Usage

name: jq_example
version: 0.0.1
description: JQ Example
metrics_enabled: true

interfaces:
JQ:
output: http
route: details
method: POST

actions:
- name: CheckBody
input: a|body
assert:
tests:
- value: firstname
is_equal_to: tom
http_code_on_error: 401

Hide Errors

NameDescriptionExamples
hide_errorshide the errors from the responsehide_errors: true
warning

Experimental Functions

Expression

Result Key

Pointer