{"record":{"id":"659c8301a10523a0","repo":"dotnet/aspnetcore","slug":"invalid-number-value-attributevalue-for-param","errorCode":null,"errorMessage":"Invalid number value '${attributeValue}' for parameter '${parameterName}'","messagePattern":"Invalid number value '(.+?)' for parameter '(.+?)'","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/Components/CustomElements/src/js/BlazorCustomElements.ts","lineNumber":125,"sourceCode":"  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  }\n}\n\nfunction dasherize(value: string): string {\n  return camelCase(value).replace(/([A-Z])/g, \"-$1\").toLowerCase();\n}","sourceCodeStart":107,"sourceCodeEnd":143,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/294cab2f9b2e03af6b953820c7ab497c3c8b7ad9/src/Components/CustomElements/src/js/BlazorCustomElements.ts#L107-L143","documentation":"Thrown by parseAttributeValue in the 'number' case when Number(attributeValue) returns NaN. Custom elements can only receive primitives via attributes; numeric parameters must be parseable by JavaScript's Number() constructor.","triggerScenarios":"Setting a numeric [Parameter] via an HTML attribute with a non-numeric value: <my-component count=\"abc\"/>, count=\"12px\", count=\"1.2.3\", or count=\"\" (for a non-nullable number). The empty string for a non-nullable number parses to 0 normally, but values like '' actually pass Number('')===0; the failing cases are genuinely non-numeric like 'abc'.","commonSituations":"Templating engines interpolating a non-number variable, localized number formats with commas ('1,000'), units appended ('5px'), or passing a number-like object's toString() that is not a clean number.","solutions":["Ensure the attribute value is a valid numeric literal that Number() accepts (e.g. '42', '3.14', '-1').","Declare the parameter as 'number?' if an empty/null representation is needed (empty string yields null, not a throw).","Set the value as a JS property to bypass string parsing: element.count = 42;","Sanitize/format the value in your templating/binding layer before emitting it as an attribute."],"exampleFix":"// before\n<my-grid page-size=\"1,000\"></my-grid>\n\n// after\n<my-grid page-size=\"1000\"></my-grid>","handlingStrategy":"validation","validationCode":"function isValidNumberAttr(v: string): boolean {\n  return v.trim() !== '' && !Number.isNaN(Number(v));\n}\n\nif (!isValidNumberAttr(raw)) {\n  throw new Error(`Refusing to set non-numeric attribute: ${raw}`);\n}\nel.setAttribute('count', raw);","typeGuard":"function asNumberAttr(v: unknown): number | null {\n  if (typeof v !== 'string' || v.trim() === '') return null;\n  const n = Number(v);\n  return Number.isNaN(n) ? null : n;\n}","tryCatchPattern":"try {\n  el.setAttribute('count', raw);\n} catch (e) {\n  if (/Invalid number value/.test((e as Error).message)) {\n    el.count = Number(raw.replace(/[^0-9.\\-]/g, '')) || 0; // fallback via property\n  } else throw e;\n}","preventionTips":["Strip thousands separators and units before emitting numeric attributes.","Use 'number?' for optional numeric parameters to allow empty/null.","Set numbers via the JS property to avoid string parsing.","Validate with Number.isNaN(Number(value)) in your binding layer."],"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"}