Create and load schema
Schemas are defined in YAML format and follow a strict structure. Splitting a schema into multiple smaller files is recommended to keep it maintainable as it grows.
Enable schema validation in your editor to catch errors while writing schema files.
Once a schema file is ready, you can check it against a running Infrahub instance to verify it is valid and won't break existing data, then load it to apply the changes. Schema operations should always be performed on a branch to avoid directly affecting the main branch.
Schema file​
Most schema files use some combination of these top-level keys:
---
version: '1.0'
generics:
- <new generics are defined here>
nodes:
- <new nodes are defined here>
extensions:
nodes:
- <node extensions are defined here>
For a full reference of all available properties, see Nodes & attributes, Relationships, and Generics & inheritance.
By convention, store schema files in a schemas folder at the root of your repository.
Format schema files​
infrahubctl schema format rewrites schema files with a consistent key order, so hand-edited files read the same way and produce small diffs in version control. It operates on the files on disk and does not contact a running instance.
infrahubctl schema format <path to schema file or a directory>
By default the command only reorders the keys within each node, generic, attribute, relationship, and dropdown choice. Comments, quoting, and inline sequences are preserved, and nodes in Infrahub-reserved namespaces are left unchanged.
Preview the result before writing it, or verify that files are already formatted:
infrahubctl schema format schemas/ --diff # preview the changes, write nothing
infrahubctl schema format schemas/ --check # exit non-zero if any file would change
--check lets you run the formatter in a pre-commit hook or CI step.
Three opt-in flags apply further changes and can be combined:
--strip-defaultsremoves keys whose value matches the schema default.--sort-by-order-weightorders attributes and relationships by theirorder_weight.--backfill-order-weightsetsorder_weighton entries that do not define one.
For the full list of options, see the infrahubctl schema reference.
Check a schema​
During development, you can validate a schema file and preview the changes it would apply:
infrahubctl schema check <path to schema file or a directory> [--branch <branch_name>]
The check command returns a summary of the changes that would be applied if the schema were loaded. No changes are made to the database.
Find the change that caused a failed validation​
When you check or load a schema file, Infrahub validates the nodes stored on the branch against the changes in the file. When some nodes do not comply, infrahubctl schema check and infrahubctl schema load print a refusal, and Infrahub changes neither the schema nor the data. The refusal contains one block for each failed validation: a line that names the validation and the change in your file that caused it, then the messages of the nodes that fail the validation.
In this example, two InfraDevice nodes share the serial SN1001, and the schema file sets unique: true on the serial attribute. The check returns this refusal:
attribute.unique.update on InfraDevice.serial failed. Cause: the change of 'unique' on the attribute 'serial' of InfraDevice.
Attribute-level 'unique' constraint violation on schema 'InfraDevice'. Node (atl1-edge1) is not compliant. The error relates to field serial='SN1001'.,
Attribute-level 'unique' constraint violation on schema 'InfraDevice'. Node (atl1-edge2) is not compliant. The error relates to field serial='SN1001'.
node.uniqueness_constraints.update on InfraDevice failed. Cause: the change of 'unique' on the attribute 'serial' of InfraDevice (derived by schema processing).
Node-level 'uniqueness_constraints' constraint violation on schema 'InfraDevice'. Node (atl1-edge1) is not compliant. The error relates to field serial.value='SN1001'.,
Node-level 'uniqueness_constraints' constraint violation on schema 'InfraDevice'. Node (atl1-edge2) is not compliant. The error relates to field serial.value='SN1001'.
Each validation line has these parts:
- The name of the validation, such as
attribute.unique.update, followed by what it applies to: a kind, such asInfraDevice, or an attribute or a relationship of the kind, such asInfraDevice.serial. Cause:followed by the change in your file, orCauses:followed by several changes separated by semicolons. The text of a change names the kind, the attribute or the relationship, and the property, such asthe change of 'unique' on the attribute 'serial' of InfraDevice.(derived by schema processing)after a change when your file did not set the validated value itself, and Infrahub derived that value from the change when it processed the schema. In the example, Infrahub adds the constraintserial__valueto theuniqueness_constraintsofInfraDevicebecause the file setsunique: trueonserial, so Infrahub also validates the uniqueness constraints of the kind.
No cause is recorded for this validation. at the end of a line means that Infrahub did not link the validation to a change in your file. For example, a schema that Infrahub processed before an upgrade can lack a value that Infrahub derives today. Infrahub then also validates that value, although your file does not set it. The messages under the line still name each node that fails the validation.
When the file renames a kind, an attribute or a relationship, the line names it by its new name, as your file does, both after on and in the cause. The cause of the rename itself names the current name and the new name, such as the rename of InfraDevice to InfraSwitch or the rename of the relationship 'site' of InfraDevice to 'location'. The line names a kind, an attribute or a relationship that the file removes by its current name.
When the file sets regex, min_length or max_length directly on an attribute, the cause names parameters.regex, parameters.min_length or parameters.max_length instead of the property that your file sets.
To resolve a failed validation, update the values of the nodes listed under its line, or change the schema file, then run the check again. When a failed schema validation blocks a merge, a rebase or a Proposed Change, Infrahub reports the messages without these lines.
Load a schema file​
Schema files can be loaded into Infrahub with the infrahubctl command or directly via the Git integration.
Use infrahubctl for development and testing. The git integration option is better suited for production.
- infrahubctl
- Git integration
The infrahubctl command can be used to load individual schema files or multiple files as part of a directory.
infrahubctl schema load <path to schema file or a directory> <path to schema file or a directory> [--branch <branch_name>]
The infrahubctl schema load command can be used to both upload a new schema and update an existing one.
You can define a schema in an external repository.
The schemas that should be loaded must be declared in the .infrahub.yml file, under schemas.
Individual files and directories are both supported.
---
schemas:
- schemas/demo_edge_fabric.yml
Inspect the loaded schema​
Once a schema is loaded, list the kinds available in your instance and inspect any one of them from the command line:
infrahubctl schema list [--filter <name>] [--branch <branch_name>]
infrahubctl schema show <kind> [--branch <branch_name>]
schema show prints the attributes and relationships of a kind, which is useful when querying or creating objects with infrahubctl.
Troubleshooting​
Input should be a valid string (string_type)​
When loading a schema, you may encounter the error "Input should be a valid string (string_type)". This typically occurs when YAML interprets certain unquoted values as booleans instead of strings.
YAML automatically converts unquoted on, off, yes, no, true, and false values to boolean types. When these values appear in fields expecting strings (like choice names), schema validation fails.
Example of the problem:
---
version: "1.0"
nodes:
- name: Stuff
namespace: Random
attributes:
- name: status
kind: Dropdown
choices:
- name: on # Interpreted as boolean true
label: On
- name: off # Interpreted as boolean false
label: Off
Solution:
Quote all boolean-like string values in your schema:
---
version: "1.0"
nodes:
- name: Stuff
namespace: Random
attributes:
- name: status
kind: Dropdown
choices:
- name: "on" # Properly interpreted as string
label: "On"
- name: "off"
label: "Off"
This issue commonly appears in:
- Dropdown choice names
- Attribute names
Related concepts​
- Schema extensions — Add attributes and relationships to existing nodes
- Schema migration — How Infrahub handles schema updates and data migrations
- Schema validation — Editor validation for schema YAML files
- Build your first schema — Step-by-step tutorial for creating a schema from scratch