fix: raise ValidationError for invalid object and menu files

Merged
OG
2mo ago
+197-18

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 with kind: Menu, or a menu file with kind: Object, raises a plain ValueError. Every other content problem in the same method raises the SDK ValidationError.
  • Unknown kind or apiVersion, or a missing spec: these raise a pydantic ValidationError, as does an invalid menu spec.

Callers that handle file problems catch only the SDK ValidationError, so these mistakes reach them as unexpected errors:

  • infrahubctl: object load, object validate, menu load and menu validate stop 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 plain ValueError cannot be handled the same way without also hiding real bugs, so a wrong kind is still logged with a traceback. With this change, the import log reads File 'objects/devices.yml': File is not an Infrahub Object file with 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 --file fails 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 infrahubctl error handler has no case for ValidationError, so commands that do not catch it themselves, such as object update --file, still show a traceback for an invalid file, as they did before.
  • MenuFile duplicates most of ObjectFile. That refactor belongs to IHS-124.

Closes IHS-301

What changed

Behavioral changes:

  • ObjectFile.validate_content() and MenuFile.validate_content() raise the SDK ValidationError for a kind of the other file type. The message text is unchanged.
  • InfrahubFile.validate_content(), and the spec parsing in ObjectFile and MenuFile, raise the SDK ValidationError for an unknown kind or apiVersion, a missing spec, or a spec that does not match the format.
  • Each problem is one line in ValidationError.messages, in the form path | reason, received value (type), for example kind | Input should be 'Menu' or 'Object', received 'Schema' (enum). Infrahub joins these lines with ; , so its import log stays on one line.
  • infrahubctl now 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 the ValidationError from 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 reading spec before validate_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 as 2: or off:, no longer raises TypeError.

What stayed the same: no change to the file format, the schema or the GraphQL API.

How to review

  1. infrahub_sdk/yaml.py: the shared error format in _content_error().
  2. infrahub_sdk/spec/object.py and infrahub_sdk/spec/menu.py: the kind checks and the spec parsing.
  3. infrahub_sdk/ctl/utils.py: the escaping in display_object_validate_format_error().
  4. changelog/+object-menu-file-validation-error.changed.md: this is filed as changed because 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 ValueError or the pydantic ValidationError around these methods must catch infrahub_sdk.exceptions.ValidationError instead. The SDK ValidationError subclasses 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() and MenuFile.validate_content() raise ValidationError for a kind of the other file type, keeping the existing message text.
  • Spec parsing in InfrahubFile, ObjectFile, and MenuFile converts 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 raise TypeError.

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.