stringMatchesPattern function

IValidator stringMatchesPattern(
  1. Pattern pattern, {
  2. String? error,
})

Validates that the String matches the provided pattern

This validator also validates that the value is a String first So there's no need to add the isString validator when using this validator

Implementation

IValidator stringMatchesPattern(Pattern pattern, {String? error}) {
  return isType<String>() &
      validator(
        (value) => pattern.allMatches(value).isNotEmpty,
        (value) => Expectation(
          message: error ?? 'String to match "$pattern"',
          value: value,
          code: 'value.pattern_mismatch',
          data: {'pattern': pattern.toString()},
        ),
      );
}