0PricingLogin
AI Prompt Engineering · Lesson

Schema and Rule Validators

Enforcing constraints.

Validators Enforce the Contract

A validator is a guardrail that checks output against an explicit specification: a JSON Schema, a set of business rules, or both. Unlike a moderation classifier, a validator is deterministic and fully auditable; the same input always yields the same verdict.

Schema Validation Layer

The first validator confirms the output matches the structural contract. Collect all errors, not just the first, so repair feedback is complete.

import jsonschema
def schema_errors(obj, schema):
    v = jsonschema.Draft202012Validator(schema)
    return [f"{list(e.path) or 'root'}: {e.message}"
            for e in v.iter_errors(obj)]

All lessons in this course

  1. What Are Guardrails
  2. Input and Output Filtering
  3. Schema and Rule Validators
  4. Self-Critique Validation
← Back to AI Prompt Engineering