diff --git a/packages/onebot/src/event-converter/element-codecs.ts b/packages/onebot/src/event-converter/element-codecs.ts index 53b5a9e8..ce3dde85 100644 --- a/packages/onebot/src/event-converter/element-codecs.ts +++ b/packages/onebot/src/event-converter/element-codecs.ts @@ -292,9 +292,18 @@ export const ELEMENT_CODECS = { return { type: 'json', data: { data: element.text ?? '' } }; }, async fromSegment(data) { + const text = data.data; + if (typeof text !== 'string' || !text.trim()) { + throw new MessageElementValidationError( + 'INVALID_FIELD', + 'message segment "json" field "data" must be a non-empty JSON string', + 'json', + 'data', + ); + } return { type: 'json', - text: String(data.data ?? ''), + text, }; }, }, diff --git a/packages/onebot/tests/message-action-validation.test.ts b/packages/onebot/tests/message-action-validation.test.ts index 773a058a..52626984 100644 --- a/packages/onebot/tests/message-action-validation.test.ts +++ b/packages/onebot/tests/message-action-validation.test.ts @@ -70,4 +70,28 @@ describe('outbound message validation at the Action boundary', () => { }); expect(bridgeSend).not.toHaveBeenCalled(); }); + + it('reports the public data field when a JSON segment uses data.text', async () => { + const bridgeSend = vi.fn(); + const ref = instanceContext(bridgeSend); + const api = new ApiHandler({ + sendGroupMessage: (groupId, message, autoEscape) => + sendGroupMessage(ref, groupId, message, autoEscape), + } as ApiActionContext); + + const response = await api.handle('send_group_msg', { + group_id: 12345, + message: [{ + type: 'json', + data: { text: '{"app":"com.tencent.contact.lua"}' }, + }], + }); + + expect(response).toMatchObject({ + status: 'failed', + retcode: 1400, + wording: 'message segment "json" field "data" must be a non-empty JSON string', + }); + expect(bridgeSend).not.toHaveBeenCalled(); + }); });