Schema Validation Guide¶
๐ค AI-Generated Content
This documentation was generated with AI assistance and is still being audited. Some, or potentially a lot, of this information may be inaccurate. Learn more.
Why Use Schemas?¶
Schemas provide: - Type safety - Early error detection - Clear contracts - Better error messages
Defining Schemas¶
from pyvider.cty import CtyObject, CtyString, CtyNumber, CtyBool
schema = CtyObject({
"name": CtyString(),
"port": CtyNumber(),
"enabled": CtyBool(),
})
Complex Schemas¶
Lists¶
from pyvider.cty import CtyList, CtyString
schema = CtyObject({
"tags": CtyList(element_type=CtyString())
})
Nested Objects¶
Lists of Objects¶
schema = CtyObject({
"users": CtyList(
element_type=CtyObject({
"name": CtyString(),
"age": CtyNumber(),
})
)
})
Validation Errors¶
from pyvider.hcl import HclParsingError, parse_hcl_to_cty
try:
result = parse_hcl_to_cty(hcl, schema=schema)
except HclParsingError as e:
print(f"Validation failed: {e}")
Schema Best Practices¶
- Define schemas for all external configuration
- Make schemas as specific as possible
- Document your schemas
- Test your schemas