checkTextLength static method

VerificationResult Function(String?, VerificationMeta) checkTextLength({
  1. int? minTextLength,
  2. int? maxTextLength,
})

A value for additionalChecks validating allowed text lengths.

Used by generated code.

Implementation

static VerificationResult Function(String?, VerificationMeta) checkTextLength(
    {int? minTextLength, int? maxTextLength}) {
  return (value, meta) {
    if (value == null) return const VerificationResult.success();

    final length = value.length;
    if (minTextLength != null && minTextLength > length) {
      return VerificationResult.failure(
          'Must at least be $minTextLength characters long.');
    }
    if (maxTextLength != null && maxTextLength < length) {
      return VerificationResult.failure(
          'Must at most be $maxTextLength characters long.');
    }

    return const VerificationResult.success();
  };
}