I Put `minimum` in My JSON Schema. ShapeCraft Ignored It. That Was By Design
I was extracting customer details from onboarding messages. The result needed a name, an age, and a country code before it could be passed to the account service. The schema looked strict enough: const PersonJsonSchema = { type : " object " , required : [ " name " , " age " , " country " ], properties : { name : { type : " string " , minLength : 3 }, age : { type : " number " , minimum : 18 ,…
The author extracted customer details from onboarding messages, requiring a name, age, and country code. They created a JSON Schema with required fields and specific constraints, including minimum age, maximum age, and uppercase country codes. However, when a test message contained a 12-year-old, the response was still valid JSON and passed through the account service.
The author initially tried to fix the issue by adding more instructions to the prompt, but this did not guarantee adherence to the constraints. They used ShapeCraft to parse and validate the response, assuming the minimum and pattern keywords were enforced there. When the minimum and pattern keywords were not enforced, the author initially treated it as a bug, but later realized it was a deliberate design choice.
The built-in JSON Schema checker only checks types, enum membership, required fields, nested properties, and array items. It does not implement every possible JSON Schema keyword, which was a better design boundary than silently having each backend behave differently. The author then used AJV to create a strict validator for the required keywords. They compiled the schema once and passed a function to generate() to validate the parsed value.
The custom validator only affects validation for inputs with the { jsonSchema } format. Zod schemas remain unaffected. The custom validator only changes validation for inputs with the { jsonSchema } format, while Zod schemas continue to work as intended.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.