Skip to content

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

schema = CtyObject({
    "server": CtyObject({
        "host": CtyString(),
        "port": CtyNumber(),
    })
})

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

  1. Define schemas for all external configuration
  2. Make schemas as specific as possible
  3. Document your schemas
  4. Test your schemas

See Also