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
- Ensure the input renders data-val-length-min and/or data-val-length-max (or data-val-minlength / data-val-maxlength) with integer values.
- Confirm the C# property has [StringLength(50)] / [MinLength(3)] / [MaxLength(20)] so the tag helper emits the attribute.
- If registering manually, pass { min: '3', max: '50' } (at least one) in params.
- 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
- Use [StringLength]/[MinLength]/[MaxLength] on the model so tag helpers emit the bound attributes.
- Verify rendered inputs include data-val-length-min/-max (or -minlength/-maxlength).
- Ensure HTML sanitizers preserve length data-* attributes.
- Pass at least one bound when registering the validator manually.
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
- EqualTo validator requires a non-empty "other" parameter.
- FileExtensions validator requires a non-empty "extensions"…
- Range validator requires at least one of "min" or "max"…
- regex validator requires a non-empty "pattern" parameter.
- assembly must be defined when using a descriptor.
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)