Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SelectLengthValidator #144

Open
1 task done
i-panov opened this issue Jan 22, 2025 · 0 comments
Open
1 task done

SelectLengthValidator #144

i-panov opened this issue Jan 22, 2025 · 0 comments
Labels
enhancement New feature or request

Comments

@i-panov
Copy link

i-panov commented Jan 22, 2025

Is there an existing issue for this?

  • I have searched the existing issues

Package/Plugin version

11.1.1

What you'd like to happen

Hello. I would like to suggest that you implement a length validator, with options to choose from. This may be necessary for some standardized form values, such as INN (which can be either 10 or 12 characters long).

Alternatives you've considered

Not found

Aditional information

class SelectLengthValidator<T> extends TranslatedValidator<T> {
  final Set<int> lengthOptions;

  const SelectLengthValidator(this.lengthOptions, {
    super.errorText,
    super.checkNullOrEmpty,
  });

  @override
  String get translatedErrorText => 'The length of the value must be one of the following values: $lengthOptions.';

  @override
  String? validateValue(T valueCandidate) {
    final valueLength = switch (valueCandidate) {
      String() => valueCandidate.length,
      Iterable() => valueCandidate.length,
      Map() => valueCandidate.length,
      _ => 0,
    };

    return !lengthOptions.contains(valueLength) ? errorText : null;
  }
}
@i-panov i-panov added the enhancement New feature or request label Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant