fix: raise ValidationError for invalid object and menu files
Why
When an object file or a menu file contains a mistake, users should see which file is wrong and why. Today, some mistakes end in a Python traceback instead:
- Wrong
kind: an object file withkind: Menu, or a menu file withkind: Object, raises a plainValueError. Every other content problem in the same method raises the SDKValidationError. - Unknown
kindorapiVersion, or a missingspec: these raise a pydanticValidationError, as does an invalid menuspec.
Callers that handle file problems catch only the SDK ValidationError, so these mistakes reach them as unexpected errors:
infrahubctl:object load,object validate,menu loadandmenu validatestop with a traceback instead of reporting the file as not valid.- Infrahub Git import: IFC-3304 (opsmill/infrahub#10890) logs each failure caused by repository content as one readable line, and it already handles the SDK
ValidationError. A plainValueErrorcannot be handled the same way without also hiding real bugs, so a wrongkindis still logged with a traceback. With this change, the import log readsFile 'objects/devices.yml': File is not an Infrahub Object filewith no change in Infrahub.
Goal: every problem in the content of an object or menu file raises the SDK ValidationError, with the file location and one line per problem.
Non-goals:
infrahubctl object create --filefails for every file, because it reads the file's data before it loads the file. This exists without this change and needs its own fix.- The shared
infrahubctlerror handler has no case forValidationError, so commands that do not catch it themselves, such asobject update --file, still show a traceback for an invalid file, as they did before. MenuFileduplicates most ofObjectFile. That refactor belongs to IHS-124.
Closes IHS-301
What changed
Behavioral changes:
ObjectFile.validate_content()andMenuFile.validate_content()raise the SDKValidationErrorfor akindof the other file type. The message text is unchanged.InfrahubFile.validate_content(), and the spec parsing inObjectFileandMenuFile, raise the SDKValidationErrorfor an unknownkindorapiVersion, a missingspec, or aspecthat does not match the format.- Each problem is one line in
ValidationError.messages, in the formpath | reason, received value (type), for examplekind | Input should be 'Menu' or 'Object', received 'Schema' (enum). Infrahub joins these lines with;, so its import log stays on one line. infrahubctlnow escapes file names and messages when it prints a validation error. Before, rich removed text in square brackets, and a value such as[/devices]made the error display itself crash.
Implementation notes:
InfrahubFile._content_error()builds theValidationErrorfrom a pydantic error, so all three classes produce the same format.- The spec parsing now catches only the pydantic
ValidationError, not every exception. A programming error, such as readingspecbeforevalidate_content(), is no longer reported as a problem in the user's file. - The models are built with
model_validate()instead of**content, so a top-level key that is not a string, such as2:oroff:, no longer raisesTypeError.
What stayed the same: no change to the file format, the schema or the GraphQL API.
How to review
infrahub_sdk/yaml.py: the shared error format in_content_error().infrahub_sdk/spec/object.pyandinfrahub_sdk/spec/menu.py: thekindchecks and the spec parsing.infrahub_sdk/ctl/utils.py: the escaping indisplay_object_validate_format_error().changelog/+object-menu-file-validation-error.changed.md: this is filed aschangedbecause the exception classes that the public API raises change.
The message format is the part most worth checking: Infrahub's import log and infrahubctl both show it to users.
How to test
uv run pytest tests/unit/sdk/test_yaml.py tests/unit/sdk/spec/ tests/unit/ctl/test_utils.py
To view the infrahubctl output, create objects/bad.yml with kind: Schema (or kind: Menu) and run:
uv run infrahubctl object validate objects/bad.yml
Expected: File 'objects/bad.yml' is not valid! followed by one line per problem, with no traceback.
Impact & rollout
- Backward compatibility: code that catches
ValueErroror the pydanticValidationErroraround these methods must catchinfrahub_sdk.exceptions.ValidationErrorinstead. The SDKValidationErrorsubclasses neither. Nothing in the SDK or in Infrahub depends on the old exceptions. - Deployment notes: safe to release on its own. Infrahub needs no change to show the new messages.
Checklist
- Tests added/updated
- Changelog entry added (
uv run towncrier create ...) - External docs updated (if user-facing or ops-facing change): not applicable, the docs do not describe these errors
- Internal .md docs updated (internal knowledge and AI code tools knowledge): not applicable
Assisted-by: opsmill-dev-commit 0.1.0
Assisted-by: opsmill-dev-pr 0.2.0
Summary by cubic
Object and menu file validation now raises the SDK ValidationError for every content problem, so infrahubctl and Infrahub's Git import report the file as invalid instead of stopping with a Python traceback.
Previously, a wrong kind, an unknown kind or apiVersion, a missing spec, or an invalid menu spec raised a plain ValueError or a pydantic ValidationError. These all now raise the SDK ValidationError, with the file location as identifier and one line per problem in messages, formatted as path | reason, received value (type), where the received value appears only for single string, number, or boolean inputs. infrahubctl also escapes file names and messages, so values containing square brackets no longer crash the error display. The file format, schema, and GraphQL API are unchanged.
ObjectFile.validate_content()andMenuFile.validate_content()raiseValidationErrorfor akindof the other file type, keeping the existing message text.- Spec parsing in
InfrahubFile,ObjectFile, andMenuFileconverts pydantic errors to the SDK error format and no longer catches every exception as a user-file problem. - File models are built with
model_validate(), so non-string top-level keys no longer raiseTypeError.
Migration
Code that catches ValueError or the pydantic ValidationError around validate_content() or the spec property must catch infrahub_sdk.exceptions.ValidationError instead. The SDK error subclasses neither. Nothing in the SDK or Infrahub depends on the old exception types.