dotnet/aspnetcore · error · Error

length/minlength/maxlength validator requires at least one…

Error message

length/minlength/maxlength validator requires at least one of "min" or "max" parameters.

What it means

The StringLength validator services the [StringLength], [MinLength], and [MaxLength] rules and needs at least one bound (min or max). With neither bound the rule has no effect, so the validator throws rather than silently pass. Bounds are inclusive.

Solutions

  1. Ensure the input renders data-val-length-min and/or data-val-length-max (or data-val-minlength / data-val-maxlength) with integer values.
  2. Confirm the C# property has [StringLength(50)] / [MinLength(3)] / [MaxLength(20)] so the tag helper emits the attribute.
  3. If registering manually, pass { min: '3', max: '50' } (at least one) in params.
  4. Verify no HTML sanitizer is stripping length/minlength/maxlength data-* attributes.

Example fix

<!-- before -->
<input name="Username" data-val="true" data-val-length="Too long" />
<!-- after -->
<input name="Username" data-val="true" data-val-length="Too long" data-val-length-max="20" />
Defensive patterns

Strategy: validation

Validate before calling

function lengthConfigured(params: any): boolean {
  return !!params && (params.min !== undefined || params.max !== undefined);
}

Type guard

function hasLengthBound(p: unknown): boolean {
  return !!p && ((p as any).min !== undefined || (p as any).max !== undefined);
}

Prevention

When it happens

Trigger: Thrown at line 12 when params.min === undefined && params.max === undefined — i.e. a 'length'/'minlength'/'maxlength' rule was registered (data-val-length / data-val-minlength / data-val-maxlength) without a bound value.

Common situations: A [StringLength]/[MinLength]/[MaxLength] attribute whose data-val-length-min / -max (or data-val-minlength / -maxlength) attributes were stripped; a hand-built input referencing the rule but missing the bound; sanitizer removing data-* attributes.

Related errors


AI-assisted analysis of dotnet/aspnetcore@3600ca084e (2026-08-11). Data as JSON: /api/errors/4fb6a8b4b31755a8. Report an issue: GitHub.

Appendix: source

Thrown at src/Components/Web.JS/src/Validation/Validators/StringLength.ts:12

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

import { ValidationContext, ValidationResult, Validator, pass, fail } from '../ValidationTypes';

// Validates string length against min and/or max bounds (inclusive).
// Used for 'length' ([StringLength]), 'minlength' ([MinLength]), and 'maxlength' ([MaxLength]) rules.
// Throws if neither bound is defined (rule has no effect).
export const stringLengthValidator: Validator = (context: ValidationContext): ValidationResult => {
  const { value, params } = context;
  if (params.min === undefined && params.max === undefined) {
    throw new Error('length/minlength/maxlength validator requires at least one of "min" or "max" parameters.');
  }

  if (!value) {
    return pass();
  }

  if (params.min !== undefined) {
    const min = parseInt(params.min, 10);
    if (value.length < min) {
      return fail();
    }
  }

  if (params.max !== undefined) {
    const max = parseInt(params.max, 10);
    if (value.length > max) {
      return fail();
    }

View on GitHub (pinned to 3600ca084e)