{"record":{"id":"bfb25273bd81648d","repo":"passbolt/passbolt_api","slug":"could-not-validate-the-metadata-key-data","errorCode":null,"errorMessage":"Could not validate the metadata key data.","messagePattern":"Could not validate the metadata key data\\.","errorType":"validation","errorClass":"FormValidationException","httpStatus":400,"severity":"error","filePath":"plugins/PassboltCe/Metadata/src/Controller/MetadataKeyCreateController.php","lineNumber":40,"sourceCode":"use Passbolt\\Metadata\\Model\\Dto\\MetadataKeyCreateDto;\nuse Passbolt\\Metadata\\Service\\MetadataKeyCreateService;\n\nclass MetadataKeyCreateController extends AppController\n{\n    /**\n     * Metadata key save action.\n     *\n     * @return void\n     */\n    public function create()\n    {\n        $this->assertJson();\n        $this->User->assertIsAdmin();\n        $this->assertNotEmptyArrayData();\n\n        $form = new MetadataKeyCreateForm();\n        if (!$form->execute($this->getRequest()->getData())) {\n            throw new FormValidationException(__('Could not validate the metadata key data.'), $form);\n        }\n\n        $dto = MetadataKeyCreateDto::fromArray($form->getData());\n        $uac = $this->User->getAccessControl();\n        $metadataKey = (new MetadataKeyCreateService())->create($uac, $dto);\n\n        $this->success(__('The operation was successful.'), $metadataKey);\n    }\n}\n","sourceCodeStart":22,"sourceCodeEnd":50,"githubUrl":"https://github.com/passbolt/passbolt_api/blob/31c1bbc10f32808a607fa9bd81891e898779c0bc/plugins/PassboltCe/Metadata/src/Controller/MetadataKeyCreateController.php#L22-L50","documentation":"MetadataKeysController.create validates the POST payload with MetadataKeyCreateForm; when the form fails execution (missing/invalid fields such as armored public key, key info, or expired metadata), it throws this BadRequestException carrying the form's nested validation errors.","triggerScenarios":"POST /metadata/keys with an absent or malformed body, missing 'armored_key'/'key_info' fields, non-admin caller body issues, or a payload that fails MetadataKeyCreateForm rules.","commonSituations":"API clients sending v4-style key payloads to the v5 metadata keys endpoint; missing Content-Type: application/json so getData() returns empty; automated scripts posting incomplete payloads.","solutions":["Inspect the nested form errors in the exception to see which fields failed and fix the payload.","Ensure the request body is valid JSON with required keys (e.g. armored_key, key_info) and Content-Type: application/json.","Verify the user is authenticated as admin before calling the endpoint.","Confirm the key data matches v5 metadata key requirements (valid OpenPGP armored key)."],"exampleFix":"// before\nawait fetch('/metadata/keys', {method:'POST'});\n// after\nawait fetch('/metadata/keys', {method:'POST', headers:{'Content-Type':'application/json'}, body: JSON.stringify({armored_key: pubArmored, key_info: {fingerprint, subkeys}})});","handlingStrategy":"validation","validationCode":"const body = {armored_key, key_info};\nif (!body.armored_key || !body.key_info?.fingerprint) throw new Error('metadata key payload incomplete');","typeGuard":"const isMetadataKeyPayload = (b) => typeof b === 'object' && b !== null && typeof b.armored_key === 'string' && b.armored_key.startsWith('-----BEGIN PGP PUBLIC KEY BLOCK-----');","tryCatchPattern":"try { await api.post('/metadata/keys', body); } catch (e) { if (e.response?.status === 400 && e.response?.data?.body?.metadata_key?.errors) { handleFormErrors(e.response.data.body.metadata_key.errors); } else throw e; }","preventionTips":["Always send Content-Type: application/json with a full payload.","Read the formErrors returned in 400 responses.","Use the official passbolt SDK helpers to build metadata key payloads.","Test payloads against a development instance first."],"tags":["validation","cakephp","form-validation","api"],"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"}