{"record":{"id":"f4121914844d8242","repo":"passbolt/passbolt_api","slug":"could-not-validate-comment-data","errorCode":null,"errorMessage":"Could not validate comment data.","messagePattern":"Could not validate comment data\\.","errorType":"http","errorClass":"BadRequestException","httpStatus":400,"severity":"error","filePath":"src/Service/Comments/CommentsAddService.php","lineNumber":102,"sourceCode":"     * @throws \\Cake\\Http\\Exception\\BadRequestException\n     * @throws \\Cake\\Http\\Exception\\NotFoundException\n     * @return void\n     */\n    protected function _handleValidationErrors(Comment $comment): void\n    {\n        $errors = $comment->getErrors();\n        if (!empty($errors)) {\n            if (\n                !empty($errors['foreign_key']) &&\n                (\n                    !empty($errors['foreign_key']['resource_exists']) ||\n                 !empty($errors['foreign_key']['resource_is_soft_deleted']) ||\n                 !empty($errors['foreign_key']['has_resource_access'])\n                )\n            ) {\n                throw new NotFoundException(__('The resource does not exist.'));\n            }\n            throw new BadRequestException(__('Could not validate comment data.'));\n        }\n    }\n\n    /**\n     * Build and validate comment entity from user input.\n     *\n     * @param \\App\\Utility\\UserAccessControl $uac The user access control\n     * @param string $foreignKey The identifier of the instance the comment belongs to.\n     * @param array $data The comment data\n     * @return \\App\\Model\\Entity\\Comment $comment comment entity\n     */\n    protected function _buildAndValidateCommentEntity(UserAccessControl $uac, string $foreignKey, array $data): Comment\n    {\n        // Build entity and perform basic check.\n        /**\n         * @var \\App\\Model\\Entity\\Comment $comment\n         */\n        $comment = $this->Comments->newEntity(","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/passbolt/passbolt_api/blob/31c1bbc10f32808a607fa9bd81891e898779c0bc/src/Service/Comments/CommentsAddService.php#L84-L120","documentation":"The fallback error of CommentsAddService::_handleValidationErrors(): the comment entity failed validation for a reason other than the foreign_key resource rules (e.g. missing/invalid parent_id, empty content, invalid user_id). Since it isn't the 'resource does not exist' case, a 400 BadRequest is returned instead of 404.","triggerScenarios":"POSTing a comment with empty content, invalid parent comment id (reply-to not a UUID or not a comment of the same resource), missing required fields, or extra invalid fields in the payload.","commonSituations":"Building reply comments with the wrong parent_id (parent belongs to a different resource); sending content as null/absent; client sending legacy field names after an API change; whitespace-only content.","solutions":["Inspect the response body errors array for the exact failing field, then fix that field in the payload.","Ensure content is a non-empty string and parent_id (if replying) is a valid comment UUID belonging to the same resource.","Send only the documented fields (parent_id, content) in the POST body."],"exampleFix":"// before\nPOST {\"parent_id\": \"<comment-of-another-resource>\", \"content\": \"\"}\n// after\nPOST {\"content\": \"My comment text\"}   // drop parent_id, or use a valid comment id of the same resource","handlingStrategy":"validation","validationCode":"const errors = [];\nif (!data.content || typeof data.content !== 'string' || !data.content.trim()) errors.push('content is required');\nif (data.parent_id && !isUuid(data.parent_id)) errors.push('parent_id must be a UUID');\nif (errors.length) throw new Error(errors.join('; '));","typeGuard":null,"tryCatchPattern":"try { await addComment(resourceId, data); }\ncatch (e) { if (e.response?.status === 400) showFieldErrors(e.response.data.errors ?? {}); else throw e; }","preventionTips":["Read the errors array in the 400 response — it names the exact failing field","Keep payloads to documented fields (content, parent_id)","For replies, use a parent comment id from the same resource"],"tags":["validation","http-400","comments"],"backgroundTag":"schema-validation-failed","analyzedSha":"31c1bbc10f32808a607fa9bd81891e898779c0bc","analyzedAt":"2026-09-17T00:04:38.960Z","contentChangedAt":"2026-09-17T00:04:38.960Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}