{"record":{"id":"c4f2e9521c644cc9","repo":"passbolt/passbolt_api","slug":"could-not-validate-permission-data","errorCode":null,"errorMessage":"Could not validate permission data.","messagePattern":"Could not validate permission data\\.","errorType":"validation","errorClass":"ValidationException","httpStatus":400,"severity":"error","filePath":"src/Service/Permissions/PermissionsCreateService.php","lineNumber":137,"sourceCode":"            'created_by' => true,\n            'modified_by' => true,\n        ];\n\n        return $this->permissionsTable->newEntity($data, ['accessibleFields' => $accessibleFields]);\n    }\n\n    /**\n     * Handle permission validation errors.\n     *\n     * @param \\App\\Model\\Entity\\Permission $permission The permission\n     * @return void\n     */\n    private function handlePermissionValidationErrors(Permission $permission): void\n    {\n        $errors = $permission->getErrors();\n        if (!empty($errors)) {\n            $msg = __('Could not validate permission data.');\n            throw new ValidationException($msg, $permission, $this->permissionsTable);\n        }\n    }\n}\n","sourceCodeStart":119,"sourceCodeEnd":141,"githubUrl":"https://github.com/passbolt/passbolt_api/blob/31c1bbc10f32808a607fa9bd81891e898779c0bc/src/Service/Permissions/PermissionsCreateService.php#L119-L141","documentation":"ValidationException thrown by PermissionsCreateService::handlePermissionValidationErrors when a Permission entity built for saving has entity errors after validation. It centralizes the conversion of CakePHP entity validation errors into an API ValidationException carrying the entity and table context.","triggerScenarios":"Calling createPermission (e.g. from share operations or permission add endpoints) with permission data failing entity rules — invalid aro/aco type, missing aco_foreign_key, unknown permission type integer, or permission on a non-shareable resource.","commonSituations":"Clients sending permission.type outside the allowed constants (1/7/15 etc.); sharing with a user/group id that fails validation; API consumers constructing permission payloads manually with wrong field names.","solutions":["Read $exception->getErrors() (entity errors) to see which field failed and correct the payload.","Use a valid permission type constant (PermissionsTable::OWNER, EDIT, READ) instead of arbitrary integers.","Ensure aco_foreign_key references an existing resource and aro references an existing user/group.","Catch ValidationException in the controller to return structured errors to the client."],"exampleFix":"// before\n$permission = $permissionsService->createPermission($resourceId, [\n    'aro' => 'user', 'aro_foreign_key' => $userId, 'type' => 99,\n]);\n\n// after\n$type = PermissionsTable::EDITOR; // 7\n$permission = $permissionsService->createPermission($resourceId, [\n    'aro' => 'user', 'aro_foreign_key' => $userId, 'type' => $type,\n]);","handlingStrategy":"try-catch","validationCode":"// PHP\n$validTypes = [PermissionsTable::OWNER, PermissionsTable::EDIT, PermissionsTable::READ];\nif (!in_array($data['type'] ?? null, $validTypes, true) || empty($data['aro_foreign_key'])) {\n    throw new BadRequestException(__('Invalid permission payload.'));\n}","typeGuard":"function isValidPermissionPayload(array $row): bool {\n    return isset($row['aro'], $row['aro_foreign_key'], $row['type'])\n        && is_int($row['type']);\n}","tryCatchPattern":"try {\n    $permission = $service->createPermission($resourceId, $data);\n} catch (\\App\\Error\\Exception\\ValidationException $e) {\n    return $this->respondValidationError($e->getErrors()); // structured field errors\n}","preventionTips":["Only send permission type values from PermissionsTable constants.","Verify the target user/group exists before creating a permission.","Use the official share/permission endpoints instead of hand-building entities."],"tags":["permissions","validation","cakephp","entity"],"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"}