CSE Roadmap

Core tier · about 4 hours · 13 parts

Part 2 of 13 · 2 min read

Reject strictly, never coerce: a schema that bends is a schema with a hole

Checking that the right keys exist in an argument object tells you almost nothing about whether the values behind those keys are safe to act on.

Think of a bank teller who checks that a deposit slip has a name and an amount filled in. The teller never checks whether the amount is a real number, negative, or written in a currency that does not exist. The slip "has the right fields," and that is the entire check.

a guardrail is a check or constraint around the model that is enforced outside it, a validator is the code that decides whether an input, argument or output is acceptable, and syntactic validation checks that a value has the right type, range and format. Strictly, with no coercion of a string "50" into the number 50, no treating a boolean as a number, and no silently accepting NaN or infinity. That is because every one of those loopholes is exactly where a hostile argument tries to slip through.

A worked example. Against 14 hand-written hostile or malformed refund arguments, a validator that only checks the required keys are present accepts 13 of 14 (93%) -- it never looks past "does amount exist" to notice that the value is -50, or 1e9, or the boolean True, or float("nan"). A strict schema validator that checks exact types, numeric ranges, an enum of allowed reasons, and a full-match ASCII pattern on the order ID accepts 0 of the 14 -- while still accepting all 4 legitimate refund requests.

The key lines. From code/mini-guardrails/guardrails.py:

# from code/mini-guardrails/guardrails.py
def validate_args(schema, args):
    """Return a list of error strings (empty = valid). Strict: no coercion, no unknown keys, bools are not numbers, NaN/inf rejected."""
    errs = []
    if not isinstance(args, dict):
        return ["arguments must be an object"]
    for k in args:
        if k not in schema:
            errs.append(f"unknown field {k}")

Read it as: the function's very first checks are "is this even a dictionary" and "does every key belong," before it looks at a single value. Rejecting unknown fields outright, rather than ignoring them, is what stops an argument like {"admin": True} slipped in alongside a legitimate-looking refund request. The schema doesn't just validate the fields it expects; it refuses anything it doesn't.

Guardrail, validator, syntactic validation

A guardrail is a check or constraint around the model that is enforced outside it. A validator is code that decides whether an input, argument or output is acceptable. Syntactic validation checks that the value has the right type, range and format.

Example: the one case where the strict validator and the naive one disagree on an Arabic-Indic-numeral order ID (ORD-١٢٣٤٥٦) is syntactic validation doing exactly its job -- the string looks like it could be an order ID to a careless eye, but a full-match ASCII pattern correctly identifies that it isn't the format the schema actually specifies.