Conditionally select between values based on conditions. Supports equality checks, comparisons, contains, and regex matching.
A Yaak plugin that allows you to conditionally select between values based on various conditions. This is the Yaak equivalent of the insomnia-plugin-conditional-selector.
==, !=, >, <, >=, <=Select between two values based on a boolean condition:
${[ conditional('true', 'production-url', 'dev-url') ]}
Arguments:
condition: Boolean value or expression (true, false, or comparison)valueIfTrue: Value returned when condition is truevalueIfFalse: Value returned when condition is falseExamples:
// With environment variable
${[ conditional('{{use_production}}', 'https://api.prod.com', 'https://api.dev.com') ]}
// With comparison
${[ conditional('{{port}} > 8000', 'high-port', 'low-port') ]}
// Nested with other template functions
${[ conditional('{{env_name}} == prod', '{{prod_token}}', '{{dev_token}}') ]}
Compare two values for equality:
${[ conditional.equals('{{environment}}', 'production', 'prod-api-key', 'dev-api-key') ]}
Arguments:
value1: First value to comparevalue2: Second value to comparevalueIfTrue: Value returned when values are equalvalueIfFalse: Value returned when values are not equalExamples:
// Check environment
${[ conditional.equals('{{env}}', 'staging', 'https://staging.api.com', 'https://dev.api.com') ]}
// Check numeric values
${[ conditional.equals('{{version}}', '2', 'v2-endpoint', 'v1-endpoint') ]}
// Check boolean flags
${[ conditional.equals('{{debug}}', 'true', 'verbose', 'quiet') ]}
Check if a string contains a substring:
${[ conditional.contains('{{user_role}}', 'admin', 'admin-token', 'user-token') ]}
Arguments:
text: Text to search insubstring: Substring to search forvalueIfTrue: Value returned when substring is foundvalueIfFalse: Value returned when substring is not foundExamples:
// Check role permissions
${[ conditional.contains('{{roles}}', 'admin', '/admin/endpoint', '/user/endpoint') ]}
// Check feature flags
${[ conditional.contains('{{features}}', 'beta', 'beta-api-key', 'stable-api-key') ]}
// Check URL patterns
${[ conditional.contains('{{base_url}}', 'localhost', 'dev-settings', 'prod-settings') ]}
Match a value against a regular expression pattern:
${[ conditional.regex('{{email}}', '.*@company\\.com$', 'internal', 'external') ]}
Arguments:
text: Text to match againstpattern: Regular expression pattern (without delimiters)valueIfTrue: Value returned when pattern matchesvalueIfFalse: Value returned when pattern does not matchExamples:
// Validate email domain
${[ conditional.regex('{{email}}', '.*@company\\.com$', 'internal-api', 'external-api') ]}
// Check semantic versioning
${[ conditional.regex('{{version}}', '^[0-9]+\\.[0-9]+\\.[0-9]+$', 'valid', 'invalid') ]}
// Match IP addresses
${[ conditional.regex('{{ip}}', '^192\\.168\\.', 'local-network', 'external-network') ]}
// Check environment naming
${[ conditional.regex('{{env}}', '^(prod|production)$', 'production-mode', 'development-mode') ]}
You can nest conditional selectors for complex logic:
// Select API version based on multiple conditions
${[ conditional.equals(
'{{region}}',
'US',
conditional.equals('{{env}}', 'prod', 'us-prod-v2', 'us-dev-v2'),
conditional.equals('{{env}}', 'prod', 'eu-prod-v1', 'eu-dev-v1')
) ]}
// Choose auth method based on environment
${[ conditional.contains(
'{{auth_type}}',
'oauth',
'Bearer {{oauth_token}}',
'Basic {{basic_auth}}'
) ]}
// Content-Type based on API version
${[ conditional('{{api_version}} >= 2', 'application/vnd.api+json', 'application/json') ]}
// Different timeout for production
${[ conditional.equals('{{env}}', 'production', '30000', '5000') ]}
// Enable/disable features
${[ conditional.equals('{{feature_flags}}', 'new-endpoint', '/api/v2/users', '/api/v1/users') ]}
The conditional() function supports these comparison operators:
== or ===: Equal to!= or !==: Not equal to>: Greater than<: Less than>=: Greater than or equal to<=: Less than or equal toExamples:
${[ conditional('{{status_code}} >= 200', 'success', 'error') ]}
${[ conditional('{{retry_count}} < 3', 'retry', 'fail') ]}
${[ conditional('{{timeout}} != 0', 'custom', 'default') ]}
For the basic conditional() function, the following values are considered falsy:
false0noAll other values are considered truthy:
true1yesconditional.equals() over conditional() when checking equality for better clarity\\. for .\\ for backslashes in regex patterns${[ ... ]}Contributions are welcome! Please feel free to submit a Pull Request.
MIT
Inspired by insomnia-plugin-conditional-selector by Barry Sunderland.