{"record":{"id":"65e349beaac0bf31","repo":"alibaba/nacos","slug":"22000","errorCode":"22000","errorMessage":"namespaceId [{namespaceId}] mismatch the pattern","messagePattern":"namespaceId \\[(.+?)\\] mismatch the pattern","errorType":"http","errorClass":"NacosApiException","httpStatus":400,"severity":"warning","filePath":"core/src/main/java/com/alibaba/nacos/core/controller/v3/NamespaceControllerV3.java","lineNumber":130,"sourceCode":"    @Since(\"3.0.0\")\n    @PostMapping\n    @Secured(resource = Commons.NACOS_ADMIN_CORE_CONTEXT_V3\n        + \"namespaces\", action = ActionTypes.WRITE, signType = SignType.CONSOLE,\n        apiType = ApiType.ADMIN_API)\n    public Result<Boolean> createNamespace(NamespaceForm namespaceForm) throws Exception {\n        namespaceForm.validate();\n        \n        String namespaceId = namespaceForm.getNamespaceId();\n        String namespaceName = namespaceForm.getNamespaceName();\n        String namespaceDesc = namespaceForm.getNamespaceDesc();\n        \n        if (StringUtils.isBlank(namespaceId)) {\n            namespaceId = UUID.randomUUID().toString();\n        } else {\n            // TODO check should be parameter check filter.\n            namespaceId = namespaceId.trim();\n            if (!namespaceIdCheckPattern.matcher(namespaceId).matches()) {\n                throw new NacosApiException(HttpStatus.BAD_REQUEST.value(),\n                    ErrorCode.ILLEGAL_NAMESPACE,\n                    \"namespaceId [\" + namespaceId + \"] mismatch the pattern\");\n            }\n            if (namespaceId.length() > NAMESPACE_ID_MAX_LENGTH) {\n                throw new NacosApiException(HttpStatus.BAD_REQUEST.value(),\n                    ErrorCode.ILLEGAL_NAMESPACE,\n                    \"too long namespaceId, over \" + NAMESPACE_ID_MAX_LENGTH);\n            }\n        }\n        // contains illegal chars\n        if (!namespaceNameCheckPattern.matcher(namespaceName).matches()) {\n            throw new NacosApiException(HttpStatus.BAD_REQUEST.value(), ErrorCode.ILLEGAL_NAMESPACE,\n                \"namespaceName [\" + namespaceName + \"] contains illegal char\");\n        }\n        return Result.success(\n            namespaceOperationService.createNamespace(namespaceId, namespaceName, namespaceDesc));\n    }\n    ","sourceCodeStart":112,"sourceCodeEnd":148,"githubUrl":"https://github.com/alibaba/nacos/blob/9b989acdf181d00898f2e8839257bb2b2a3cefe3/core/src/main/java/com/alibaba/nacos/core/controller/v3/NamespaceControllerV3.java#L112-L148","documentation":"Thrown on namespace creation (POST) when a user-supplied namespaceId does not match the allowed pattern ^[\\\\w-]+ (word characters [A-Za-z0-9_] and hyphens). NacosApiException ErrorCode.ILLEGAL_NAMESPACE (code 22000), HTTP 400. Note: the pattern is applied after a trim().","triggerScenarios":"Submitting a custom namespaceId containing spaces, dots, slashes, colons, @, or other non-word/non-hyphen characters. The regex is matched with Matcher.matches() which requires the ENTIRE string to conform (anchored by ^, though note the pattern lacks a trailing $ anchor which is a known quirk).","commonSituations":"Operator wants a 'dev.env' or 'tenant:a' namespace id; copying an id containing dots/colons from another system; trailing/leading characters that the pattern rejects.","solutions":["Restrict namespaceId to letters, digits, underscore, and hyphen only (e.g. 'dev-env', 'tenant_1').","Omit the namespaceId entirely to let Nacos generate a UUID.","If you need dots/colons, that is intentionally disallowed — redesign the id scheme."],"exampleFix":"// before\nPOST /v3/admin/core/namespace\n{\"customNamespaceId\":\"dev.env\"}\n\n// after\nPOST /v3/admin/core/namespace\n{\"customNamespaceId\":\"dev-env\"}","handlingStrategy":"validation","validationCode":"private static final Pattern ID = Pattern.compile(\"^[\\\\\\\\w-]+$\");\nif (namespaceId != null && !ID.matcher(namespaceId.trim()).matches()) {\n    // reject before calling the create API\n}","typeGuard":null,"tryCatchPattern":"try {\n    client.createNamespace(form);\n} catch (NacosApiException e) {\n    if (e.getErrCode() == ErrorCode.ILLEGAL_NAMESPACE.getCode()) { /* fix the id */ }\n}","preventionTips":["Restrict namespaceId to [A-Za-z0-9_-] only.","Omit the id to use an auto-generated UUID."],"tags":["namespace","validation","illegal-namespace","api","bad-request","regex"],"backgroundTag":null,"analyzedSha":"9b989acdf181d00898f2e8839257bb2b2a3cefe3","analyzedAt":"2026-08-14T07:17:31.569Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}