{"record":{"id":"33b1480733be908b","repo":"mermaid-js/mermaid","slug":"context-must-be-between-0-1-decimal-or-0-100","errorCode":null,"errorMessage":"${context} must be between 0-1 (decimal) or 0-100 (percentage). Received: ${value}","messagePattern":"(.+?) must be between 0-1 \\(decimal\\) or 0-100 \\(percentage\\)\\. Received: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/mermaid/src/diagrams/wardley/wardleyParser.ts","lineNumber":12,"sourceCode":"import type { Wardley } from '@mermaid-js/parser';\nimport { parse } from '@mermaid-js/parser';\nimport type { ParserDefinition } from '../../diagram-api/types.js';\nimport { log } from '../../logger.js';\nimport { populateCommonDb } from '../common/populateCommonDb.js';\nimport type { WardleyDB } from './wardleyTypes.js';\n\nconst toPercent = (value: number, context: string): number => {\n  // Accept values in 0-1 range (converted to percentage) or 0-100 range (used as-is)\n  const normalized = value <= 1 ? value * 100 : value;\n  if (normalized < 0 || normalized > 100) {\n    throw new Error(\n      `${context} must be between 0-1 (decimal) or 0-100 (percentage). Received: ${value}`\n    );\n  }\n  return normalized;\n};\n\nconst toCoordinates = (\n  visibility: number,\n  evolution: number,\n  context: string\n): { x: number; y: number } => {\n  return {\n    x: toPercent(evolution, `${context} evolution`),\n    y: toPercent(visibility, `${context} visibility`),\n  };\n};\n\nconst getFlowFromPort = (port?: string): 'forward' | 'backward' | 'bidirectional' | undefined => {","sourceCodeStart":1,"sourceCodeEnd":30,"githubUrl":"https://github.com/mermaid-js/mermaid/blob/d93e9c88c01a599c062ee6a3f1462e3558ac6b90/packages/mermaid/src/diagrams/wardley/wardleyParser.ts#L1-L30","documentation":"toPercent() normalises wardley coordinate values: a value <= 1 is treated as a decimal and multiplied by 100; anything else is treated as a percentage and used as-is. If the resulting number falls outside [0,100], it throws naming the context (e.g. component/visibility/evolution) and the offending raw value. Both visibility and evolution pass through it.","triggerScenarios":"Supplying visibility or evolution values that are negative, greater than 100, or in an ambiguous range; e.g. `[-5, 0.5]`, `[0.5, 150]`, or `[2, 0.8]` (2 is >1 so treated as 2%, valid, but surprises users who meant 0..1). Note values like 1.5 are treated as percentages (1.5%) because only <=1 scales.","commonSituations":"Unit confusion — mixing decimal (0..1) and percentage (0..100) in one diagram; off-by-100 typos; copying coordinates from a tool that uses a 0..255 or 0..1 scale differently; negative margins.","solutions":["Pick one unit per diagram and stick to it: either all decimals 0..1 or all percentages 0..100.","Clamp the offending value into [0,1] or [0,100] according to your chosen unit.","Avoid values in (1, 100] if you intended decimals — they are read as percentages.","Validate coordinates upstream against the same rule before rendering."],"exampleFix":"// before\ncomponent Foo [150, 0.8]  // 150% -> throws\n\n// after (percentage unit)\ncomponent Foo [80, 50]\n// or (decimal unit)\ncomponent Foo [0.8, 0.5]","handlingStrategy":"validation","validationCode":"// Validate wardley coordinates with the same rule the parser uses\nfunction toPercent(value, context) {\n  const normalized = value <= 1 ? value * 100 : value;\n  if (normalized < 0 || normalized > 100) {\n    throw new Error(`${context} out of range: ${value}`);\n  }\n  return normalized;\n}\n// pick one unit and validate all coordinates before rendering","typeGuard":"const isValidCoordinate = (v: number): boolean => {\n  const n = v <= 1 ? v * 100 : v;\n  return Number.isFinite(v) && n >= 0 && n <= 100;\n};","tryCatchPattern":"try {\n  await mermaid.run({ nodes: [el] });\n} catch (e) {\n  if (e instanceof Error && /must be between 0-1/.test(e.message)) {\n    // read the bad value from the message and clamp/convert it\n  } else { throw e; }\n}","preventionTips":["Choose one unit (decimal 0..1 or percent 0..100) per diagram.","Avoid values in (1, 100] if you mean decimals — they read as percentages.","Clamp coordinates upstream before rendering."],"tags":["wardley","coordinates","range","validation","units"],"backgroundTag":null,"analyzedSha":"d93e9c88c01a599c062ee6a3f1462e3558ac6b90","analyzedAt":"2026-08-12T06:23:11.304Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}