Skip to content

String Assertion Rules

String assertions first ensure the value is a string, then perform matching.

6.1 Shorthand

ExpressionDescription
==text / =textEquals
!=textNot equals
%=textEquals (case-insensitive)
%!textNot equals (case-insensitive)
~=/re/flags / ~/re/flagsRegex match
~!/re/flagsRegex not match
?=textContains
?!textDoes not contain
*=textContains (case-insensitive)
*!textDoes not contain (case-insensitive)
^=text / ^!textStarts with / does not start with
$=text / $!textEnds with / does not end with

6.2 Full Form

ExpressionEquivalent shorthand
:equal:text==text
:not-equal:text!=text
:equal-i:text%=text
:not-equal-i:text%!text
:match:/re/~=/re/
:not-match:/re/~!/re/
:include:text?=text
:not-include:text?!text
:include-i:text*=text
:not-include-i:text*!text
:start-with:text^=text
:not-start-with:text^!text
:start-with-i:textCase-insensitive prefix
:not-start-with-i:textCase-insensitive non-prefix
:end-with:text$=text
:not-end-with:text$!text
:end-with-i:textCase-insensitive suffix
:not-end-with-i:textCase-insensitive non-suffix

6.3 Examples

typescript
// Equality
compiler.compile({ rule: '==hello' });
// ✓ "hello"
// ✗ "Hello", "world"

// Case-insensitive equality
compiler.compile({ rule: '%=hello' });
// ✓ "hello", "HELLO", "HeLLo"
// ✗ "world"

// Regex match: email format
compiler.compile({ rule: '~=/^[^@]+@[^@]+\\.[^@]+$/' });
// ✓ "[email protected]"
// ✗ "invalid.email"

// Contains
compiler.compile({ rule: '?=@gmail' });
// ✓ "[email protected]", "[email protected]"
// ✗ "[email protected]"

// Prefix match: URL scheme
compiler.compile({ rule: '^=https://' });
// ✓ "https://example.com"
// ✗ "http://example.com"

// Suffix match: file extension
compiler.compile({ rule: '$=.json' });
// ✓ "config.json", "data.json"
// ✗ "config.yaml"

// Combined: API path validation
compiler.compile({
    rule: {
        method: ['$.enum', 'GET', 'POST', 'PUT', 'DELETE'],
        path: ['$.and', 'string', '^=/api/']
    }
});
// ✓ { method: "GET", path: "/api/users" }
// ✗ { method: "GET", path: "/admin/users" }