Parsing a Manifest
A manifest arrives as text: a file, a request body, or a string that an extension or a management
command passes on. parse_manifest() turns that text, YAML or JSON, into a dict, and
read_manifest_file() does the same for a file. YAML is a superset of JSON, so one parser reads
both.
Parsing checks only that the text is a YAML object. Whether the object is a valid manifest, with an
apiVersion, a kind, metadata and a spec of the right types, is decided by the kind’s
Pydantic model, which reports every problem at once.
from smarter.apps.guardrail.manifest.models.guardrail.model import SAMGuardrail
from smarter.lib.manifest.parsing import read_manifest_file
data = read_manifest_file("path/to/guardrail.yaml")
manifest = SAMGuardrail.model_validate(data)
Text that is not YAML, or YAML that is not an object, e.g. a list, raises
SAMValidationError, which the api returns to the client as a 400.
Parse a manifest’s text, YAML or JSON, into the dict that its Pydantic model validates.
A manifest arrives as text: a file, a request body, or a string that an extension or a
management command passes on. This module turns that text into a dict, and nothing more.
Whether the dict is a valid manifest, i.e. whether it has an apiVersion, a kind,
metadata and a spec of the right types, is decided by the kind’s Pydantic model.
YAML is a superset of JSON, so one parser reads both.
- smarter.lib.manifest.parsing.parse_manifest(manifest)[source]
Parse a manifest’s YAML or JSON text into a dict.
data = parse_manifest(yaml_text) manifest = SAMGuardrail.model_validate(data)
- smarter.lib.manifest.parsing.read_manifest_file(file_path)[source]
Read a manifest file, YAML or JSON, into a dict.
- Parameters:
file_path (
str) – The path of the manifest file.- Return type:
- Returns:
The manifest, as a dict.
- Raises:
FileNotFoundError – If there is no such file.
SAMValidationError – If the file is not a YAML or JSON object.