/** * Built-in validation functions for form fields. * * Each factory returns a {@link SyncValidator} that can be passed * to a field's `validators` array in {@link FormConfig}. * * @module bquery/forms */ import type { AsyncValidator, SyncValidator, Validator } from './types'; /** * Requires a non-empty value. * * Fails for `undefined`, `null`, empty strings (after trim), and empty arrays. * * @param message - Custom error message (default: `'This field is required'`) * @returns A sync validator function * * @example * ```ts * import { required } from '@bquery/bquery/forms'; * const validate = required('Name is required'); * validate(''); // 'Name is required' * validate('Ada'); // true * ``` */ export declare const required: (message?: string) => SyncValidator; /** * Requires a string to have at least `len` characters. * * Non-string values are coerced via `String()` before checking length. * * @param len - Minimum length * @param message - Custom error message * @returns A sync validator function * * @example * ```ts * import { minLength } from '@bquery/bquery/forms'; * const validate = minLength(3); * validate('ab'); // 'Must be at least 3 characters' * validate('abc'); // true * ``` */ export declare const minLength: (len: number, message?: string) => SyncValidator; /** * Requires a string to have at most `len` characters. * * Non-string values are coerced via `String()` before checking length. * * @param len - Maximum length * @param message - Custom error message * @returns A sync validator function * * @example * ```ts * import { maxLength } from '@bquery/bquery/forms'; * const validate = maxLength(10); * validate('hello world!!'); // 'Must be at most 10 characters' * validate('hello'); // true * ``` */ export declare const maxLength: (len: number, message?: string) => SyncValidator; /** * Requires a string to match a regular expression pattern. * * Non-string values are coerced via `String()` before testing. * * @param regex - Pattern to test against * @param message - Custom error message (default: `'Invalid format'`) * @returns A sync validator function * * @example * ```ts * import { pattern } from '@bquery/bquery/forms'; * const validate = pattern(/^\d+$/, 'Numbers only'); * validate('abc'); // 'Numbers only' * validate('123'); // true * ``` */ export declare const pattern: (regex: RegExp, message?: string) => SyncValidator; /** * RFC 5322–simplified email validation. * * @param message - Custom error message (default: `'Invalid email address'`) * @returns A sync validator function * * @example * ```ts * import { email } from '@bquery/bquery/forms'; * const validate = email(); * validate('nope'); // 'Invalid email address' * validate('ada@lovelace'); // 'Invalid email address' * validate('ada@love.co'); // true * ``` */ export declare const email: (message?: string) => SyncValidator; /** * Requires a string to be a valid URL. * * Uses the native `URL` constructor for validation. * * @param message - Custom error message (default: `'Invalid URL'`) * @returns A sync validator function * * @example * ```ts * import { url } from '@bquery/bquery/forms'; * const validate = url(); * validate('not-a-url'); // 'Invalid URL' * validate('https://example.com'); // true * ``` */ export declare const url: (message?: string) => SyncValidator; /** * Requires a numeric value to be at least `limit`. * * @param limit - Minimum allowed value (inclusive) * @param message - Custom error message * @returns A sync validator function * * @example * ```ts * import { min } from '@bquery/bquery/forms'; * const validate = min(1, 'Must be positive'); * validate(0); // 'Must be positive' * validate(1); // true * ``` */ export declare const min: (limit: number, message?: string) => SyncValidator; /** * Requires a numeric value to be at most `limit`. * * @param limit - Maximum allowed value (inclusive) * @param message - Custom error message * @returns A sync validator function * * @example * ```ts * import { max } from '@bquery/bquery/forms'; * const validate = max(100, 'Too high'); * validate(101); // 'Too high' * validate(100); // true * ``` */ export declare const max: (limit: number, message?: string) => SyncValidator; /** * Creates a custom synchronous validator from any predicate function. * * @param fn - Predicate that returns `true` when the value is valid * @param message - Error message when the predicate returns `false` * @returns A sync validator function * * @example * ```ts * import { custom } from '@bquery/bquery/forms'; * const isEven = custom((v: number) => v % 2 === 0, 'Must be even'); * isEven(3); // 'Must be even' * isEven(4); // true * ``` */ export declare const custom: (fn: (value: T) => boolean, message: string) => SyncValidator; /** * Creates a custom asynchronous validator. * * @param fn - Async predicate that resolves to `true` when valid * @param message - Error message when the predicate resolves to `false` * @returns An async validator function * * @example * ```ts * import { customAsync } from '@bquery/bquery/forms'; * const isUnique = customAsync( * async (name: string) => !(await checkExists(name)), * 'Already taken', * ); * ``` */ export declare const customAsync: (fn: (value: T) => Promise, message: string) => AsyncValidator; /** * Requires a field's value to match the current value of a reference signal. * * Typically used for "confirm password" or "confirm email" patterns where * one field must have the same value as another. * * @param ref - A reactive signal whose current value is the comparison target * @param message - Custom error message (default: `'Fields do not match'`) * @returns A sync validator function * * @example * ```ts * import { signal } from '@bquery/bquery/reactive'; * import { matchField } from '@bquery/bquery/forms'; * * const password = signal('secret'); * const confirmPassword = signal(''); * const validateConfirmPassword = matchField(password, 'Passwords must match'); * * validateConfirmPassword(confirmPassword.value); * ``` */ export declare const matchField: (ref: { readonly value: T; }, message?: string) => SyncValidator; /** * Requires the value to be a finite integer. * * Strings are accepted only when they parse cleanly to an integer (no decimals, * no extra whitespace beyond outer trim). * * @param message - Custom error message (default: `'Must be an integer'`) * @returns A sync validator function * * @example * ```ts * import { integer } from '@bquery/bquery/forms'; * integer()('3.14'); // 'Must be an integer' * integer()('42'); // true * ``` */ export declare const integer: (message?: string) => SyncValidator; /** * Requires the value to be a finite number (integer or decimal). * * @param message - Custom error message (default: `'Must be a number'`) * @returns A sync validator function * * @example * ```ts * import { numeric } from '@bquery/bquery/forms'; * numeric()('abc'); // 'Must be a number' * numeric()('3.5'); // true * ``` */ export declare const numeric: (message?: string) => SyncValidator; /** * Requires a numeric value to fall within the inclusive `[minLimit, maxLimit]` range. * * @param minLimit - Inclusive lower bound * @param maxLimit - Inclusive upper bound * @param message - Custom error message * @returns A sync validator function * * @example * ```ts * import { between } from '@bquery/bquery/forms'; * between(1, 10)(0); // 'Must be between 1 and 10' * between(1, 10)(5); // true * ``` */ export declare const between: (minLimit: number, maxLimit: number, message?: string) => SyncValidator; /** * Requires a string or array to have exactly `exact` items/characters. * * @param exact - Required length * @param message - Custom error message * @returns A sync validator function * * @example * ```ts * import { length } from '@bquery/bquery/forms'; * length(3)('ab'); // 'Must be exactly 3 characters' * length(3)('abc'); // true * length(2)([1, 2]); // true * ``` */ export declare const length: (exact: number, message?: string) => SyncValidator; /** * Requires the value to be one of `values` (using `Object.is` for comparison). * * @param values - Allowed values * @param message - Custom error message (default: `'Invalid value'`) * @returns A sync validator function * * @example * ```ts * import { oneOf } from '@bquery/bquery/forms'; * oneOf(['a', 'b'])('c'); // 'Invalid value' * oneOf(['a', 'b'])('a'); // true * ``` */ export declare const oneOf: (values: readonly T[], message?: string) => SyncValidator; /** * Rejects values that match any item in `values` (using `Object.is`). * * @param values - Disallowed values * @param message - Custom error message (default: `'Value is not allowed'`) * @returns A sync validator function */ export declare const notOneOf: (values: readonly T[], message?: string) => SyncValidator; /** * Validates each item in an array against `itemValidator`. * * Returns the first item error (prefixed with `[index]`) when any item fails. * Non-array values pass through unchanged so this validator can be composed * with `required()` to enforce presence separately. * * @param itemValidator - Validator applied to every array element * @param message - Optional custom message returned instead of the default `[index] ` output * @returns A sync or async validator * * @example * ```ts * import { arrayOf, required } from '@bquery/bquery/forms'; * const noEmpty = arrayOf(required('Cannot be empty')); * noEmpty(['a', '']); // '[1] Cannot be empty' * ``` */ export declare const arrayOf: (itemValidator: Validator, message?: string) => Validator; /** * Requires a non-empty value only when `predicate` returns truthy. * * Useful for fields that become mandatory based on other state (e.g. another * field's value). The predicate receives the current field value but the * decision can be based on any captured state. * * @param predicate - Function that decides whether the value is required * @param message - Custom error message (default: `'This field is required'`) * @returns A sync validator function * * @example * ```ts * import { requiredIf } from '@bquery/bquery/forms'; * import { signal } from '@bquery/bquery/reactive'; * * const wantsNewsletter = signal(true); * const validate = requiredIf(() => wantsNewsletter.value); * ``` */ export declare const requiredIf: (predicate: (value: T) => unknown, message?: string) => SyncValidator; /** * Requires a non-empty value unless `predicate` returns truthy. * * @param predicate - Function that decides whether the value is optional * @param message - Custom error message (default: `'This field is required'`) * @returns A sync validator function */ export declare const requiredUnless: (predicate: (value: T) => unknown, message?: string) => SyncValidator; /** * Requires the value to be a parseable date. * * Accepts `Date` instances, ISO date strings, and numeric timestamps. Empty * values pass through and should be combined with `required()` when needed. * * @param message - Custom error message (default: `'Invalid date'`) * @returns A sync validator function */ export declare const validDate: (message?: string) => SyncValidator; /** * Requires a date value to be strictly after `date`. * * @param date - The reference date (Date, ISO string, or timestamp number) * @param message - Custom error message * @returns A sync validator function */ export declare const dateAfter: (date: Date | string | number, message?: string) => SyncValidator; /** * Requires a date value to be strictly before `date`. * * @param date - The reference date (Date, ISO string, or timestamp number) * @param message - Custom error message * @returns A sync validator function */ export declare const dateBefore: (date: Date | string | number, message?: string) => SyncValidator; /** * Requires every `File` in the value to be at most `maxBytes` in size. * * Accepts a single `File`, a `FileList`, or an array of `File`s. Non-file * values pass through unchanged. * * @param maxBytes - Maximum allowed file size in bytes * @param message - Custom error message * @returns A sync validator function */ export declare const fileSize: (maxBytes: number, message?: string) => SyncValidator; /** * Requires every `File` in the value to match one of the allowed MIME types. * * Allowed types support wildcards like `'image/*'`. * * @param allowedMime - Allowed MIME type list * @param message - Custom error message * @returns A sync validator function */ export declare const fileType: (allowedMime: readonly string[], message?: string) => SyncValidator; /** * Combines multiple validators; returns on the first failure (logical AND). * * Equivalent to passing the validators in order to a field's `validators` * array, but useful when you want to expose a single composed validator. * * @example * ```ts * import { compose, required, minLength } from '@bquery/bquery/forms'; * const usernameRule = compose(required(), minLength(3)); * ``` */ export declare const compose: (...validators: Validator[]) => Validator; /** * Runs all validators and joins their error messages with `separator`. * * Unlike `compose`, this does not short-circuit; it collects every failure * and reports them together, useful for "show all issues at once" UX. * * @param validators - Validators to run * @param separator - String used to join messages (default: `'; '`) * @returns A validator function */ export declare const all: (validators: Validator[], separator?: string) => Validator; /** * Inverts a validator's result. * * If the inner validator returns valid, this validator fails with `message`; * if the inner validator fails, this one passes. * * @example * ```ts * import { not, oneOf } from '@bquery/bquery/forms'; * const notReserved = not(oneOf(['admin', 'root']), 'Username is reserved'); * ``` */ export declare const not: (validator: Validator, message?: string) => Validator; /** * Wraps a validator, replacing its error message with `message`. * * Useful for adapting library validators to your i18n strings without * re-writing the rules. * * @example * ```ts * import { withMessage, email } from '@bquery/bquery/forms'; * const v = withMessage(email(), 'Please enter a valid email'); * ``` */ export declare const withMessage: (validator: Validator, message: string) => Validator; //# sourceMappingURL=validators.d.ts.map