Skip to content

Conversation

@throuxel
Copy link
Member

@throuxel throuxel commented Jan 7, 2026

Proposed changes

  • Allow pydantic to parse number as str

Related issues

Checklist

  • I consider the submitted work as finished
  • I have signed my commits using GPG key.
  • I tested the code for its functionality using different use cases
  • I added/update the relevant documentation (either on github or on notion)
  • Where necessary I refactored code to improve the overall quality

Further comments

@throuxel throuxel self-assigned this Jan 7, 2026
@throuxel throuxel added bug use for describing something not working as expected filigran team use to identify PR from the Filigran team tiny use to identify tiny feature to develop or bug to fix (< a week to solve) connector: misp labels Jan 7, 2026
@throuxel throuxel linked an issue Jan 7, 2026 that may be closed by this pull request
Copy link
Member

@Megafredo Megafredo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enabling coerce_numbers_to_str in the MISPBaseModel class resolves most of the type errors listed in the issue:

✅ Cases that work correctly with coerce_numbers_to_str=True:
From ExtendedEvent(MISPBaseModel):

  • Event.id → ok
  • Event.org_id → ok
  • Event.orgc_id → ok
  • Event.attribute_count → ok
  • Event.timestamp → ok
  • Event.sharing_group_id → ok
  • Event.publish_timestamp → ok

From EventOrganization(MISPBaseModel):

  • Event.Org.id → ok
  • Event.Orgc.id → ok

⚠️ Cases that do not work with coerce_numbers_to_str=True:
When the field is a StrEnum and not simply a str, coerce_numbers_to_str is not sufficient to accept an int as a value:

From ExtendedEvent(MISPBaseModel):

  • Event.distribution → fails
  • Event.analysis → fails
  • Event.threat_level_id → fails

Typical error example:
(distribution) Input should be '0', '1', '2', '3', '4' or '5' [type=enum, input_value=1, input_type=int]
(analysis) Input should be '0', '1' or '2' [type=enum, input_value=1, input_type=int]
(threat_level_id) Input should be '1', '2', '3' or '4' [type=enum, input_value=3, input_type=int]

In addition, we already have coerce_numbers_to_str=True for these 3 fields in their definition:

distribution: Optional[DistributionLevelId] = Field(default=None, coerce_numbers_to_str=True) 
analysis: Optional[AnalysisLevelId] = Field(default=None, coerce_numbers_to_str=True) 
threat_level_id: Optional[ThreatLevelId] = Field(default=None, coerce_numbers_to_str=True)

It seems that coerce_numbers_to_str only applies to fields of the classic str type. For these three fields, it will be necessary to add a field_validator in mode="before" in order to automatically convert int values to str before validating the enum.

@throuxel throuxel force-pushed the feat/5054-misp-fix-pydantic-errors branch from 3a8413d to 128cdb3 Compare January 9, 2026 14:20
@throuxel throuxel requested a review from Megafredo January 9, 2026 14:20
@SamuelHassine SamuelHassine force-pushed the master branch 2 times, most recently from a35f198 to 34b8e57 Compare January 9, 2026 14:33
@throuxel throuxel force-pushed the feat/5054-misp-fix-pydantic-errors branch from 128cdb3 to fb4a69b Compare January 9, 2026 14:36
Copy link
Member

@Megafredo Megafredo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fine with me! Good job!

@throuxel throuxel merged commit f9b25ee into master Jan 9, 2026
5 checks passed
@throuxel throuxel deleted the feat/5054-misp-fix-pydantic-errors branch January 9, 2026 14:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug use for describing something not working as expected connector: misp filigran team use to identify PR from the Filigran team tiny use to identify tiny feature to develop or bug to fix (< a week to solve)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[MISP] Pydantic errors

3 participants