Practice in Lab: Validate a closed structured-output schema without coercion →
Validate a closed structured-output schema without coercion
A structured-output flag is not a substitute for validating data at the application boundary. Coercing a boolean into an integer, accepting an unexpected property or silently ignoring an unsupported schema can change the authority of a tool call. This exercise implements a small closed vocabulary and fails explicitly when asked to enforce a feature it does not understand. Paths identify nested failures without executing the returned data. Production systems should normally use a maintained validator and a versioned schema rather than expanding an ad hoc subset indefinitely.
value = {'count':True, 'extra':'unexpected'}
errors = []
if type(value['count']) is not int:
errors.append('$.count:type')
if 'extra' in value:
errors.append('$.extra:extra')
print(errors)Review the design
Review schema-version compatibility and unsupported vocabulary. Structural validity alone does not establish permission, factual correctness or safe execution.
Your task
Return ordered path errors from root $. Support only type (object,array,string,integer,number,boolean,null), enum, closed-object properties/required/additionalProperties=False, array items/maxItems, string maxLength. Unknown or inapplicable schema keywords raise ValueError before checking data. Object children visit sorted keys, missing requirements first; arrays visit index order. Errors use :type,:enum,:missing,:extra,:length. Numeric booleans are not integers/numbers and numbers must be finite. enum uses exact Python type and equality. Omitted additionalProperties means closed. This is an explicit JSON-like subset, not a general JSON Schema implementation.
The explanation opens here once every check passes.