Schema Validation
Syntactic validation checks that every YAML file is well-formed and that its structure matches a schema: which keys exist, which ones are mandatory, and which types and values they accept. It catches typos in key names, missing mandatory attributes, wrong types and out-of-range values — before the data model reaches any downstream tooling.
The schema is written in Yamale syntax, which nac-validate extends with additional constraints such as primary keys. It is passed with -s/--schema and defaults to .schema.yaml in the current directory. Without a schema, only the YAML syntax is checked.
How Schema Validation Works
Section titled “How Schema Validation Works”- YAML syntax — every YAML file (
.yamlor.yml) in the given paths is parsed. Errors such as wrong indentation are reported with line and column. - Schema — every file is validated on its own against the schema with Yamale.
- Schema extensions — once all files passed, the extensions are checked, first in every file and then, where needed, on the data merged from all files.
Semantic validation only runs if syntactic validation passed. Syntactic errors are reported with exit code 2.
Because each file is validated individually, a field marked as required must be present in every file that contains its parent. If the data of one object is spread across several files, its fields must be optional (required=False). This is why most fields in Network as Code schemas are optional, except for identifiers such as name.
Schema Structure
Section titled “Schema Structure”A schema file consists of one or more YAML documents separated by ---. The first document describes the root of the data, all following documents define reusable includes, referenced by name with include():
---fabric: include('fabric', required=False)---fabric: name: regex('^[a-zA-Z0-9_.-]{1,64}$', required=False) nodes: list(include('node'), primary_key='id', required=False) interfaces: list(include('interface'), primary_key='node_id,name', required=False)
node: id: int(min=101, max=4000) name: str(max=63) role: enum('leaf', 'spine') mgmt_ip: ip(version=4, required=False)
interface: node_id: int() name: regex('^eth\d+/\d+$') description: str(exclude='"', required=False) vlans: list(int(min=1, max=4094), required=False)Every key maps to a validator, and nested maps can be written inline or moved to an include. Includes can reference other includes, and themselves for recursive structures. A valid data file for the schema above:
fabric: name: FABRIC1 nodes: - id: 101 name: leaf-101 role: leaf mgmt_ip: 10.0.0.101/24 interfaces: - node_id: 101 name: eth1/1 vlans: [10, 20]Common Keywords
Section titled “Common Keywords”All validators accept the following keywords:
| Keyword | Default | Description |
|---|---|---|
required | True | Whether the key must be present. required=False makes it optional. |
none | True | Whether an optional key may be set to null (e.g. name: without a value). none=False rejects null values. A required key never accepts null. |
description | Documents the schema element. Not checked against the data. |
| Keyword | Default | Description |
|---|---|---|
required | True | Whether the key must be present. required=False makes it optional. |
none | True | Whether an optional key may be set to null (e.g. name: without a value). none=False rejects null values. A required key never accepts null. |
An optional key set to null is treated as not set, so its validator and any extensions are skipped.
Validators
Section titled “Validators”The validators below are those of Yamale 6, which nac-validate uses. See the Yamale documentation for all details.
Scalars
Section titled “Scalars”| Validator | Accepts | Keywords |
|---|---|---|
str() | Strings (not numbers or booleans) | min, max (length), equals, starts_with, ends_with, matches (regex), exclude (characters not allowed), ignore_case (for equals, starts_with, ends_with and exclude) |
regex(pattern, ...) | Strings matching at least one of the patterns | name (used in error messages), ignore_case, multiline, dotall |
int() | Integers | min, max |
num() | Integers and floats | min, max |
bool() | true and false | |
enum(value, ...) | One of the given constants, e.g. enum('leaf', 'spine') or enum(1, 2, 3) | |
ip() | IPv4 and IPv6 addresses, with or without prefix length (10.0.0.1, 10.0.0.0/24) | version (4 or 6), type |
mac() | MAC addresses (00:11:22:33:44:55, 0011.2233.4455, …) | |
semver() | Semantic versions (1.2.3) | |
day() | Dates (2026-11-05) | min, max |
timestamp() | Date and time (2026-11-05 10:00:00) | min, max |
null() | Null values only |
All validators of a single value also accept ref.
| Validator | Accepts | Keywords |
|---|---|---|
str() | Strings (not numbers or booleans) | min, max (length), equals, starts_with, ends_with, matches (regex), exclude (characters not allowed), ignore_case (for equals, starts_with, ends_with and exclude) |
regex(pattern, ...) | Strings matching at least one of the patterns | name (used in error messages), ignore_case, multiline, dotall |
int() | Integers | min, max |
num() | Integers and floats | min, max |
bool() | true and false | |
enum(value, ...) | One of the given constants, e.g. enum('leaf', 'spine') or enum(1, 2, 3) | |
ip() | IPv4 and IPv6 addresses, with or without prefix length (10.0.0.1, 10.0.0.0/24) | version (4 or 6) |
mac() | MAC addresses (00:11:22:33:44:55, 0011.2233.4455, …) | |
semver() | Semantic versions (1.2.3) | |
day() | Dates (2026-11-05) | min, max |
timestamp() | Date and time (2026-11-05 10:00:00) | min, max |
null() | Null values only |
Regex patterns match from the start of the string, but not necessarily to its end — anchor them with ^...$ to match the entire value. Validator arguments are parsed like Python string literals: escape sequences such as \d or \. are kept as written ('^eth\d+/\d+$'), but a literal backslash in the value needs four backslashes in the pattern ('^a\\\\b$' matches a\b).
name: regex('^[a-zA-Z0-9_.-]{1,64}$')hostname: str(min=1, max=63, exclude=' ')mtu: int(min=576, max=9216, required=False)admin_state: enum('up', 'down', required=False)loopback: ip(version=4)Lists and Maps
Section titled “Lists and Maps”| Validator | Accepts | Keywords |
|---|---|---|
list(validator, ...) | Lists whose items match at least one of the validators. list() accepts any items. | min, max (number of items), primary_key, unique, ref_name, ref_implicit, ref_scope, ordered |
map(validator, ...) | Maps whose values match at least one of the validators. map() accepts any keys and values. | min, max (number of keys), key (validator for the keys) |
include('name') | Data matching the named include | strict |
| Validator | Accepts | Keywords |
|---|---|---|
list(validator, ...) | Lists whose items match at least one of the validators. list() accepts any items. | min, max (number of items), primary_key |
map(validator, ...) | Maps whose values match at least one of the validators. map() accepts any keys and values. | min, max (number of keys), key (validator for the keys) |
include('name') | Data matching the named include | strict |
nodes: list(include('node'), min=1)vlans: list(int(min=1, max=4094))labels: map(str(), key=regex('^[a-z_]+$'), required=False)map() is meant for maps with arbitrary keys. Maps with a fixed set of keys are defined as an include or nested inline in the schema.
Combining Validators
Section titled “Combining Validators”| Validator | Accepts | Keywords |
|---|---|---|
any(validator, ...) | Data matching at least one of the validators | |
subset(validator, ...) | A list or map where every item matches one of the validators; a single value is treated as a list of one | allow_empty |
# A VLAN ID or the keyword 'all'vlan: any(int(min=1, max=4094), enum('all'), required=False)# Either a single prefix or a list of prefixesprefixes: subset(ip(), allow_empty=True)subset() ignores required=False: the key is optional only with allow_empty=True.
Strict Mode
Section titled “Strict Mode”By default, keys that are not defined in the schema are reported as Unexpected element. This catches misspelled keys, which would otherwise silently be ignored by downstream tooling.
--non-strict turns this off for the whole data, and include('name', strict=False) for the data below a single include — for example, for parts of the data model consumed by other tools. Untyped data can also be allowed with any():
---fabric: include('fabric', required=False)# Data used by other tools, not validatedcustom: any(required=False)nac-validate Extensions
Section titled “nac-validate Extensions”nac-validate adds keywords to the standard Yamale validators. Plain Yamale accepts and ignores them, so a schema using them still works with Yamale and other tools, which then just don’t enforce them.
-
Extensions are checked once the data passed Yamale validation, so they only report errors in data that is otherwise valid.
-
An extension keyword on a validator that does not support it, or with an invalid value, fails loading the schema (exit code
3):ERROR - Invalid schema: node.name: primary_key is only supported on list() -
A keyword known neither to Yamale nor to
nac-validate, such as a misspelled one, is logged as a warning and ignored:WARNING - Unknown schema keyword 'requird' at 'node.name' is ignored
Primary Keys
Section titled “Primary Keys”primary_key on a list() of maps names a field that every item must set to a single value — not a map or a list — that is unique within the list. It is available in nac-validate 2.1.0 and later.
nodes: list(include('node'), primary_key='id', required=False)A composite key names several comma-separated fields, whose combination must be unique. This allows, for example, the same interface name on different nodes:
interfaces: list(include('interface'), primary_key='node_id,name', required=False)The following errors are reported:
data/nodes.yaml: fabric.nodes: Primary key 'id' missing in item 2data/nodes.yaml: fabric.nodes: Primary key 'id' in item 3 must be a single value, not a listdata/nodes.yaml: fabric.nodes: Duplicate primary key id=101 in items 0, 1data/interfaces.yaml: fabric.interfaces: Duplicate primary key node_id=101, name=eth1/1 in items 0, 2Values of different types are distinct, so 1 and "1" do not collide.
Primary keys are checked in every file and, once all files passed, on the data merged from all files. When merging, list items with the same key are only merged if all their shared fields have the same values (see Merging YAML). Therefore, the same key can be used in several files to spread an object across them, but a key used with conflicting values in two files remains a duplicate:
fabric: nodes: - id: 102 name: leaf-102 role: leaffabric: nodes: - id: 102 name: spine-102 role: spinedata: fabric.nodes: Duplicate primary key id=102 across files (with conflicting values that prevent merging)Unlike a semantic rule checking for unique keys, a primary key is part of the schema, so it applies to every list using the include and doesn’t need to be maintained separately.
Unique Values
Section titled “Unique Values”unique on a list() requires values to be unique within the list without making them the key of an item. unique=True applies to a list of single values; items that are maps or lists are not compared:
vlans: list(int(min=1, max=4094), unique=True, required=False)On a list of maps, unique names comma-separated fields whose combination must be unique. Unlike with primary_key, the fields may be optional: a field that is not set is a value of its own, so a route without a vrf doesn’t collide with the same prefix in a VRF. Items setting none of the fields are not compared:
routes: list(include('route'), unique='prefix,vrf', required=False)unique can be combined with a primary_key, for example to require unique names next to unique IDs, but must not name the same fields:
nodes: list(include('node'), primary_key='id', unique='name', required=False)The following errors are reported:
data/interfaces.yaml: fabric.interfaces.[name=eth1/1].vlans: Duplicate value 10 in items 0, 2data/interfaces.yaml: fabric.routes: Duplicate unique key prefix=10.1.0.0/16 in items 0, 2data/nodes.yaml: fabric.nodes: Duplicate unique key name=leaf-101 in items 0, 1As with primary keys, values of different types are distinct, and unique is checked in every file and, once all files passed, on the data merged from all files. Items from different files that are not merged, because they have different primary keys, still collide.
References
Section titled “References”References check in the schema that a value names an object defined elsewhere in the data model, which otherwise requires a semantic rule. A list declares what its items can be referenced as with ref_name, and a field what it refers to with ref. The value of the field must then be the key of an item of such a list:
fabric: nodes: list(include('node'), primary_key='id', ref_name='node', required=False) vrfs: list(include('vrf'), primary_key='name', ref_name='vrf', ref_implicit='default,management', required=False) interfaces: list(include('interface'), primary_key='node_id,name', required=False)
interface: node_id: int(ref='node') name: regex('^eth\d+/\d+$') vrf: str(ref='vrf', required=False)ref_namerequires a single-fieldprimary_key. Several lists may share a name, so a reference can name an item of any of them, for example hosts, networks and ranges asnetwork_object.ref_implicitlists comma-separated values that exist without being defined in the data, such as default objects of the platform.*and?are wildcards (system-*).refworks on any validator of a single value, including the item validator of a list (list(str(ref='vrf'))) andany(). Values are compared as text, so an integer key matches its string form.
An interface on an undefined node or VRF is reported as:
data: fabric.interfaces.[name=eth1/2].node_id: node '103' is not defineddata: fabric.interfaces.[name=eth1/2].vrf: vrf 'DEV' is not definedTargets are looked up following the nesting of the data: for every list enclosing both the reference and a list of targets, both must be in the same item of it. A VRF defined in one device is not visible from another device, while a reference outside the devices list sees the VRFs of all devices:
devices: list(include('device'), primary_key='name')---device: name: str() vrfs: list(include('vrf'), primary_key='name', ref_name='vrf', required=False) interfaces: list(include('interface'), primary_key='name', required=False)data: devices.[name=LEAF2].interfaces.[name=Ethernet1/1].vrf: vrf 'PROD' is not defined in devices[name=LEAF2]ref_in names fields of the referencing item whose values select the items the target is in, the last one the innermost list’s item. Paths are dotted and read from the nearest list item enclosing the reference. A selector that is not set falls back to nesting, and a selector naming an unknown item is only reported by its own ref:
leak_destination: tenant: str(ref='tenant') vrf: str(ref='tenant_vrf', ref_in='tenant')contract_master: application_profile: str(ref='application_profile', required=False) endpoint_group: str(ref='endpoint_group', ref_in='application_profile')ref_scope='any' on a list with a single-field primary_key lifts the isolation of its items: a reference inside one tenant sees the targets in all tenants.
References are checked on the data merged from all files, also for a single file, once all files passed. Loading the schema fails for a ref naming no ref_name, a ref_in path that doesn’t exist in the enclosing list item, more ref_in selectors than lists enclose the target, a selected list without a single-field primary_key, and lists sharing a ref_name used with ref_in that are nested differently:
ERROR - Invalid schema: fabric.interfaces.vrf: ref names no list with ref_name 'vrf'IP Forms
Section titled “IP Forms”type on an ip() restricts the value to one or several comma-separated forms:
| Type | Accepted | Rejected |
|---|---|---|
address | 10.0.0.1 | Any prefix length |
interface | 10.0.0.1/24 | No prefix length, network or broadcast address (10.0.0.0/24, 10.0.0.255/24) |
network | 10.0.0.0/24 | No prefix length, host bits set (10.0.0.1/24) |
next_hop: ip(type='address')mgmt_ip: ip(version=4, type='interface')prefix: ip(type='network')destination: ip(type='interface,network')interface rejects the network address of the subnet, the IPv4 broadcast address and the IPv6 subnet-router anycast address (2001:db8::/64), except on IPv4 /31 and /32 and IPv6 /127 and /128 subnets. Prefix lengths must be numbers, netmasks like 10.0.0.1/255.255.255.0 are rejected. Errors say why a value is rejected:
data/interfaces.yaml: fabric.routes.[prefix=10.2.1.1/16].prefix: '10.2.1.1/16' is not an IP network, host bits are set (network is 10.2.0.0/16)data/interfaces.yaml: fabric.routes.[prefix=10.2.1.1/16].next_hop: '10.0.0.1/24' is not an IP address, it has a prefix lengthdata/nodes.yaml: fabric.nodes.[name=leaf-102].mgmt_ip: '10.0.0.0/24' is not an IP host address with prefix length, it is the network addressWithin any(), list() and subset(), a value matches the first validator whose extensions also accept it, so any(ip(type='address'), ip(type='network')) accepts both forms.
Ordered Lists
Section titled “Ordered Lists”ordered=True on a list() marks the order of its items as significant, for example for the entries of an access list. A list without it, or with ordered=False, has no significant order. It is not checked against the data:
entries: list(include('acl_entry'), ordered=True)Descriptions
Section titled “Descriptions”description documents a schema element. It is supported on every validator and not checked against the data:
nodes: list(include('node'), primary_key='id', description='Switches of the fabric')---node: id: int(min=101, max=4000, description='Node ID')Error Messages
Section titled “Error Messages”Schema errors are reported per file, with the path of the invalid value in the data:
• data/interfaces.yaml: fabric.interfaces.[name=Ethernet1/2].name: 'Ethernet1/2' is not a regex match. • data/interfaces.yaml: fabric.interfaces.[name=Ethernet1/2].vlans: 5000 is greater than 4094 • data/nodes.yaml: fabric.nodes.[name=leaf-102].role: 'border' not in ('leaf', 'spine') • data/more_nodes.yaml: fabric.nodes.[id=102].foo: Unexpected element • data/more_nodes.yaml: fabric.nodes.[id=102].role: Required field missingList items are shown by one of their fields instead of their index: name if set, otherwise id, otherwise the first scalar field of the item.