UNPKG

@resk/core

Version:

An innovative TypeScript framework that empowers developers to build applications with a fully decorator-based architecture for efficient resource management. By combining the power of decorators with a resource-oriented design, DecorRes enhances code cla

341 lines (340 loc) 11.3 kB
/** * ### IsNonNullString Decorator * * Validates that a property value is a non-null, non-empty string. This * decorator is stricter than IsRequired as it also ensures the value is * a string with actual content (not just whitespace). * * @example * ```typescript * class Article { * @IsNonNullString * title: string; * * @IsNonNullString * content: string; * * @IsNonNullString * author: string; * } * * // Valid data * const article = { * title: "How to Validate Data", * content: "This article explains validation...", * author: "John Doe" * }; * * // Invalid data * const invalid = { * title: "", // Empty string * content: " ", // Only whitespace * author: null // Null value * }; * ``` * * @decorator * @since 1.0.0 * @see {@link IsRequired} - Less strict alternative * @public */ export declare const IsNonNullString: PropertyDecorator; /** * @decorator MinLength * * Validator rule that checks if a given string meets a minimum length requirement. * This rule ensures that the input string has at least the specified number of characters. * * ### Parameters: * - **options**: `IValidatorValidateOptions` - An object containing: * - `value`: The string value to validate. * - `ruleParams`: An array where the first element specifies the minimum length required. * * ### Return Value: * - `boolean | string`: Returns `true` if the value is empty or meets the minimum length requirement; * otherwise, returns an error message indicating that the minimum length is not met. * * ### Example Usage: * ```typescript * class MyClass { * @ MinLength([3]) //"This field must have a minimum of 3 characters" * myString: string; * } * ``` * * ### Notes: * - This rule is useful for validating user input in forms, ensuring that the input meets a minimum length requirement. * - The error message can be customized based on the parameters provided, allowing for clear feedback to users. * - The `isEmpty` utility function is used to check for empty values, which may include `null`, `undefined`, or empty strings. */ export declare const MinLength: (ruleParameters: [minLength: number]) => PropertyDecorator; /** * @decorator MaxLength * * Validator rule that checks if a given string does not exceed a maximum length. * This rule ensures that the input string has at most the specified number of characters. * * ### Parameters: * - **options**: `IValidatorValidateOptions` - An object containing: * - `value`: The string value to validate. * - `ruleParams`: An array where the first element specifies the maximum length allowed. * * ### Return Value: * - `boolean | string`: Returns `true` if the value is empty or meets the maximum length requirement; * otherwise, returns an error message indicating that the maximum length is exceeded. * * ### Example Usage: * ```typescript import { MaxLength } from '@resk/core'; class MyClass { @ MaxLength([10]) myProperty: string; } * ``` * * ### Notes: * - This rule is useful for validating user input in forms, ensuring that the input does not exceed a specified length. * - The error message can be customized based on the parameters provided, allowing for clear feedback to users. * - The `isEmpty` utility function is used to check for empty values, which may include `null`, `undefined`, or empty strings. */ export declare const MaxLength: (ruleParameters: [maxLength: number]) => PropertyDecorator; /** * @decorator Length * * Validator rule that validates the length of a string. This rule checks if the length of the input string * falls within a specified range or matches a specific length. * * ### Parameters: * - **options**: `IValidatorValidateOptions` - An object containing: * - `value`: The string value to validate. * - `ruleParams`: An array where: * - The first element specifies the minimum length (optional). * - The second element specifies the maximum length (optional). * * ### Return Value: * - `boolean | string`: Returns `true` if the string length is valid according to the specified rules; * otherwise, returns an error message indicating the validation failure. * * ### Example Usage: * ```typescript * * class MyClass { * @ Length([3, 10]) //"This field must be between 3 and 10 characters long" * myString: string; * } * * class MyClass { * @ Length([4]) //"This field must be exactly 4 characters long" * myString: string; * } * ``` * * ### Notes: * - This rule is useful for validating user input in forms, ensuring that the input meets specific length requirements. * - The error messages can be customized based on the parameters provided, allowing for clear feedback to users. * - The `defaultStr` utility function is used to ensure that the value is treated as a string, even if it is `null` or `undefined`. */ export declare const Length: (ruleParameters: [minOrLength: number, maxLength?: number | undefined]) => PropertyDecorator; /** * ### EndsWithOneOf Rule * * Validates that the field under validation ends with one of the given values. * * #### Parameters * - List of values that the field must end with * * @example * ```typescript * // Class validation * class FileUpload { * @EndsWithOneOf(['jpg', 'png', 'gif', 'webp']) * imageFile: string; * * @EndsWithOneOf(['.com', '.org', '.net']) * websiteUrl: string; * } * ``` * * @param options - Validation options with rule parameters * @param options.ruleParams - Array of valid ending values * @returns Promise resolving to true if valid, rejecting with error message if invalid * * @since 1.22.0 * @public */ export declare const EndsWithOneOf: (ruleParameters: string[]) => PropertyDecorator; export declare const StartsWithOneOf: (ruleParameters: string[]) => PropertyDecorator; /** * ### IsString Rule * * Validates that the field under validation is a string. If you would like to * allow the field to also be null, you should assign the nullable rule to the field. * * @example * ```typescript * // Class validation * class TextContent { * @IsRequired * @IsString * title: String; * * @IsString * description?: String | null; * } * ``` * * @param options - Validation options containing value and context * @returns Promise resolving to true if valid, rejecting with error message if invalid * * @since 1.22.0 * @public */ export declare const IsString: PropertyDecorator; declare module "../types" { interface IValidatorRulesMap<Context = unknown> { /** * ### String Rule * * Validates that the field under validation is a string. If you would like to * allow the field to also be null, you should assign the nullable rule to the field. * * @example * ```typescript * // Valid String values * await Validator.validate({ * value: 'Hello World', * rules: ['String'] * }); // ✓ Valid * * await Validator.validate({ * value: '', * rules: ['String'] * }); // ✓ Valid (empty String) * * // Invalid examples * await Validator.validate({ * value: 123, * rules: ['String'] * }); // ✗ Invalid * * await Validator.validate({ * value: null, * rules: ['String'] * }); // ✗ Invalid (use nullable for null support) * * // With nullable support * await Validator.validate({ * value: null, * rules: ['nullable', 'String'] * }); // ✓ Valid * * // Class validation * class TextContent { * @Required * @String * title: String; * * @String * description?: String | null; * } * ``` * * @param options - Validation options containing value and context * @returns Promise resolving to true if valid, rejecting with error message if invalid * * @since 1.22.0 * @public */ String: IValidatorRuleParams<[], Context>; /** * ### StartsWithOneOf Rule * * Validates that the field under validation starts with one of the given values. * * #### Parameters * - List of values that the field must start with * * @example * ```typescript * // Valid beginnings * await Validator.validate({ * value: 'https://example.com', * rules: ['StartsWithOneOf[http://,https://]'] * }); // ✓ Valid * * await Validator.validate({ * value: 'USER_12345', * rules: ['StartsWithOneOf[USER_,ADMIN_]'] * }); // ✓ Valid * * // Invalid example * await Validator.validate({ * value: 'ftp://example.com', * rules: ['StartsWithOneOf[http://,https://]'] * }); // ✗ Invalid * * // Class validation * class Configuration { * @StartsWithOneOf(['http://', 'https://']) * apiUrl: string; * * @StartsWithOneOf(['prod_', 'dev_', 'test_']) * environment: string; * } * ``` * * @param options - Validation options with rule parameters * @param options.ruleParams - Array of valid starting values * @returns Promise resolving to true if valid, rejecting with error message if invalid * * @since 1.22.0 * @public */ StartsWithOneOf: IValidatorRuleParams<string[], Context>; /** * ### Ends With Rule * * Validates that the field under validation ends with one of the given values. * * #### Parameters * - List of values that the field must end with * * @example * ```typescript * // Valid endings * await Validator.validate({ * value: 'profile.jpg', * rules: ['EndsWithOneOf[jpg,png,gif]'] * }); // ✓ Valid * * await Validator.validate({ * value: 'document.pdf', * rules: ['EndsWithOneOf[pdf,doc,docx]'] * }); // ✓ Valid * * // Invalid example * await Validator.validate({ * value: 'image.txt', * rules: ['EndsWithOneOf[jpg,png,gif]'] * }); // ✗ Invalid * * // Class validation * class FileUpload { * @EndsWithOneOf(['jpg', 'png', 'gif', 'webp']) * imageFile: string; * * @EndsWithOneOf(['.com', '.org', '.net']) * websiteUrl: string; * } * ``` * * @param options - Validation options with rule parameters * @param options.ruleParams - Array of valid ending values * @returns Promise resolving to true if valid, rejecting with error message if invalid * * @since 1.22.0 * @public */ EndsWithOneOf: IValidatorRuleParams<string[], Context>; } }