Powerful Condition Checking for TypeScript
Evaluate complex conditions against objects with ease. A flexible, zero-dependency TypeScript library for all your validation and condition checking needs.
"keyword">import { checkConditions } "keyword">from '@timkit/conditions';
"keyword">const user = {
name: 'John Doe',
age: 30,
email: 'john@example.com',
active: true
};
// Simple condition
checkConditions(user, ['age >= 18']); // true
// Complex conditions with AND/OR logic
checkConditions(user, [
['active == true', 'age > 25']
]); // true
// String validation
checkConditions(user, ['email isEmail']); // true
Get Started in Seconds
Install and start using powerful condition checking in your TypeScript projects
Installation
npm install @timkit/conditions
# or
yarn add @timkit/conditions
Zero dependencies means a lightweight addition to your project with maximum compatibility.
Basic Usage
"keyword">import { checkConditions } "keyword">from '@timkit/conditions';
"keyword">const data = { name: 'Alice', age: 25 };
// Simple equality check
checkConditions(data, ['name == Alice']); // true
// Comparison operators
checkConditions(data, ['age >= 18']); // true
Clean, intuitive API that works with any JavaScript object structure.
Comprehensive Condition Checking
From simple equality checks to complex validation logic - handle all your condition checking needs
Multiple Operators
Support for comparison, string, validation, list, and presence operators with intuitive syntax.
Complex Logic
Build complex conditions with AND/OR logic, nested conditions, and negation support.
Nested Properties
Access and validate nested object properties with dot notation (e.g., address.city).
Built-in Validation
Email, URL, number, and date validation built-in with isEmail, isUrl, isNumber, and more.
Type Safe
Full TypeScript support with proper type inference and compile-time safety.
Lightweight
Zero dependencies and minimal bundle size impact on your applications.
Operator Reference
Complete guide to all supported operators and their usage
Comparison Operators
Equality & Comparison
// Equality checks
checkConditions(user, ['age == 30']); // equal
checkConditions(user, ['status = active']); // equal
// Numerical comparisons
checkConditions(user, ['age > 18']); // greater than
checkConditions(user, ['age < 65']); // less than
checkConditions(user, ['score >= 80']); // greater or equal
checkConditions(user, ['rating <= 5']); // less or equal
String Operators
String Matching
// String operations
checkConditions(user, ['name contains John']);
checkConditions(user, ['email startsWith admin']);
checkConditions(user, ['domain endsWith .com']);
checkConditions(user, ['phone matches ^\+1']);
// List membership
checkConditions(user, ['role in admin,manager']);
Validation Operators
Built-in Validators
// Email validation
checkConditions(user, ['email isEmail']);
// URL validation
checkConditions(user, ['website isUrl']);
// Number validation
checkConditions(user, ['age isNumber']);
// Date validation
checkConditions(user, ['birthdate isDate']);
Presence Operators
Field Validation
// Check "keyword">if field is blank/empty
checkConditions(user, ['description isBlank']);
// Check "keyword">if field is required/present
checkConditions(user, ['name isRequired']);
// Negation support
checkConditions(user, ['email !isBlank']);
checkConditions(user, ['age !isRequired']);
Complete Operator Reference
All supported operators with descriptions and examples
Primary Operators
Operator | Description | Example |
---|---|---|
= | Equality | 'age = 30' |
== | Equality | 'age == 30' |
> | Greater than | 'age > 25' |
< | Less than | 'age < 35' |
>= | Greater than or equal | 'age >= 30' |
<= | Less than or equal | 'age <= 30' |
contains | String contains or array includes | 'name contains John' |
matches | String matches regex pattern | 'email matches .*@example\\.com' |
in | Value is in list | 'age in (25, 30, 35)' |
startsWith | String starts with substring | 'name startsWith John' |
endsWith | String ends with substring | 'email endsWith example.com' |
isBetween | Number is between min and max (inclusive) | 'age isBetween 25, 30' |
Validation Operators
Operator | Description | Example |
---|---|---|
isBlank | Field is empty, null, or undefined | 'address.zipcode isBlank' |
isRequired | Field is not empty, null, or undefined | 'name isRequired' |
isEmail | Field is a valid email address | 'email isEmail' |
isUrl | Field is a valid URL | 'website isUrl' |
isBool | Field is a boolean | 'active isBool' |
isNumber | Field is a number | 'age isNumber' |
isInteger | Field is an integer | 'count isInteger' |
isFloat | Field is a floating-point number | 'price isFloat' |
isDate | Field is a valid date | 'birthdate isDate' |
isTime | Field is a valid time (HH:MM, HH:MM:SS) | 'meetingTime isTime' |
isDateTime | Field is a valid date and time | 'createdAt isDateTime' |
isArray | Field is an array | 'tags isArray' |
isObject | Field is an object | 'address isObject' |
is | Combined validation (see below) | 'age is required|number|min:18' |
!operator | Negation of most operators | 'name !contains Smith' |
Combined Validation Rules
Use with the is
operator, separated by pipes (|
)
Validation | Description | Example |
---|---|---|
required | Field is not empty, null, or undefined | required |
string | Field is a string | string |
number | Field is a number | number |
min:value | Number is greater than or equal to value | min:18 |
max:value | Number is less than or equal to value | max:100 |
minLength:value | String or array has minimum length | minLength:5 |
maxLength:value | String or array has maximum length | maxLength:20 |
pattern:regex | String matches regex pattern | pattern:^[a-z0-9_]+$ |
startsWith:value | String starts with substring | startsWith:http |
endsWith:value | String ends with substring | endsWith:.com |
contains:value | String contains substring | contains:example |
Logic Operations
Combine conditions with AND/OR logic for complex validation scenarios
OR Logic (Default)
Top-level conditions are combined with OR logic by default.
// name == John OR age == 30
['name == John', 'age == 30']
AND Logic
Nest conditions in an array for AND logic.
// name == John AND age == 30
[['name == John', 'age == 30']]
Complex Logic
Combine AND and OR for sophisticated conditions.
// (A AND B) OR(C AND D)
[
['name == John', 'age == 30'],
['email contains example', 'active == true']
]
Advanced Usage Examples
Real-world examples showing the power and flexibility of complex condition checking
Complex Logic & Nested Conditions
"keyword">const user = {
name: 'Jane Smith',
age: 28,
email: 'jane@company.com',
address: {
city: 'New York',
country: 'USA'
},
roles: ['user', 'admin'],
active: true
};
// AND logic - all conditions must be true
"keyword">const isValidAdmin = checkConditions(user, [
['active == true', 'age >= 18', 'email isEmail']
]);
// OR logic - at least one condition must be true
"keyword">const hasAccess = checkConditions(user, [
'roles contains admin',
'roles contains manager'
]);
// Nested property access
"keyword">const isUSUser = checkConditions(user, [
'address.country == USA'
]);
// Complex combined validation
"keyword">const canCreateProject = checkConditions(user, [
['active == true', 'age >= 21'],
['roles contains admin', 'roles contains manager'],
'address.country in USA,Canada,UK'
]);
Real-World Scenarios
User Access Control
// Check "keyword">if user can access admin panel
"keyword">const canAccessAdmin = checkConditions(user, [
['active == true', 'email isEmail'],
'roles contains admin',
'lastLogin !isBlank'
]);
Form Validation
// Validate registration form
"keyword">const isValidRegistration = checkConditions(form, [
['name !isBlank', 'name.length >= 2'],
'email isEmail',
['password !isBlank', 'password.length >= 8'],
'age >= 13'
]);
Content Filtering
// Filter content based on criteria
"keyword">const isRelevantContent = checkConditions(content, [
['status == published', 'featured == true'],
'category in tech,programming,tutorial',
'publishDate >= 2023-01-01'
]);
Perfect for Every TypeScript Project
From simple validation to complex business logic - @timkit/conditions adapts to your needs
Form Validation
Validate user input with complex rules and business logic without writing verbose validation code.
Access Control
Implement role-based access control and permission systems with readable condition syntax.
Data Filtering
Filter arrays and collections based on complex criteria with intuitive condition expressions.
Business Rules
Encode complex business logic and rules in a readable format that non-developers can understand.
API Validation
Validate API requests and responses with flexible conditions that adapt to changing requirements.
Workflow Automation
Create automated workflows with conditional logic that executes based on dynamic data states.
Ready to Simplify Your Condition Logic?
Join developers using @timkit/conditions to write cleaner, more maintainable validation and condition checking code.
npm install @timkit/conditions
Pro Tip: Start with simple equality checks and gradually build up to complex nested conditions.
Questions? Check out the package documentation on NPM.