Skip to content

v3.0.0

Compare
Choose a tag to compare
@lukeed lukeed released this 07 Aug 17:36
· 15 commits to main since this release

Breaking

  • (t.object) Add additionalProperties: false by default when properties are defined (#5): bc4dc26
    This is breaking because when additionalProperties is left unspecified, JSON schema treats it as additionalProperties: true.
    It was changed to match the "schema" concept, in that users generally only want to allow the properties known/defined in their schema. To allow extra properties, simply add additionalProperties: true in the options parameter:

    // Accept any object
    t.object(); 
    //-> { type: "object" }
    
    // Accept an object of defined shape
    t.object({
      name: t.string(),
    });
    //-> {
    //->   type: "object",
    //->   additionalProperties: false, // <<
    //->   required: ["name"],
    //->   properties: {
    //->     name: { type: "string" }
    //->   }
    //-> }
    
    // Accept an object of given shape, but allow extra properties
    t.object({
      name: t.string(),
    }, {
      additionalProperties: true,
    });
    //-> {
    //->   type: "object",
    //->   additionalProperties: true, // <<
    //->   required: ["name"],
    //->   properties: {
    //->     name: { type: "string" }
    //->   }
    //-> }
  • (t.tuple) Modify JSON schema output to enforce tuple length (#7): bf3663a
    While this could be classified as a patch fix, it's technically breaking as the default 2.x output generated looser JSON schema tuple definitions which, by default, allowed for fewer and/or extra items to still be considered valid. This doesn't match the TypeScript (or most languages') concept of a tuple, so this release's t.tuple() default output has been changed.
    However, minItems and maxItems are still configurable thru options if you wish to change them.

    t.tuple([t.string(), t.number()]);
    
    // Before (2.x)
    // Allows any array length
    //-> {
    //->   type: "array",
    //->   prefixItems: [
    //->     { type: "string" },
    //->     { type: "number" },
    //->   }]
    //-> }
    
    // After (3.0)
    // Requires 2 items
    //-> {
    //->   type: "array",
    //->   prefixItems: [
    //->     { type: "string" },
    //->     { type: "number" },
    //->   }],
    //->   minItems: 2,
    //->   maxItems: 2
    //-> }

Chores

  • Add zod-to-json-schema to benchmark: 4a27c67

Full Changelog: v2.1.0...v3.0.0