/metaschema:metaschema-constraints-authoringfrom metaschema
Guides creation of Metaschema constraints for validating document instances, covering all constraint types (allowed-values, expect, matches, index, etc.), inline vs external definitions, and Metapath expressions.
Ships in metaschema. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
This skill guides the creation of Metaschema constraints for validating document instances. Constraints can be defined inline within definitions or externally in constraint modules.
| Constraint | Allowed On | Purpose |
|---|---|---|
allowed-values | flag, field, assembly | Restrict values to enumerated set |
expect | flag, field, assembly | Assert Metapath condition is true (fires when FALSE) |
report | flag, field, assembly | Report when Metapath condition is true (fires when TRUE) |
matches | flag, field, assembly | Validate against regex and/or datatype |
has-cardinality | assembly only | Enforce occurrence counts |
index | assembly only | Create named index with composite keys |
index-has-key | flag, field, assembly | Validate cross-references against index |
is-unique | assembly only | Ensure unique composite keys |
let | all | Define reusable variables |
| Attribute | Values | Default | Description |
|---|---|---|---|
id | token | none | Constraint identifier |
level | CRITICAL, ERROR, WARNING, INFORMATIONAL, DEBUG | ERROR | Violation severity |
target | Metapath | . (flags/fields) | Node selection expression |
Define constraints directly within flag, field, or assembly definitions.
- object-type: flag
name: status
as-type: token
constraint:
rules:
- object-type: allowed-values
enums:
- value: active
remark: The item is active.
- value: inactive
remark: The item is inactive.
- value: pending
remark: The item is pending approval.
<define-flag name="status" as-type="token">
<constraint>
<allowed-values>
<enum value="active">The item is active.</enum>
<enum value="inactive">The item is inactive.</enum>
<enum value="pending">The item is pending approval.</enum>
</allowed-values>
</constraint>
</define-flag>
Restricts values to an enumerated set.
Attributes:
allow-other: yes or no (default: no) - permit values beyond enumerated setextensible: model, external, or none (default: external) - allow constraint extensiontarget: Required for fields/assembliesYAML Example:
constraint:
rules:
- object-type: allowed-values
allow-other: "no"
extensible: external
target: "./format"
enums:
- value: XML
remark: XML format.
- value: JSON
remark: JSON format.
- value: YAML
remark: YAML format.
XML Example:
<constraint>
<allowed-values allow-other="no" extensible="external" target="./format">
<enum value="XML">XML format.</enum>
<enum value="JSON">JSON format.</enum>
<enum value="YAML">YAML format.</enum>
</allowed-values>
</constraint>
Deprecating Values:
enums:
- value: new-value
remark: Current preferred value.
- value: old-value
deprecated: "1.0.0"
remark: Deprecated, use new-value instead.
Validates that a Metapath expression evaluates to true.
Attributes:
test: Required - Metapath boolean expressiontarget: Required for fields/assembliesmessage: Optional - custom error message with Metapath templatesYAML Example:
constraint:
rules:
- object-type: expect
target: "."
test: "string-length(.) > 0"
message: "The value must not be empty."
- object-type: expect
target: "./start-date"
test: ". <= ../end-date"
message: "Start date ({ . }) must be before or equal to end date ({ ../end-date })."
XML Example:
<constraint>
<expect target="." test="string-length(.) gt 0">
<message>The value must not be empty.</message>
</expect>
<expect target="./start-date" test=". le ../end-date">
<message>Start date ({ . }) must be before or equal to end date ({ ../end-date }).</message>
</expect>
</constraint>
Reports a finding when a Metapath expression evaluates to true. This is the inverse of expect: expect fires when the test is FALSE (assertion failed), while report fires when the test is TRUE (condition detected).
Use cases:
Attributes:
test: Required - Metapath boolean expressiontarget: Required for fields/assembliesmessage: Optional - custom message with Metapath templateslevel: Typically INFORMATIONAL or WARNING (not ERROR)YAML Example:
constraint:
rules:
- object-type: report
id: deprecated-element
level: WARNING
target: "."
test: "exists(@deprecated)"
message: "This element is deprecated. Consider using the replacement."
- object-type: report
id: large-collection
level: INFORMATIONAL
target: "./items"
test: "count(./item) > 100"
message: "Collection has { count(./item) } items, which may impact performance."
XML Example:
<constraint>
<report id="deprecated-element" level="WARNING" target="." test="exists(@deprecated)">
<message>This element is deprecated. Consider using the replacement.</message>
</report>
<report id="large-collection" level="INFORMATIONAL" target="./items" test="count(./item) gt 100">
<message>Collection has { count(./item) } items, which may impact performance.</message>
</report>
</constraint>
expect vs report:
| Constraint | Fires When | Typical Level | Use Case |
|---|---|---|---|
expect | test is FALSE | ERROR | Validation failures |
report | test is TRUE | WARNING/INFORMATIONAL | Detected conditions |
Validates values against datatype and/or regex patterns.
Attributes:
datatype: Optional - required datatype formatregex: Optional - pattern match requirementtarget: Required for fields/assembliesYAML Example:
constraint:
rules:
- object-type: matches
target: "./code"
regex: "^[A-Z]{2}-[0-9]{4}$"
- object-type: matches
target: "./email"
datatype: email-address
XML Example:
<constraint>
<matches target="./code" regex="^[A-Z]{2}-[0-9]{4}$"/>
<matches target="./email" datatype="email-address"/>
</constraint>
Enforces occurrence counts for selected nodes. Assembly only.
Attributes:
target: Required - Metapath expression selecting nodes to countmin-occurs: Optional - minimum required occurrencesmax-occurs: Optional - maximum allowed occurrences (or unbounded)YAML Example:
constraint:
rules:
- object-type: has-cardinality
target: "./contact"
min-occurs: 1
max-occurs: 5
message: "Must have between 1 and 5 contacts."
XML Example:
<constraint>
<has-cardinality target="./contact" min-occurs="1" max-occurs="5">
<message>Must have between 1 and 5 contacts.</message>
</has-cardinality>
</constraint>
Creates a named index of nodes with composite keys. Assembly only.
Attributes:
name: Required - unique index identifiertarget: Required - Metapath expression selecting nodes to indexYAML Example:
constraint:
rules:
- object-type: index
name: user-index
target: ".//user"
key-fields:
- target: "@id"
XML Example:
<constraint>
<index name="user-index" target=".//user">
<key-field target="@id"/>
</index>
</constraint>
Composite Keys:
- object-type: index
name: user-dept-index
target: ".//user"
key-fields:
- target: "@id"
- target: "@department"
Validates that cross-references exist in a named index.
Attributes:
name: Required - references the index to check againsttarget: Required - Metapath expression selecting nodes to validateYAML Example:
constraint:
rules:
- object-type: index-has-key
name: user-index
target: "./user-ref"
key-fields:
- target: "@ref"
message: "Referenced user '{ @ref }' does not exist."
XML Example:
<constraint>
<index-has-key name="user-index" target="./user-ref">
<key-field target="@ref"/>
<message>Referenced user '{ @ref }' does not exist.</message>
</index-has-key>
</constraint>
Ensures computed keys are unique (inline uniqueness check). Assembly only.
Attributes:
target: Required - Metapath expression selecting nodes to checkYAML Example:
constraint:
rules:
- object-type: is-unique
target: "./entry"
key-fields:
- target: "@code"
message: "Duplicate entry code '{ @code }' found."
XML Example:
<constraint>
<is-unique target="./entry">
<key-field target="@code"/>
<message>Duplicate entry code '{ @code }' found.</message>
</is-unique>
</constraint>
Defines reusable variables for subsequent constraint evaluation.
YAML Example:
constraint:
lets:
- var: parent
expression: ".."
- var: sibling-count
expression: "count($parent/sibling)"
rules:
- object-type: expect
target: "."
test: "$sibling-count >= 1"
message: "Must have at least one sibling."
XML Example:
<constraint>
<let var="parent" expression=".."/>
<let var="sibling-count" expression="count($parent/sibling)"/>
<expect target="." test="$sibling-count ge 1">
<message>Must have at least one sibling.</message>
</expect>
</constraint>
Constraints use Metapath expressions based on XPath 3.1. See the metapath-expressions skill for comprehensive documentation on:
External constraint modules apply constraints across modules without modifying original definitions.
XML:
<?xml version="1.0" encoding="UTF-8"?>
<metaschema-meta-constraints
xmlns="http://csrc.nist.gov/ns/oscal/metaschema/1.0">
<import href="other-constraints.xml"/>
<context>
<metapath target="/catalog"/>
<constraints>
<expect target="./metadata/title" test="string-length(.) > 0">
<message>Catalog must have a title.</message>
</expect>
</constraints>
<context>
<metapath target="./group"/>
<constraints>
<has-cardinality target="./control" min-occurs="1"/>
</constraints>
</context>
</context>
</metaschema-meta-constraints>
Constraints can be nested within contexts for scoping:
<context>
<metapath target="/document"/>
<constraints>
<!-- document-level constraints -->
</constraints>
<context>
<metapath target="./section"/>
<constraints>
<!-- section-level constraints -->
</constraints>
</context>
</context>
Target specific definitions by name and namespace:
<definition-context name="user" namespace="http://example.com/ns">
<constraints>
<rules>
<expect target="./email" test="matches(., '.+@.+\..+')"/>
</rules>
</constraints>
</definition-context>
| Level | Description |
|---|---|
CRITICAL | Serious fault preventing typical use |
ERROR | Fault in content (default) |
WARNING | Potential issue |
INFORMATIONAL | Point of interest |
DEBUG | For model/tool development |
Use Inline Constraints When:
Use External Constraints When:
Hierarchical Validation:
constraint:
rules:
- object-type: expect
target: "./child"
test: "count(../child) <= @max-children"
message: "Too many children (max: { @max-children })."
Cross-Reference Validation:
# First, create the index at the container level
- object-type: index
name: component-index
target: ".//component"
key-fields:
- target: "@uuid"
# Then validate references
- object-type: index-has-key
name: component-index
target: ".//component-ref"
key-fields:
- target: "@component-uuid"
Conditional Validation:
constraint:
rules:
- object-type: expect
target: "./start-date"
test: "not(exists(../end-date)) or . <= ../end-date"
message: "Start date must be before end date when end date is specified."
Create test content that exercises constraints:
See .claude/rules/metaschema-authoring.md for repository-specific testing commands.
constraint:
rules:
- object-type: expect
target: "./title"
test: "exists(.) and string-length(normalize-space(.)) > 0"
message: "Title is required and cannot be empty."
constraint:
rules:
- object-type: expect
target: "."
test: "not(exists(./option-a) and exists(./option-b))"
message: "Only one of option-a or option-b may be specified."
constraint:
rules:
- object-type: expect
target: "."
test: "exists(./email) or exists(./phone)"
message: "At least one contact method (email or phone) is required."
constraint:
rules:
- object-type: expect
target: "./selection"
test: ". = ancestor::container/options/option/@value"
message: "Selection must be one of the defined options."
constraint:
rules:
- object-type: expect
target: "./end-date"
test: ". >= ../start-date"
message: "End date ({ . }) must be on or after start date ({ ../start-date })."
What SKILL.md sets; muted values are Claude Code's defaults
Other plugins' skills for the same job
npx claudepluginhub metaschema-framework/claude-plugins --plugin metaschema4 skills in this plugin
/metapath-expressionsUse when writing Metapath expressions for constraints, queries, or traversals - covers path syntax, operators, functions, and data model based on XPath 3.1/metaschema-basicsUse as an introduction to Metaschema concepts - provides overview and routes to detailed skills/metaschema-module-authoringUse when creating or modifying Metaschema modules - guides module structure, definitions, format-specific features, and best practices for both YAML and XML formats