{"record":{"id":"f37f825ee244825b","repo":"dotnet/aspnetcore","slug":"invalid-boolean-value-attributevalue-for-para","errorCode":null,"errorMessage":"Invalid boolean value '${attributeValue}' for parameter '${parameterName}'","messagePattern":"Invalid boolean value '(.+?)' for parameter '(.+?)'","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/Components/CustomElements/src/js/BlazorCustomElements.ts","lineNumber":120,"sourceCode":"        await setParametersPromise;\n      }\n    }\n  }\n\n  static parseAttributeValue(attributeValue: string, type: JSComponentParameterType, parameterName: string): any {\n    switch (type) {\n      case 'string':\n        return attributeValue;\n      case 'boolean':\n        switch (attributeValue) {\n          case 'true':\n          case 'True':\n            return true;\n          case 'false':\n          case 'False':\n            return false;\n          default:\n            throw new Error(`Invalid boolean value '${attributeValue}' for parameter '${parameterName}'`);\n        }\n      case 'number':\n        const number = Number(attributeValue);\n        if (Number.isNaN(number)) {\n          throw new Error(`Invalid number value '${attributeValue}' for parameter '${parameterName}'`);\n        } else {\n          return number;\n        }\n      case 'boolean?':\n        return attributeValue ? BlazorCustomElement.parseAttributeValue(attributeValue, 'boolean', parameterName) : null;\n      case 'number?':\n        return attributeValue ? BlazorCustomElement.parseAttributeValue(attributeValue, 'number', parameterName) : null;\n      case 'object':\n        throw new Error(`The parameter '${parameterName}' accepts a complex-typed object so it cannot be set using an attribute. Try setting it as a element property instead.`);\n      default:\n        throw new Error(`Unknown type '${type}' for parameter '${parameterName}'`);\n    }\n  }","sourceCodeStart":102,"sourceCodeEnd":138,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/294cab2f9b2e03af6b953820c7ab497c3c8b7ad9/src/Components/CustomElements/src/js/BlazorCustomElements.ts#L102-L138","documentation":"Thrown by BlazorCustomElement.parseAttributeValue when a custom element attribute is declared as a boolean parameter but the attribute string is not one of the accepted literals ('true','True','false','False'). Blazor custom elements coerce HTML attribute strings (which are always strings) into the .NET parameter type, and a boolean parameter only accepts those four spellings.","triggerScenarios":"Setting a boolean [Parameter] via an HTML attribute with an unsupported value: <my-component enabled=\"1\"/>, enabled=\"yes\", enabled=\"\", or enabled=\"on\". The attributeChangedCallback routes the value to parseAttributeValue which hits the 'boolean' case default branch.","commonSituations":"Writing attributes by hand with truthy shorthand ('1'/'yes'), passing JSON-style values, copy-pasting from a different component that accepted numbers, or templating engines that emit presence-only attributes as empty strings.","solutions":["Use one of the accepted spellings: 'true'/'True'/'false'/'False' (see BlazorCustomElements.ts:113-118).","If you need a nullable boolean, declare the parameter as 'boolean?' so an empty string is treated as null instead of throwing.","Set the value as a JS property instead of an attribute: element.enabled = true; (this bypasses string parsing).","Coerce in your templating layer before emitting the attribute, emitting 'true'/'false' literally."],"exampleFix":"// before\n<my-counter enabled=\"1\"></my-counter>\n\n// after\n<my-counter enabled=\"true\"></my-counter>","handlingStrategy":"validation","validationCode":"const BOOL_LITERALS = new Set(['true', 'True', 'false', 'false'] as const);\nconst BOOL_OK = new Set(['true', 'True', 'false', 'False']);\nfunction isValidBoolAttr(v: string): boolean {\n  return BOOL_OK.has(v);\n}\n\n// before setting:\nif (!isValidBoolAttr(value)) {\n  throw new Error(`Refusing to set non-boolean attribute: ${value}`);\n}\nel.setAttribute('enabled', value);","typeGuard":"function asBoolAttr(v: unknown): 'true' | 'True' | 'false' | 'False' | null {\n  return v === 'true' || v === 'True' || v === 'false' || v === 'False' ? (v as any) : null;\n}","tryCatchPattern":"try {\n  el.setAttribute('enabled', raw);\n} catch (e) {\n  if (/Invalid boolean value/.test((e as Error).message)) {\n    console.warn('Skipping invalid boolean attribute', raw);\n  } else throw e;\n}","preventionTips":["Always emit 'true'/'false' literally from templating layers.","For tri-state, declare the .NET parameter as bool? so empty maps to null.","Set booleans via the JS property (element.enabled = true) to bypass string parsing.","Add a unit test that feeds your template's output through the accepted-literal set."],"tags":["blazor-custom-elements","type-coercion","attributes","validation"],"analyzedSha":"294cab2f9b2e03af6b953820c7ab497c3c8b7ad9","analyzedAt":"2026-08-06T20:08:02.189Z","schemaVersion":2},"datasetVersion":"2026-08-06T23:17:07.152Z"}