UNPKG

guardz

Version:

A simple and lightweight TypeScript type guard library for runtime type validation.

61 lines (60 loc) 1.84 kB
import type { TypeGuardFn } from './isType'; /** * Creates a type guard function for object schemas with improved nested type support. * * This function is similar to `isType` but provides better support for deeply nested structures * by automatically handling nested type guards without requiring explicit `isType` calls for each level. * * Key differences from `isType`: * - Automatically detects and handles nested object structures * - Provides better error messages for nested validation failures * - Supports both inline object definitions and existing type guards * - Maintains backward compatibility with existing type guard patterns * * @template T - The type to validate * @param schema - Object mapping property keys to their type guard functions or inline object definitions * @returns A type guard function that validates the object structure * * @example * ```typescript * import { isSchema, isString, isNumber, isBoolean } from 'guardz'; * * // Simple nested structure * const isUser = isSchema({ * name: isString, * age: isNumber, * address: { * street: isString, * city: isString, * zipCode: isNumber, * }, * preferences: { * theme: isString, * notifications: isBoolean, * }, * }); * * // Complex nested structure with arrays * const isComplexUser = isSchema({ * id: isNumber, * profile: { * name: isString, * email: isString, * contacts: [{ * type: isString, * value: isString, * }], * }, * settings: { * theme: isString, * notifications: { * email: isBoolean, * push: isBoolean, * }, * }, * }); * ``` */ export declare function isSchema<T>(schema: any): TypeGuardFn<T>; export declare const isShape: typeof isSchema; export declare const isNestedType: typeof isSchema;