A condition in Alumio is an object that tests a single value. Conditions can be used in transformers to filter values or to conditionally transform values.
Example use of a condition in a filter
In the following example a condition is used in an entity filter to remove products that are not enabled.
{
"prototype": "data",
"parameters": {
"filters": [
{
"prototype": "value-condition",
"parameters": {
"accessor": {
"prototype": "key",
"parameters": {
"keys": [
"enabled"
]
}
},
"conditions": [
{
"prototype": "equals",
"parameters": {
"value": true
}
}
]
}
}
]
}
}
Input:
[
{
"sku": "tnt",
"enabled": true
},
{
"sku": "dynamite",
"enabled": false
}
]
Output:
[
{
"sku": "tnt",
"enabled": true
}
]
Combining conditions
Conditions can be combined using logical conditions. They can also be combined with a mapper using a mapped condition.
Logic: not
Code: logic-not
{
"prototype": "logic-not",
"parameters": {
"conditions": [
{
"prototype": "type-array"
}
]
}
}
> ["foo"] => false
> "foo" => true
Logic: and
Code: logic-and
{
"prototype": "logic-and",
"parameters": {
"conditions": [
{
"prototype": "format-email"
},
{
"prototype": "string-length-between",
"parameters": {
"min": 6
}
}
]
}
}
> "example@example.com" => true
> "x@x.com" => false
Logic: or
Code: logic-or
{
"prototype": "logic-or",
"parameters": {
"conditions": [
{
"prototype": "format-email"
},
{
"prototype": "format-url"
}
]
}
}
> "example@example.com" => true
> "https://example.com" => true
> "foo" => false
Mapped condition
Code: mapped-condition
A mapped condition applies a mapper on the value before the value is tested.
{
"prototype": "mapped-condition",
"parameters": {
"mappers": [
{
"prototype": "string-upper"
}
],
"conditions": [
{
"prototype": "equals",
"parameters": {
"value": "POST"
}
}
]
}
}
> "POST" => true
> "post" => true
> "get" => false
Available conditions
The following are available conditions.
For custom conditions, !! a new condition !! can be created.
Equals
Code: equals
Compare a value against a configured value. Both the type and the value have must match.
{
"prototype": "equals",
"parameters": {
"value": 10
}
}
> 10 => true
> 9 => false
> "10" => false
Not equals
Code: not-equals
Inverse of "Equals".
Equals adaptive
Code: equals-adaptive
Compare a value adaptively to a configured value. The type may differ.
{
"prototype": "equals-adaptive",
"parameters": {
"value": 10
}
}
> 10 => true
> 9 => false
> "10" => true
Not equals adaptive
Code: not-equals-adaptive
Inverse of "Equals adaptive".
One of list
Code: one-of
Compare a value to a list of values.
{
"prototype": "one-of",
"parameters": {
"value": [
10,
20
]
}
}
> 10 => true
> 20 => true
> 9 => false
Not one of list
Code: not-one-of
Inverse of "One of list"
Empty
Code: empty
Checks whether a value is empty.
> 0 => true
> 10 => false
> null => true
> [] => true
> [10] => false
Note that this condition usually does not check whether a value has been set. To be able to check the value it has to be retrieved from the entity.
Not empty
Code: not-empty
Inverse of "Empty".
Number: greater than
Code: number-greater-than
{
"prototype": "number-greater-than",
"parameters": {
"value": 10
}
}
> 11 => true
> 10 => false
Number: greater than or equals
Code: number-greater-than-or-equals
{
"prototype": "number-greater-than-or-equals",
"parameters": {
"value": 10
}
}
> 11 => true
> 10 => true
> 9 => false
Number: less than
Code: number-less-than
{
"prototype": "number-less-than",
"parameters": {
"value": 10
}
}
> 9 => true
> 10 => false
Number: less than or equals
Code: number-less-than-or-equals
{
"prototype": "number-less-than-or-equals",
"parameters": {
"value": 10
}
}
> 9 => true
> 10 => true
> 11 => false
List: contains
Code: list-contains
{
"prototype": "list-contains",
"parameters": {
"value": 10
}
}
> [8, 9, 10] => true
> [1, 2, 3] => false
> 2 => false
List: Does not contain
Code: list-not-contains
Inverse of "List: contains".
String: Matches fnmatch pattern
Code: string-match
Compares a string to an fnmatch pattern.
{
"prototype": "string-match",
"parameters": {
"value": "foo-*"
}
}
> "foo-bar" => true
> "bar" => false
String: Does not match fnmatch pattern
Code: string-not-match
Inverse of "String: Matches fnmatch pattern".
String: Matches PCRE expression
Code: string-regex
Compares a string to PCRE regular expression
{
"prototype": "string-regex",
"parameters": {
"value": "/foo-.*/"
}
}
> "foo-bar" => true
> "bar" => false
String: Does not match PCRE expression
Code: string-not-regex
Inverse of "String: Matches PCRE expression".
String: Contains only alphanumeric characters
Code: string-alnum
Checks whether a string only contains alphanumeric characters.
> "foo123" => true
> "foo@#$" => false
String: Contains only alphabetic characters
Code: string-alpha
Checks whether a string only contains alphabetic characters.
> "foo" => true
> "foo123" => false
String: Contains only numbers
Code: string-number
Checks whether a string contains only numbers.
> "123" => true
> "foo123" => false
String: Length equals
Code: string-length
Checks whether the length of a string equals a number.
{
"prototype": "string-length",
"parameters": {
"length": 3
}
}
> "foo" => true
> "foobar" => false
String: Length between
Code: string-length-between
Checks whether the length of a string is between two values.
{
"prototype": "string-length-between",
"parameters": {
"max": 6,
"min": 3
}
}
> "f" => false
> "foo" => true
> "foobar" => true
> "foobarbaz" => false
Format: Valid date time
Code: format-datetime
> "2019-01-01 00:00:00" => true
> "now" => true
> "foo" => false
Format: Valid credit card
Code: format-credit-card
> "4111111111111111" => true
> "foo" => false
Format: Valid email address
Code: format-email
> "example@example.com" => true
> "foo" => false
Format: Valid URL
Code: format-url
> "https://example.com/index.html" => true
> "foo" => false
Format: Valid unique identifier (UUID)
Code: format-uuid
> "6fc23076-8d38-4563-9cb2-1e91f6621666" => true
> "foo" => false
Format: Valid JSON
Code: format-json
> "{\"foo\": [\"bar\"]}" => true
> "\"foo\"" => true
> "false" => true
> "No JSON" => false
Type: boolean
Code: type-boolean
> true => true
> 10 => false
Type: string
Code: type-string
> "foo" => true
> 10 => false
Type: integer
Code: type-integer
> 10 => true
> "foo" => false
Type: float
Code: type-float
> 10.5 => true
> "foo" => false
Type: numeric
Code: type-numeric
> 10.5 => true
> 10 => true
> "10" => true
> "foo" => false
Type: array
Code: type-array
> ["foo"] => true
> "foo" => false