hasSpecial static method

Validator<String> hasSpecial({
  1. String? allowedChars,
  2. String? error,
})

Implementation

static Validator<String> hasSpecial({
  String? allowedChars,
  String? error,
}) =>
    (value, context) {
      String buildSpecialCharPattern(String allowedChars) {
        final escapedChars =
            allowedChars.split('').map(RegExp.escape).toList();

        escapedChars.sort((a, b) {
          if (a == r'-') return 1;
          return -1;
        });

        return '[${escapedChars.join()}]';
      }

      final pattern = allowedChars != null
          ? context.resources.getPatternOrCreate(
              'specialChars',
              buildSpecialCharPattern(allowedChars),
            )
          : context.resources.specialCharsPattern;

      if (pattern.hasMatch(value)) {
        return (true, null);
      }
      return (
        false,
        error ??
            context.errors.passwordErrors.hasSpecial(allowedChars ?? '[\\W]'),
      );
    };