const REQUEST_ID_SCHEMA = { type: "string", description: "服务端生成的请求追踪ID", }; const ERROR_DETAILS_SCHEMA = { type: "object", additionalProperties: true, }; const ERROR_BODY_SCHEMA = { type: "object", required: ["code", "message", "details"], properties: { code: { type: "string" }, message: { type: "string" }, details: ERROR_DETAILS_SCHEMA, }, }; function buildDataEnvelopeSchema(dataSchema, description = "OK") { return { description, type: "object", required: ["requestId", "data"], properties: { requestId: REQUEST_ID_SCHEMA, data: dataSchema, }, }; } function buildErrorEnvelopeSchema({ description = "业务错误", codeExample = "VALID_001", messageExample = "参数校验失败", detailsExample, } = {}) { const schema = { description, type: "object", required: ["requestId", "error"], properties: { requestId: REQUEST_ID_SCHEMA, error: { ...ERROR_BODY_SCHEMA, properties: { ...ERROR_BODY_SCHEMA.properties, code: { ...ERROR_BODY_SCHEMA.properties.code, example: codeExample, }, message: { ...ERROR_BODY_SCHEMA.properties.message, example: messageExample, }, details: { ...ERROR_DETAILS_SCHEMA, }, }, }, }, }; if (detailsExample !== undefined) { schema.properties.error.properties.details.example = detailsExample; } return schema; } const UUID_PARAM_SCHEMA = { type: "object", required: ["id"], properties: { id: { type: "string", format: "uuid", }, }, }; module.exports = { REQUEST_ID_SCHEMA, ERROR_BODY_SCHEMA, buildDataEnvelopeSchema, buildErrorEnvelopeSchema, UUID_PARAM_SCHEMA, };