{"record":{"id":"4fb6a8b4b31755a8","repo":"dotnet/aspnetcore","slug":"length-minlength-maxlength-validator-requires-at-l","errorCode":null,"errorMessage":"length/minlength/maxlength validator requires at least one of \"min\" or \"max\" parameters.","messagePattern":"length/minlength/maxlength validator requires at least one of \"min\" or \"max\" parameters\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/Components/Web.JS/src/Validation/Validators/StringLength.ts","lineNumber":12,"sourceCode":"// Licensed to the .NET Foundation under one or more agreements.\n// The .NET Foundation licenses this file to you under the MIT license.\n\nimport { ValidationContext, ValidationResult, Validator, pass, fail } from '../ValidationTypes';\n\n// Validates string length against min and/or max bounds (inclusive).\n// Used for 'length' ([StringLength]), 'minlength' ([MinLength]), and 'maxlength' ([MaxLength]) rules.\n// Throws if neither bound is defined (rule has no effect).\nexport const stringLengthValidator: Validator = (context: ValidationContext): ValidationResult => {\n  const { value, params } = context;\n  if (params.min === undefined && params.max === undefined) {\n    throw new Error('length/minlength/maxlength validator requires at least one of \"min\" or \"max\" parameters.');\n  }\n\n  if (!value) {\n    return pass();\n  }\n\n  if (params.min !== undefined) {\n    const min = parseInt(params.min, 10);\n    if (value.length < min) {\n      return fail();\n    }\n  }\n\n  if (params.max !== undefined) {\n    const max = parseInt(params.max, 10);\n    if (value.length > max) {\n      return fail();\n    }","sourceCodeStart":1,"sourceCodeEnd":30,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/3600ca084e9c8b5f4174fc5e747f4c52d2100806/src/Components/Web.JS/src/Validation/Validators/StringLength.ts#L1-L30","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"<!-- before -->\n<input name=\"Username\" data-val=\"true\" data-val-length=\"Too long\" />\n<!-- after -->\n<input name=\"Username\" data-val=\"true\" data-val-length=\"Too long\" data-val-length-max=\"20\" />","handlingStrategy":"validation","validationCode":"function lengthConfigured(params: any): boolean {\n  return !!params && (params.min !== undefined || params.max !== undefined);\n}","typeGuard":"function hasLengthBound(p: unknown): boolean {\n  return !!p && ((p as any).min !== undefined || (p as any).max !== undefined);\n}","tryCatchPattern":null,"preventionTips":["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."],"tags":["blazor","validation","stringlength","minlength","maxlength","form-validation","misconfiguration"],"backgroundTag":null,"analyzedSha":"3600ca084e9c8b5f4174fc5e747f4c52d2100806","analyzedAt":"2026-08-11T16:32:30.678Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}