Skip to content

Validator API Reference

Reference for schema validation functionality.

validate()

Validate a schema dictionary against the SlideGen schema specification.

from slidegen.validator import validate

result = validate(schema)

Parameters:

  • schema (Dict[str, Any]): Schema dictionary to validate

Returns:

  • ValidationResult: Validation result object

Example:

from slidegen.validator import validate

schema = {
    "presentation": {
        "slides": [
            {
                "layout": "title",
                "title": "Welcome"
            }
        ]
    }
}

result = validate(schema)

if result.is_valid:
    print("Schema is valid!")
else:
    for error in result.errors:
        print(f"Error: {error.get('message')}")

ValidationResult

Result object returned by validate().

Properties

is_valid: bool

Whether the schema is valid.

result = validate(schema)
if result.is_valid:
    # Schema is valid
    pass

errors: List[Dict[str, Any]]

List of validation errors. Each error is a dictionary with:

  • field (str): Field path where error occurred
  • message (str): Error message
  • suggestion (Optional[str]): Suggested fix
result = validate(schema)
for error in result.errors:
    field = error.get("field", "unknown")
    message = error.get("message", "unknown error")
    suggestion = error.get("suggestion")

    print(f"{field}: {message}")
    if suggestion:
        print(f"  Suggestion: {suggestion}")

Error Format

Validation errors follow this structure:

{
    "field": "slides[2].layout",
    "message": "Invalid layout type 'invalid_layout'",
    "suggestion": "Use one of: title, bullet_list, chart, table, ..."
}

Validation Rules

The validator checks:

  1. Schema Structure: Required fields, correct types
  2. Layout Types: Valid layout names
  3. Business Rules: Chart data format, bullet limits, etc.
  4. Data Sources: External file existence (if referenced)

CLI Validation

You can also validate from the command line:

slidegen validate deck.yaml

Example: Comprehensive Validation

from slidegen.validator import validate
import json

def validate_schema(schema_path: str) -> bool:
    """Validate a schema file and print detailed errors."""
    with open(schema_path, "r") as f:
        if schema_path.endswith(".json"):
            schema = json.load(f)
        else:
            import yaml
            schema = yaml.safe_load(f)

    result = validate(schema)

    if result.is_valid:
        print(f"✓ {schema_path} is valid")
        return True
    else:
        print(f"✗ {schema_path} has {len(result.errors)} error(s):")
        for i, error in enumerate(result.errors, 1):
            field = error.get("field", "unknown")
            message = error.get("message", "unknown error")
            suggestion = error.get("suggestion")

            print(f"\n  {i}. {field}")
            print(f"     {message}")
            if suggestion:
                print(f"     → {suggestion}")

        return False

# Usage
validate_schema("deck.yaml")

See Also