Week 05 · lesson
Schemas Turn Assumptions Into Tests
A schema is a machine-checkable or human-checkable statement about the shape of data.
It turns “we expect this message to look right” into explicit conditions that can be tested.
A small schema is enough to teach the idea
For the fictional telemetry message, define:
schema_version
integer
allowed: 1
required
robot_id
string
pattern: RNX-##
required
motor_temp_c
integer
allowed: -20..120
required
battery_percent
integer
allowed: 0..100
required
enabled
boolean
required
Now every field has a boundary.
Validation order should be deliberate
A useful sequence is:
- can the message be decoded?
- is the top-level structure correct?
- is the schema version supported?
- are required fields present?
- are field types correct?
- are values inside allowed ranges or sets?
- do cross-field rules hold?
Cross-field rules matter because individually valid values can form an invalid combination.
Example:
enabled = false
command = "move"
Both fields may be individually valid, but the pair may violate the system contract.
Lab: design a validator without hiding assumptions
Use pseudocode or Python:
def validate(message):
required = {
"schema_version",
"robot_id",
"motor_temp_c",
"battery_percent",
"enabled",
}
if set(message) != required:
return False, "field-set"
if message["schema_version"] != 1:
return False, "schema-version"
if type(message["motor_temp_c"]) is not int:
return False, "motor-temp-type"
if not -20 <= message["motor_temp_c"] <= 120:
return False, "motor-temp-range"
if type(message["battery_percent"]) is not int:
return False, "battery-type"
if not 0 <= message["battery_percent"] <= 100:
return False, "battery-range"
if type(message["enabled"]) is not bool:
return False, "enabled-type"
return True, "ok"
This is intentionally incomplete. It gives you something inspectable to improve.
Add checks for:
robot_idshape;- no extra fields;
- supported schema version; and
- one cross-field rule you can justify.
Do not connect the validator to a real robot or external service. Use supplied fictional messages only.
Test with a matrix, not one example
Create at least eight cases:
| Case | Condition | Expected |
|---|---|---|
| 1 | known-good message | accept |
| 2 | missing required field | reject |
| 3 | extra unexpected field | reject or document policy |
| 4 | wrong type | reject |
| 5 | low boundary | accept |
| 6 | just below low boundary | reject |
| 7 | high boundary | accept |
| 8 | just above high boundary | reject |
Add one cross-field mismatch.
Then record the actual result.
A schema does not make data true
This limitation matters.
Suppose the message says:
{"motor_temp_c":72}
The value can be structurally valid and still be inaccurate because:
- the sensor is miscalibrated;
- the value is stale;
- the wrong sensor was mapped to the field;
- the timestamp is wrong;
- the upstream component fabricated or corrupted the measurement; or
- the unit conversion happened incorrectly before validation.
A schema validates representation and declared constraints. It does not prove the physical world matches the message.
That is a perfect example of bounded evidence.
Contracts need ownership
Someone has to decide:
- who may change the schema;
- how versions are introduced;
- how old consumers are handled;
- how failures are logged;
- what tests must pass before deployment; and
- how the change is documented.
That turns a simple schema into a governance object.
You will return to this idea during change control and risk governance later in CS1337.
Finish the Data Contract and Parser Test Record
Submit:
- field dictionary;
- schema/version rule;
- one valid message;
- at least seven malformed/boundary messages;
- expected and observed result for each;
- one cross-field rule;
- rejection/logging behavior;
- one limitation; and
- one change-control rule for future schema versions.
A strong final claim:
The validator enforced the documented Week 5 field set, types, version, and tested value boundaries for the supplied messages. It does not prove that accepted sensor values are physically accurate or trustworthy.
That is what a defensible data claim sounds like.