@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
537 lines (536 loc) • 16.1 kB
TypeScript
/**
* ### Array Rule
*
* Validates that the field under validation is an array.
*
* @example
* ```typescript
* // Class validation
* class DataCollection {
* @IsRequired
* @IsArray
* items: any[];
* }
* ```
*
* @param options - Validation options containing value and context
* @returns Promise resolving to true if valid, rejecting with error message if invalid
*
* @since 1.35.1
* @public
*/
export declare const IsArray: PropertyDecorator;
/**
* ### ArrayMinLength Rule
*
* Validates that the array has at least the specified minimum length.
*
* #### Parameters
* - Minimum length (number)
*
* @example
* ```typescript
* // Class validation
* class ShoppingCart {
* @ArrayMinLength(1)
* items: Product[];
* }
* ```
*
* @param options - Validation options with rule parameters
* @param options.ruleParams - Array containing minimum length
* @returns Promise resolving to true if valid, rejecting with error message if invalid
*
* @since 1.35.1
* @public
*/
export declare const ArrayMinLength: (ruleParameters: [number]) => PropertyDecorator;
/**
* ### ArrayMaxLength Rule
*
* Validates that the array has at most the specified maximum length.
*
* #### Parameters
* - Maximum length (number)
*
* @example
* ```typescript
* // Class validation
* class LimitedList {
* @ArrayMaxLength(10)
* tags: string[];
* }
* ```
*
* @param options - Validation options with rule parameters
* @param options.ruleParams - Array containing maximum length
* @returns Promise resolving to true if valid, rejecting with error message if invalid
*
* @since 1.35.1
* @public
*/
export declare const ArrayMaxLength: (ruleParameters: [number]) => PropertyDecorator;
/**
* ### ArrayLength Rule
*
* Validates that the array has exactly the specified length.
*
* #### Parameters
* - Exact length (number)
*
* @example
* ```typescript
* // Class validation
* class FixedSizeArray {
* @ArrayLength(3)
* coordinates: number[];
* }
* ```
*
* @param options - Validation options with rule parameters
* @param options.ruleParams - Array containing exact length
* @returns Promise resolving to true if valid, rejecting with error message if invalid
*
* @since 1.35.1
* @public
*/
export declare const ArrayLength: (ruleParameters: [number]) => PropertyDecorator;
/**
* ### ArrayContains Rule
*
* Validates that the array contains all of the specified values.
*
* #### Parameters
* - Values that must be present in the array
*
* @example
* ```typescript
* // Class validation
* class Permissions {
* @ArrayContains(['read'])
* userPermissions: string[];
* }
* ```
*
* @param options - Validation options with rule parameters
* @param options.ruleParams - Array of values that must be contained
* @returns Promise resolving to true if valid, rejecting with error message if invalid
*
* @since 1.35.1
* @public
*/
export declare const ArrayContains: (ruleParameters: any[]) => PropertyDecorator;
/**
* ### ArrayUnique Rule
*
* Validates that all elements in the array are unique.
*
* @example
* ```typescript
* // Class validation
* class UniqueTags {
* @ArrayUnique
* tags: string[];
* }
* ```
*
* @param options - Validation options containing value and context
* @returns Promise resolving to true if valid, rejecting with error message if invalid
*
* @since 1.35.1
* @public
*/
export declare const ArrayUnique: PropertyDecorator;
/**
* ### ArrayAllStrings Rule
*
* Validates that all elements in the array are strings.
* The check is strict — only primitive `string` values pass; other types fail.
*
* @example
* ```typescript
* // Programmatic API
* await Validator.validate({ value: ["a", "b"], rules: ["ArrayAllStrings"] }); // ✓ Valid
* await Validator.validate({ value: ["a", 1], rules: ["ArrayAllStrings"] }); // ✗ Invalid
* await Validator.validate({ value: "not an array", rules: ["ArrayAllStrings"] }); // ✗ Invalid (not an array)
*
* // Class validation
* class Tags {
* @ArrayAllStrings
* tags: string[];
* }
* ```
*
* @param options - Validation options containing value and context
* @returns Promise resolving to true if valid, rejecting with error message if invalid
*
* @since 1.35.1
* @public
*/
export declare const ArrayAllStrings: PropertyDecorator;
/**
* ### ArrayAllNumbers Rule
*
* Validates that all elements in the array are numbers.
* The check is strict — only primitive `number` values pass; `NaN` fails.
*
* @example
* ```typescript
* // Programmatic API
* await Validator.validate({ value: [1, 2, 3], rules: ["ArrayAllNumbers"] }); // ✓ Valid
* await Validator.validate({ value: [1, "2"], rules: ["ArrayAllNumbers"] }); // ✗ Invalid
* await Validator.validate({ value: [1, NaN], rules: ["ArrayAllNumbers"] }); // ✗ Invalid (NaN)
* await Validator.validate({ value: "not an array", rules: ["ArrayAllNumbers"] }); // ✗ Invalid (not an array)
*
* // Class validation
* class Scores {
* @ArrayAllNumbers
* values: number[];
* }
* ```
*
* @param options - Validation options containing value and context
* @returns Promise resolving to true if valid, rejecting with error message if invalid
*
* @since 1.35.1
* @public
*/
export declare const ArrayAllNumbers: PropertyDecorator;
declare module "../types" {
interface IValidatorRulesMap<Context = unknown> {
/**
* ### Array Rule
*
* Validates that the field under validation is an array.
*
* @example
* ```typescript
* // Valid arrays
* await Validator.validate({
* value: [1, 2, 3],
* rules: ['Array']
* }); // ✓ Valid
*
* await Validator.validate({
* value: [],
* rules: ['Array']
* }); // ✓ Valid
*
* // Invalid examples
* await Validator.validate({
* value: "not an array",
* rules: ['Array']
* }); // ✗ Invalid
*
* await Validator.validate({
* value: null,
* rules: ['Array']
* }); // ✗ Invalid
*
* // Class validation
* class DataCollection {
* @Required
* @Array
* items: any[];
* }
* ```
*
* @param options - Validation options containing value and context
* @returns Promise resolving to true if valid, rejecting with error message if invalid
*
* @since 1.35.1
* @public
*/
Array: IValidatorRuleParams<[], Context>;
/**
* ### ArrayMinLength Rule
*
* Validates that the array has at least the specified minimum length.
*
* #### Parameters
* - Minimum length (number)
*
* @example
* ```typescript
* // Valid examples
* await Validator.validate({
* value: [1, 2, 3],
* rules: ['ArrayMinLength[2]']
* }); // ✓ Valid
*
* await Validator.validate({
* value: ['a', 'b'],
* rules: ['ArrayMinLength[1]']
* }); // ✓ Valid
*
* // Invalid examples
* await Validator.validate({
* value: [1],
* rules: ['ArrayMinLength[2]']
* }); // ✗ Invalid
*
* await Validator.validate({
* value: "not an array",
* rules: ['ArrayMinLength[1]']
* }); // ✗ Invalid
*
* // Class validation
* class ShoppingCart {
* @ArrayMinLength(1)
* items: Product[];
* }
* ```
*
* @param options - Validation options with rule parameters
* @param options.ruleParams - Array containing minimum length
* @returns Promise resolving to true if valid, rejecting with error message if invalid
*
* @since 1.35.1
* @public
*/
ArrayMinLength: IValidatorRuleParams<[minLength: number], Context>;
/**
* ### ArrayMaxLength Rule
*
* Validates that the array has at most the specified maximum length.
*
* #### Parameters
* - Maximum length (number)
*
* @example
* ```typescript
* // Valid examples
* await Validator.validate({
* value: [1, 2],
* rules: ['ArrayMaxLength[3]']
* }); // ✓ Valid
*
* await Validator.validate({
* value: [],
* rules: ['ArrayMaxLength[10]']
* }); // ✓ Valid
*
* // Invalid examples
* await Validator.validate({
* value: [1, 2, 3, 4],
* rules: ['ArrayMaxLength[3]']
* }); // ✗ Invalid
*
* await Validator.validate({
* value: "not an array",
* rules: ['ArrayMaxLength[5]']
* }); // ✗ Invalid
*
* // Class validation
* class LimitedList {
* @ArrayMaxLength(10)
* tags: string[];
* }
* ```
*
* @param options - Validation options with rule parameters
* @param options.ruleParams - Array containing maximum length
* @returns Promise resolving to true if valid, rejecting with error message if invalid
*
* @since 1.35.1
* @public
*/
ArrayMaxLength: IValidatorRuleParams<[maxLength: number], Context>;
/**
* ### ArrayLength Rule
*
* Validates that the array has exactly the specified length.
*
* #### Parameters
* - Exact length (number)
*
* @example
* ```typescript
* // Valid examples
* await Validator.validate({
* value: [1, 2, 3],
* rules: ['ArrayLength[3]']
* }); // ✓ Valid
*
* await Validator.validate({
* value: ['x', 'y'],
* rules: ['ArrayLength[2]']
* }); // ✓ Valid
*
* // Invalid examples
* await Validator.validate({
* value: [1, 2],
* rules: ['ArrayLength[3]']
* }); // ✗ Invalid
*
* await Validator.validate({
* value: [1, 2, 3, 4],
* rules: ['ArrayLength[3]']
* }); // ✗ Invalid
*
* await Validator.validate({
* value: "not an array",
* rules: ['ArrayLength[2]']
* }); // ✗ Invalid
*
* // Class validation
* class FixedSizeArray {
* @ArrayLength(3)
* coordinates: number[];
* }
* ```
*
* @param options - Validation options with rule parameters
* @param options.ruleParams - Array containing exact length
* @returns Promise resolving to true if valid, rejecting with error message if invalid
*
* @since 1.35.1
* @public
*/
ArrayLength: IValidatorRuleParams<[length: number], Context>;
/**
* ### ArrayContains Rule
*
* Validates that the array contains all of the specified values.
*
* #### Parameters
* - Values that must be present in the array
*
* @example
* ```typescript
* // Valid examples
* await Validator.validate({
* value: ['read', 'write', 'delete'],
* rules: ['ArrayContains[read,write]']
* }); // ✓ Valid
*
* await Validator.validate({
* value: [1, 2, 3, 4],
* rules: ['ArrayContains[2,3]']
* }); // ✓ Valid
*
* // Invalid examples
* await Validator.validate({
* value: ['read', 'write'],
* rules: ['ArrayContains[read,delete]']
* }); // ✗ Invalid
*
* await Validator.validate({
* value: "not an array",
* rules: ['ArrayContains[1]']
* }); // ✗ Invalid
*
* // Class validation
* class Permissions {
* @ArrayContains(['read'])
* userPermissions: string[];
* }
* ```
*
* @param options - Validation options with rule parameters
* @param options.ruleParams - Array of values that must be contained
* @returns Promise resolving to true if valid, rejecting with error message if invalid
*
* @since 1.35.1
* @public
*/
ArrayContains: IValidatorRuleParams<any[], Context>;
/**
* ### ArrayUnique Rule
*
* Validates that all elements in the array are unique.
*
* @example
* ```typescript
* // Valid examples
* await Validator.validate({
* value: [1, 2, 3],
* rules: ['ArrayUnique']
* }); // ✓ Valid
*
* await Validator.validate({
* value: ['a', 'b', 'c'],
* rules: ['ArrayUnique']
* }); // ✓ Valid
*
* // Invalid examples
* await Validator.validate({
* value: [1, 2, 2, 3],
* rules: ['ArrayUnique']
* }); // ✗ Invalid
*
* await Validator.validate({
* value: ['a', 'b', 'a'],
* rules: ['ArrayUnique']
* }); // ✗ Invalid
*
* await Validator.validate({
* value: "not an array",
* rules: ['ArrayUnique']
* }); // ✗ Invalid
*
* // Class validation
* class UniqueTags {
* @ArrayUnique
* tags: string[];
* }
* ```
*
* @param options - Validation options containing value and context
* @returns Promise resolving to true if valid, rejecting with error message if invalid
*
* @since 1.35.1
* @public
*/
ArrayUnique: IValidatorRuleParams<[], Context>;
/**
* ### ArrayAllStrings Rule
*
* Validates that all elements in the array are strings.
*
* @example
* ```typescript
* await Validator.validate({ value: ["a", "b"], rules: ["ArrayAllStrings"] }); // ✓ Valid
* await Validator.validate({ value: ["a", 1], rules: ["ArrayAllStrings"] }); // ✗ Invalid
* await Validator.validate({ value: "not an array", rules: ["ArrayAllStrings"] }); // ✗ Invalid
*
* class Tags {
* @ArrayAllStrings
* tags: string[];
* }
* ```
*
* @param options - Validation options containing value and context
* @returns Promise resolving to true if valid, rejecting with error message if invalid
*
* @since 1.35.1
* @public
*/
ArrayAllStrings: IValidatorRuleParams<[], Context>;
/**
* ### ArrayAllNumbers Rule
*
* Validates that all elements in the array are numbers. `NaN` fails.
*
* @example
* ```typescript
* await Validator.validate({ value: [1, 2, 3], rules: ["ArrayAllNumbers"] }); // ✓ Valid
* await Validator.validate({ value: [1, "2"], rules: ["ArrayAllNumbers"] }); // ✗ Invalid
* await Validator.validate({ value: [1, NaN], rules: ["ArrayAllNumbers"] }); // ✗ Invalid
* await Validator.validate({ value: "not an array", rules: ["ArrayAllNumbers"] }); // ✗ Invalid
*
* class Scores {
* @ArrayAllNumbers
* values: number[];
* }
* ```
*
* @param options - Validation options containing value and context
* @returns Promise resolving to true if valid, rejecting with error message if invalid
*
* @since 1.35.1
* @public
*/
ArrayAllNumbers: IValidatorRuleParams<[], Context>;
}
}