Skip to content

SelectLengthValidator #144

@i-panov

Description

@i-panov

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;
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions