Schema and Rule Validators
Enforcing constraints.
Schema and Rule Validators is a free AI Prompt Engineering lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AI Prompt Engineering learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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)]Beyond Schema: Business Rules
Schemas cannot express everything. A schema can say discount is a number, but not that it must not exceed the subtotal. Rule validators encode these cross-field and domain invariants in code.
RULES = [
('discount_le_subtotal', lambda o: o['discount'] <= o['subtotal']),
('total_consistent', lambda o: o['total'] == o['subtotal'] - o['discount']),
('currency_supported', lambda o: o['currency'] in SUPPORTED)
]
def rule_errors(o):
return [name for name, fn in RULES if not fn(o)]Typed Models as Validators
A typed model library (e.g., Pydantic) doubles as schema generator and validator, with custom validators for rules. Parsing failure is the verdict.
from pydantic import BaseModel, field_validator
class Order(BaseModel):
subtotal: float
discount: float
total: float
@field_validator('discount')
@classmethod
def discount_ok(cls, v, info):
if v < 0:
raise ValueError('discount must be non-negative')
return vGrounding and Citation Validators
For RAG outputs, validate that claims are supported by the provided context. Require the model to cite source ids, then verify each cited id exists and that quoted spans actually appear in the source. Reject answers that cite missing or fabricated sources.
def citations_valid(answer, sources):
for cite in answer['citations']:
if cite['source_id'] not in sources:
return False
if cite['quote'] not in sources[cite['source_id']]:
return False
return TrueReferential and Cross-System Checks
Some rules require external lookups: does the referenced order id exist, is the product in stock, is the date in a valid window? These validators query your systems. Keep them fast (cache, batch) because they sit on the response path.
def order_exists(o):
return db.exists('SELECT 1 FROM orders WHERE id = %s', o['order_id'])Severity-Tagged Verdicts
Not all violations are equal. Tag each rule with a severity so the pipeline can choose the right action: hard-block critical violations, regenerate on quality issues, and pass-with-warning on minor ones.
RULE_SEVERITY = {
'discount_le_subtotal': 'critical',
'total_consistent': 'critical',
'tone_professional': 'warning'
}Compose Validators in Order
Run validators cheapest-first and short-circuit on hard failures: schema, then in-memory rules, then external lookups, then any model-judge checks. This minimizes cost on the common reject path.
def validate_all(obj):
errs = schema_errors(obj, SCHEMA)
if errs: return Reject(errs, 'critical')
errs = rule_errors(obj)
if errs: return Reject(errs, max_severity(errs))
if not order_exists(obj): return Reject(['order_id'], 'critical')
return Accept()Validators Drive Repair Feedback
A validator's value multiplies when its messages feed the repair loop. Emit machine-actionable errors with the field path, the failed rule, and the offending value so the model can fix precisely in one turn.
{'errors': [
{'path': 'discount', 'rule': 'discount_le_subtotal',
'got': 120, 'limit': 100}
]}Test the Validators Themselves
A buggy validator silently passes bad output or blocks good output. Unit-test each rule with positive and negative fixtures, and add property-based tests for numeric invariants. The validator suite is production-critical code, not a script.
def test_discount_rule():
assert rule_errors({'discount':10,'subtotal':100,'total':90}) == []
assert 'discount_le_subtotal' in rule_errors(
{'discount':120,'subtotal':100,'total':-20})Validators vs Constrained Decoding
Constrained decoding prevents structural errors at generation time but cannot enforce semantics (it does not know your business rules). Validators catch semantic violations after the fact. Use both: decoding for shape, validators for meaning, repair loop to reconcile.
Quick Check
A JSON Schema marks discount and subtotal as numbers. The model returns discount 120 with subtotal 100. Which layer should catch this?
Recap
Schema and rule validators:
- Schema validation covers structure; rule validators cover semantics.
- Typed models generate schemas and validate in one place.
- Add grounding, citation, and referential checks for RAG and data flows.
- Tag severity, compose cheapest-first, feed precise repair errors.
- Test validators as production-critical code.
Next: self-critique validation, where the model checks its own output.
Frequently asked questions
Is the “Schema and Rule Validators” lesson free?
Yes — the full text of “Schema and Rule Validators” is free to read here on the web, and the AI Prompt Engineering course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AI Prompt Engineering course, upgrade to CoddyKit PRO.
What will I learn in “Schema and Rule Validators”?
Enforcing constraints. You practise AI Prompt Engineering with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start AI Prompt Engineering?
No prior experience is required. AI Prompt Engineering on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Schema and Rule Validators” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this AI Prompt Engineering lesson?
Yes. Every AI Prompt Engineering lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- What Are Guardrails
- Input and Output Filtering
- Schema and Rule Validators
- Self-Critique Validation