custom static method

String? custom({
  1. required String value,
  2. String? title,
  3. bool trimWhitespace = true,
  4. bool isRequired = true,
  5. int? minLength,
  6. int? maxLength,
  7. String? pattern,
  8. PatternType? patternType,
  9. bool allowOnlyNumbers = false,
  10. bool allowOnlyLetters = false,
  11. String? emptyMessage,
  12. String? minLengthMessage,
  13. String? maxLengthMessage,
  14. String? invalidPatternMessage,
  15. String? invalidNumberMessage,
  16. String? invalidLettersMessage,
  17. String? customValidator(
    1. String value
    )?,
  18. List<String>? existingValues,
  19. String? alreadyExistsMessage,
})

Provides flexible custom validation with multiple configurable rules.

The most versatile validation method, allowing combination of multiple validation rules. Perfect for usernames, product codes, or custom fields.

Validation order: required check, existing values, length, character restrictions, pattern matching, custom function.

Parameters:

  • value: The input value to validate
  • title: Field name for error messages (default: "Field")
  • trimWhitespace: Whether to trim whitespace (default: true)
  • isRequired: Whether the field is required (default: true)
  • minLength, maxLength: Length constraints
  • pattern: Custom regex pattern
  • patternType: Predefined pattern from PatternType enum
  • allowOnlyNumbers, allowOnlyLetters: Character type restrictions
  • customValidator: Custom validation function
  • existingValues: List of existing values to check against
  • Various message parameters for customizing error messages Validates custom input with flexible validation rules.

Parameters:

  • value: The value to validate
  • title: Field title for error messages
  • trimWhitespace: Whether to trim whitespace (default: true)
  • isRequired: Whether empty values are allowed (default: true)
  • minLength: Minimum allowed length
  • maxLength: Maximum allowed length
  • pattern: Custom regex pattern to validate against
  • patternType: Predefined pattern type (overrides pattern if both provided)
  • allowOnlyNumbers: Restrict to numeric characters only (default: false)
  • allowOnlyLetters: Restrict to alphabetic characters only (default: false)
  • emptyMessage: Custom empty validation message
  • minLengthMessage: Custom minimum length message
  • maxLengthMessage: Custom maximum length message
  • invalidPatternMessage: Custom invalid pattern message
  • invalidNumberMessage: Custom numbers-only violation message
  • invalidLettersMessage: Custom letters-only violation message
  • customValidator: Custom validation function for complex rules
  • existingValues: List of existing values for uniqueness check
  • alreadyExistsMessage: Custom duplicate value message

Returns null if valid, or an error message if invalid.

Example:

// Validate username with custom rules
final error = SahihValidator.custom(
  value: username,
  minLength: 4,
  maxLength: 20,
  allowOnlyLetters: true,
  invalidLettersMessage: 'Username must contain only letters'
);

Implementation

static String? custom({
  required String value,
  String? title,
  bool trimWhitespace = true,
  bool isRequired = true,
  int? minLength,
  int? maxLength,
  String? pattern,
  PatternType? patternType,
  bool allowOnlyNumbers = false,
  bool allowOnlyLetters = false,
  String? emptyMessage,
  String? minLengthMessage,
  String? maxLengthMessage,
  String? invalidPatternMessage,
  String? invalidNumberMessage,
  String? invalidLettersMessage,
  String? Function(String value)? customValidator,
  List<String>? existingValues,
  String? alreadyExistsMessage,
}) {
  return customValidate(
    value: value,
    title: title,
    trimWhitespace: trimWhitespace,
    isRequired: isRequired,
    minLength: minLength,
    maxLength: maxLength,
    pattern: pattern,
    patternType: patternType,
    allowOnlyNumbers: allowOnlyNumbers,
    allowOnlyLetters: allowOnlyLetters,
    emptyMessage: emptyMessage,
    minLengthMessage: minLengthMessage,
    maxLengthMessage: maxLengthMessage,
    invalidPatternMessage: invalidPatternMessage,
    invalidNumberMessage: invalidNumberMessage,
    invalidLettersMessage: invalidLettersMessage,
    customValidator: customValidator,
    existingValues: existingValues,
    alreadyExistsMessage: alreadyExistsMessage,
  );
}