{"record":{"id":"b731e1d4ac62c968","repo":"iflytek/astron-agent","slug":"translation-text-cannot-be-empty","errorCode":null,"errorMessage":"Translation text cannot be empty","messagePattern":"Translation text cannot be empty","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"warning","filePath":"core/plugin/aitools/service/translation/translation_service.py","lineNumber":48,"sourceCode":"    TRANSLATION_AUTH_ERROR = (45255, \"翻译服务认证失败\")\n    TRANSLATION_NETWORK_ERROR = (45256, \"翻译服务网络连接失败\")\n\n\nclass TranslationInput(BaseModel):\n    \"\"\"Translation input\"\"\"\n\n    text: str  # Original text to be translated\n    target_language: str  # Target language code\n    source_language: str = (\n        CHINESE_LANGUAGE_CODE  # Source language code, default Chinese\n    )\n\n    @field_validator(\"text\")\n    @classmethod\n    def validate_text(cls, value: str) -> str:\n        \"\"\"validate text\"\"\"\n        if not value or not value.strip():\n            raise ValueError(\"Translation text cannot be empty\")\n        if len(value) > 5000:\n            raise ValueError(\"Translation text cannot exceed 5000 characters\")\n        return value\n\n    @field_validator(\"target_language\")\n    @classmethod\n    def validate_target_language(cls, value: str) -> str:\n        \"\"\"validate target language\"\"\"\n        if value not in VALID_LANGUAGE_CODES:\n            raise ValueError(\n                f\"Invalid target language: {value}.\\n\"\n                f\"Valid options: {list(VALID_LANGUAGE_CODES)}\"\n            )\n        return value\n\n    @field_validator(\"source_language\")\n    @classmethod\n    def validate_source_language(cls, value: str) -> str:","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/iflytek/astron-agent/blob/5e758547a83371a5a4b29dadf4ac03e8dd527635/core/plugin/aitools/service/translation/translation_service.py#L30-L66","documentation":"A pydantic field_validator on the TranslationInput model's `text` field raises this ValueError when the text is empty, None, or whitespace-only. Pydantic wraps it into a ValidationError during request-model construction, so the request never reaches the translation service. It is an input-schema guard ensuring the translation API has something to translate.","triggerScenarios":"Constructing TranslationInput (or POSTing to the translation endpoint) with `text` = \"\", \"   \", or None; the validator `if not value or not value.strip()` rejects both.","commonSituations":"User submits the translation form with an empty box; upstream component passes an empty string from a failed fetch; client sends JSON without the text field so pydantic coerces/defaults it; automated tests omit text.","solutions":["Provide a non-empty, non-whitespace `text` value in the request body.","Trim input client-side and reject empty input before calling the API.","Check the client's field name casing/structure matches the model so text is actually deserialized.","If text comes from another service, add a guard there to fail earlier with a clearer message."],"exampleFix":"// before\nawait translate({ text: '', source_language: 'en', target_language: 'cn' });\n// after\nconst trimmed = sourceText.trim();\nif (!trimmed) throw new Error('Please enter text to translate');\nawait translate({ text: trimmed, source_language: 'en', target_language: 'cn' });","handlingStrategy":"validation","validationCode":"function canTranslate(input) {\n  return typeof input.text === 'string' && input.text.trim().length > 0;\n}","typeGuard":"function hasTranslationText(v: unknown): v is { text: string } {\n  return typeof v === 'object' && v !== null && typeof (v as any).text === 'string' && (v as any).text.trim() !== '';\n}","tryCatchPattern":"try:\n    inp = TranslationInput(**payload)\nexcept ValidationError as e:\n    return JSONResponse(status_code=400, content={'error': e.errors()})","preventionTips":["Disable the submit button until the text area has non-whitespace content.","Trim text on the client before sending.","Surface pydantic validation errors to the user instead of logging-only."],"tags":["validation","pydantic","translation"],"backgroundTag":"schema-validation-failed","analyzedSha":"5e758547a83371a5a4b29dadf4ac03e8dd527635","analyzedAt":"2026-09-12T08:03:51.356Z","contentChangedAt":"2026-09-12T08:03:51.356Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}