{"record":{"id":"396128877e529fb2","repo":"ZhuLinsen/daily_stock_analysis","slug":"market-must-be-one-of-cn-hk-us-jp-kr-tw","errorCode":null,"errorMessage":"market must be one of cn, hk, us, jp, kr, tw","messagePattern":"market must be one of cn, hk, us, jp, kr, tw","errorType":"validation","errorClass":"ValueError","httpStatus":400,"severity":"error","filePath":"src/services/decision_signal_service.py","lineNumber":1137,"sourceCode":"        return code\n\n    @staticmethod\n    def _normalize_hk_stock_code(value: str) -> str:\n        normalized = canonical_stock_code(normalize_stock_code(value))\n        digits = \"\"\n        if normalized.startswith(\"HK\"):\n            digits = normalized[2:]\n        elif normalized.isdigit():\n            digits = normalized\n        if digits.isdigit() and 1 <= len(digits) <= 5:\n            return f\"HK{digits.zfill(5)}\"\n        return normalized\n\n    @staticmethod\n    def _normalize_market(value: Any) -> str:\n        market = str(value or \"\").strip().lower()\n        if market not in VALID_MARKETS:\n            raise ValueError(\"market must be one of cn, hk, us, jp, kr, tw\")\n        return market\n\n    @classmethod\n    def _normalize_optional_market(cls, value: Any) -> Optional[str]:\n        if value in (None, \"\"):\n            return None\n        return cls._normalize_market(value)\n\n    @staticmethod\n    def _normalize_action(value: Any) -> str:\n        action = str(value or \"\").strip().lower()\n        if not action or action not in DECISION_ACTIONS:\n            raise ValueError(\"action must be one of buy/add/hold/reduce/sell/watch/avoid/alert\")\n        return action\n\n    @classmethod\n    def _normalize_optional_action(cls, value: Any) -> Optional[str]:\n        if value in (None, \"\"):","sourceCodeStart":1119,"sourceCodeEnd":1155,"githubUrl":"https://github.com/ZhuLinsen/daily_stock_analysis/blob/5159bd72e8373d215492dff122acc9d389e219c9/src/services/decision_signal_service.py#L1119-L1155","documentation":"ValueError from DecisionSignalService._normalize_market (src/services/decision_signal_service.py:1137): the market field is lowercased/stripped and must be one of VALID_MARKETS = {cn, hk, us, jp, kr, tw}. Anything else — including localized names like 'A股', 'china', exchange names, or case/typo variants not fixed by lowercasing — is rejected.","triggerScenarios":"create/update signal payloads with market: 'zh', 'CN ' (ok after trim/lower → 'cn'), 'a_share', 'sh', 'hongkong', 'USA', 'japan', 'TSE', or None/'' when market is required. _normalize_optional_market tolerates None but any non-empty wrong value hits this error.","commonSituations":"Mapping from a provider that uses ISO country codes (CHN/HKG/USA) or exchange codes (SH/SZ/HKEX/NASDAQ) directly to market; locale-dependent labels; adding a new market to the enum without updating callers; data pipelines forwarding the stock's currency ('CNY'/'HKD') as market.","solutions":["Map provider values to the six supported tokens before the call (CHN→cn, HKG/hkex→hk, USA/NASDAQ→us, JPY/japan→jp, KRW/korea→kr, TAIEX→tw).","Use _normalize_optional_market / the API's optional field when market may be absent instead of sending placeholder strings.","If you genuinely need an unsupported market, extend VALID_MARKETS and update docs/tests — do not bypass the check.","Add a client-side enum guard (see validationCode)."],"exampleFix":"# before\nservice.create_signal({\"stock_code\": \"00700\", \"market\": \"HKG\", \"action\": \"buy\"})  # ValueError\n\n# after\nMARKET_MAP = {\"CHN\": \"cn\", \"HKG\": \"hk\", \"USA\": \"us\"}\nmarket = MARKET_MAP.get(provider_market, provider_market.strip().lower())\nservice.create_signal({\"stock_code\": \"00700\", \"market\": \"hk\", \"action\": \"buy\"})","handlingStrategy":"type-guard","validationCode":"VALID_MARKETS = {'cn', 'hk', 'us', 'jp', 'kr', 'tw'}\nmarket = str(payload.get('market') or '').strip().lower()\nif market not in VALID_MARKETS:\n    raise ValueError(f'market must be one of {sorted(VALID_MARKETS)}')","typeGuard":"def is_valid_market(v) -> bool:\n    return str(v or '').strip().lower() in {'cn', 'hk', 'us', 'jp', 'kr', 'tw'}","tryCatchPattern":null,"preventionTips":["Translate provider codes (CHN/HKG/USA/exchange names) to the six tokens at the ingestion boundary.","Send market omitted rather than a placeholder string when unknown.","Extend VALID_MARKETS in the library itself if a new market is genuinely needed."],"tags":["decision-signal","validation","enum","market"],"backgroundTag":null,"analyzedSha":"5159bd72e8373d215492dff122acc9d389e219c9","analyzedAt":"2026-08-15T01:59:36.292Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}