{"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":null,"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/294cab2f9b2e03af6b953820c7ab497c3c8b7ad9/src/Components/Web.JS/src/Validation/Validators/StringLength.ts#L1-L30","documentation":"Thrown by the stringLength validator (StringLength.ts:12) when both params.min and params.max are undefined. This single validator backs three attributes — [StringLength], [MinLength], and [MaxLength] — all of which enforce string length bounds. With neither bound supplied the rule has no effect, so it throws to surface the misconfiguration rather than silently passing.","triggerScenarios":"Invoking stringLengthValidator with a params object containing neither 'min' nor 'max'. The validator is registered under the 'length', 'minlength', and 'maxlength' rule names; if the corresponding data-val-* attribute fails to emit its bound(s), the JS validator throws.","commonSituations":"Model has [StringLength] / [MinLength] / [MaxLength] but the rendered input is missing both data-val-length-min and data-val-length-max (or the minlength/maxlength equivalents). Manual validator wiring that forgets bounds. Custom form rendering that strips data-val-length-* attributes.","solutions":["Apply [StringLength(50, MinimumLength=3)] or [MinLength(3)] / [MaxLength(50)] and confirm the rendered input carries data-val-length-min and/or data-val-length-max with non-empty values.","When wiring manually, pass params: { min: '3', max: '50' } — at least one must be present.","Confirm bound values (including 0) are serialized as strings and not dropped by a serializer that treats 0 as falsy.","Check that no tag helper or partial view is omitting the data-val-length-* attributes."],"exampleFix":"// before\n<input name=\"Username\" data-val-length />\n\n// after\n<input name=\"Username\" data-val-length data-val-length-min=\"3\" data-val-length-max=\"50\" />","handlingStrategy":"validation","validationCode":"function hasLengthBound(params: Record<string, unknown>): boolean {\n  return params && (params.min !== undefined || params.max !== undefined);\n}","typeGuard":"function isStringLengthConfigured(p: any): p is { min?: string; max?: string } {\n  return p && (p.min !== undefined || p.max !== undefined);\n}","tryCatchPattern":null,"preventionTips":["Always provide at least one bound on [StringLength]/[MinLength]/[MaxLength].","Confirm data-val-length-min/max render on the input.","Serialize bounds (including 0) as strings.","Use the same validator name conventions the framework emits."],"tags":["validation","blazor","forms","string-length","configuration"],"analyzedSha":"294cab2f9b2e03af6b953820c7ab497c3c8b7ad9","analyzedAt":"2026-08-06T20:08:02.189Z","schemaVersion":2},"datasetVersion":"2026-08-06T23:17:07.152Z"}