{"record":{"id":"daded6c8f5b0445d","repo":"ramsey/uuid","slug":"invalid-uuid-string-encodeduuid","errorCode":null,"errorMessage":"Invalid UUID string: {encodedUuid}","messagePattern":"Invalid UUID string: (.+?)","errorType":"exception","errorClass":"InvalidUuidStringException","httpStatus":null,"severity":"error","filePath":"src/Codec/StringCodec.php","lineNumber":116,"sourceCode":"    }\n\n    /**\n     * Returns a byte string of the UUID\n     */\n    protected function getBytes(string $encodedUuid): string\n    {\n        $parsedUuid = str_replace(['urn:', 'uuid:', 'URN:', 'UUID:', '{', '}', '-'], '', $encodedUuid);\n\n        $components = [\n            substr($parsedUuid, 0, 8),\n            substr($parsedUuid, 8, 4),\n            substr($parsedUuid, 12, 4),\n            substr($parsedUuid, 16, 4),\n            substr($parsedUuid, 20),\n        ];\n\n        if (!Uuid::isValid(implode('-', $components))) {\n            throw new InvalidUuidStringException('Invalid UUID string: ' . $encodedUuid);\n        }\n\n        return (string) hex2bin($parsedUuid);\n    }\n}\n","sourceCodeStart":98,"sourceCodeEnd":122,"githubUrl":"https://github.com/ramsey/uuid/blob/da5b521600a707d2dd097598464bd3090de850f5/src/Codec/StringCodec.php#L98-L122","documentation":"StringCodec::getBytes() powers Uuid::fromString() and $codec->decode(): it strips 'urn:uuid:'/'UUID:' prefixes, braces and dashes, re-groups the remainder into five components, and validates the reassembled form with Uuid::isValid(). When validation fails it throws InvalidUuidStringException with the offending input in the message.","triggerScenarios":"Uuid::fromString() with an empty string, non-hex characters (g-z, punctuation), the wrong number of hex digits after stripping, leading/trailing whitespace or newlines, a ULID or other non-UUID identifier, or a value truncated by copy/paste or a char(32)/char(36) column.","commonSituations":"Unvalidated user/API input reaching fromString(); CSV/file lines with trailing newline; identifiers truncated by database column width; null coerced to ''; passing a 32-hex COMB string with an odd character.","solutions":["Validate first: if (!Uuid::isValid(trim($value))) { reject } - isValid accepts braces, URN and dashes exactly like fromString.","Normalize input: trim whitespace/newlines before parsing.","Reject bad IDs at the API boundary (HTTP 400 / domain error) instead of letting the exception escape.","Confirm the source wrote a full 32-hex-digit value (column wide enough, no partial reads)."],"exampleFix":"// before\n$uuid = Uuid::fromString($request->query('id')); // may throw InvalidUuidStringException\n\n// after\n$value = trim((string) $request->query('id'));\nif (!Uuid::isValid($value)) {\n    throw new NotFoundHttpException('invalid identifier');\n}\n$uuid = Uuid::fromString($value);","handlingStrategy":"validation","validationCode":"$value = trim((string) $input);\nif (!\\Ramsey\\Uuid\\Uuid::isValid($value)) {\n    throw new InvalidArgumentException(sprintf('not a valid UUID string: %s', $value));\n}\n$uuid = Uuid::fromString($value);","typeGuard":"function isParsableUuidString(mixed $value): bool\n{\n    return is_string($value) && Uuid::isValid(trim($value));\n}","tryCatchPattern":"try {\n    $uuid = Uuid::fromString($value);\n} catch (\\Ramsey\\Uuid\\Exception\\InvalidUuidStringException $e) {\n    // invalid external identifier - respond 404/400 rather than a 500\n    throw new NotFoundHttpException('unknown identifier');\n}","preventionTips":["Validate with Uuid::isValid() at API boundaries before parsing.","trim() input from files, CSVs, and config to strip newlines/spaces.","Ensure char columns are wide enough (36 with dashes) to avoid silent truncation.","Reject null/scalar coercion early: fromString('') also throws."],"tags":["php","ramsey-uuid","string-codec","uuid-parse","input-validation"],"backgroundTag":"invalid-uuid-string","analyzedSha":"da5b521600a707d2dd097598464bd3090de850f5","analyzedAt":"2026-08-21T01:35:29.252Z","schemaVersion":2},"datasetVersion":"2026-08-21T03:17:12.404Z"}