typespec1-9 Exercise 1: A Member Schema and Seven Documents ==================================================================== member.tsp (TypeSpec 1.17.0, @typespec/json-schema 1.17.0): import "@typespec/json-schema"; using TypeSpec.JsonSchema; @jsonSchema namespace Club; enum Tier { bronze, silver, gold } model Member { @pattern("^[A-Z]{2}[0-9]{4}$") code: string; @minLength(2) @maxLength(40) name: string; @minValue(16) @maxValue(120) age: int32; tier: Tier = Tier.bronze; @format("email") email?: string; visits: int64; } The emitted schema (JSON, from the file-type: json config), properties only in summary: code string, pattern ^[A-Z]{2}[0-9]{4}$ name string, minLength 2, maxLength 40 age integer, minimum 16, maximum 120 tier $ref Tier.json, default bronze email string, format email (not required) visits string (int64 becomes a string by default) required: code, name, age, tier, visits validate_member.py (jsonschema 4.26.0) results: --- without a format checker a valid member valid code in lower case 'ab1234' does not match '^[A-Z]{2}[0-9]{4}$' age 15 15 is less than the minimum of 16 visits sent as a number 12 is not of type 'string' tier left out (it has a default) 'tier' is a required property tier 'platinum' 'platinum' is not one of ['bronze', 'silver', 'gold'] email 'not-an-email' valid --- with FormatChecker() a valid member valid code in lower case 'ab1234' does not match '^[A-Z]{2}[0-9]{4}$' age 15 15 is less than the minimum of 16 visits sent as a number 12 is not of type 'string' tier left out (it has a default) 'tier' is a required property tier 'platinum' 'platinum' is not one of ['bronze', 'silver', 'gold'] email 'not-an-email' 'not-an-email' is not a 'email' WHAT SURPRISED ME 1. 'tier' is REQUIRED although it has a default: leaving it out fails validation. The schema records the default but does not make the property optional (chapter 3's rule, now in JSON Schema). 2. 'visits' sent as the number 12 is rejected. The int64 strategy made it a string. A client that sends a number is wrong according to this schema. 3. 'not-an-email' passes unless the validator is given a format checker. In JSON Schema, 'format' is an annotation by default; checking it is the validator's choice. With FormatChecker() it was rejected. (I tried only the email format.) WHY THIS WORKS AS AN ANSWER --------------------------- All seven documents were chosen to hit one decorator or one default each, so every result is traceable to a line of the TypeSpec. The three surprises are things the TypeSpec does not show you; only running a validator does.