@synet/patterns
Version:
Robust, battle-tested collection of stable patterns used in Synet packages
152 lines (151 loc) • 5.09 kB
TypeScript
import { Result } from "./result";
/**
* Guard provides type-specific validation methods organized by data type.
*
* This design improves discoverability and organization of validation logic.
*
*
*/
export declare class Guard {
/**
* Common validation for any type
*/
static defined<T>(value: T, name: string): Result<T>;
/**
* Validates that a condition is true
*/
static assert(condition: boolean, message: string): Result<void>;
/**
* Combines multiple validation results into a single result
*/
static combine(results: Result<unknown>[]): Result<void>;
/**
* String-specific validations
*/
static String: {
/**
* Validates that a string is not empty
*/
nonEmpty(value: string, name: string): Result<string>;
/**
* Validates that a string matches a specific pattern
*/
pattern(value: string, regex: RegExp, name: string, message?: string): Result<string>;
/**
* Validates that a string is a valid email
*/
email(value: string, name: string): Result<string>;
/**
* Validates that a string has a minimum length
*/
minLength(value: string, minLength: number, name: string): Result<string>;
/**
* Validates that a string doesn't exceed maximum length
*/
maxLength(value: string, maxLength: number, name: string): Result<string>;
/**
* Validates that a string's length is within a range
*/
length(value: string, minLength: number, maxLength: number, name: string): Result<string>;
/**
* Validates that a string contains only letters
*/
letters(value: string, name: string): Result<string>;
/**
* Validates that a string contains only alphanumeric characters
*/
alphanumeric(value: string, name: string): Result<string>;
};
/**
* Number-specific validations
*/
static Number: {
/**
* Validates that a number is positive (greater than 0)
*/
positive(value: number, name: string): Result<number>;
/**
* Validates that a number is non-negative (0 or greater)
*/
nonNegative(value: number, name: string): Result<number>;
/**
* Validates that a number is within a range
*/
range(value: number, min: number, max: number, name: string): Result<number>;
/**
* Validates that a number is greater than a minimum value
*/
min(value: number, min: number, name: string): Result<number>;
/**
* Validates that a number is less than a maximum value
*/
max(value: number, max: number, name: string): Result<number>;
/**
* Validates that a number is an integer
*/
integer(value: number, name: string): Result<number>;
};
/**
* Array-specific validations
*/
static Array: {
/**
* Validates that an array is not empty
*/
nonEmpty<T>(array: T[], name: string): Result<T[]>;
/**
* Validates that an array has a minimum length
*/
minLength<T>(array: T[], minLength: number, name: string): Result<T[]>;
/**
* Validates that an array doesn't exceed maximum length
*/
maxLength<T>(array: T[], maxLength: number, name: string): Result<T[]>;
/**
* Validates that an array contains a specific item
*/
includes<T>(array: T[], item: T, name: string): Result<T[]>;
/**
* Validates that all items in an array meet a condition
*/
every<T>(array: T[], predicate: (item: T) => boolean, name: string, message: string): Result<T[]>;
};
/**
* Object-specific validations
*/
static Object: {
/**
* Validates that an object has a specific property
*/
hasProperty<T extends object, K extends string | number | symbol>(obj: T, property: K, name: string): Result<T & Record<K, unknown>>;
/**
* Validates that an object is not empty
*/
nonEmpty<T extends object>(obj: T, name: string): Result<T>;
};
/**
* Date-specific validations
*/
static Date: {
/**
* Validates that a date is in the past
*/
inPast(date: Date, name: string): Result<Date>;
/**
* Validates that a date is in the future
*/
inFuture(date: Date, name: string): Result<Date>;
/**
* Validates that a date is after a specific date
*/
after(date: Date, threshold: Date, name: string): Result<Date>;
/**
* Validates that a date is before a specific date
*/
before(date: Date, threshold: Date, name: string): Result<Date>;
};
/**
* Validates that a boolean is true. Non-static
*/
isTrue(value: boolean, name: string): Result<boolean>;
}