Skip to content

Releases: lukeed/tschema

v3.1.0

14 Aug 07:41
Compare
Choose a tag to compare

Features

  • Add unknown export: b9ac7b8

    // NOTE: Allows any value!
    // ---
    let x = t.unknown();
    //-> { }
    type X = t.Infer<typeof x>;
    //-> unknown
  • Add partial modifier: 56041e6
    Accepts a t.object and marks all its fields as optional (not required).

    let person1 = t.object({
      name: t.string(),
      age: t.number(),
    });
    type Person1 = t.Infer<typeof person1>;
    //-> {
    //->   name: string;
    //->   age: number;
    //-> }
    
    // NOTE: The input (`person1`) is not modified
    let person2 = t.partial(person1);
    type Person2 = t.Infer<typeof person2>;
    //-> {
    //->   name?: string;
    //->   age?: number;
    //-> }
    
    // NOTE: equivalent to person2
    let person3 = t.object(
      name: t.optional(t.string()),
      age: t.optional(t.number()),
    });
    type Person3 = t.Infer<typeof person3>;
    //-> {
    //->   name?: string;
    //->   age?: number;
    //-> }

Patches

  • (types): Add default object type arg: ac25ad7
    This means that you can now import/extend the t.object type directly.

Chores

  • Update module size: 22ee312
    NOTE: Everything is still tree-shakable!
    In other words, if you don't use the new methods, you don't import those bytes.

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

v3.0.0

07 Aug 17:36
Compare
Choose a tag to compare

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

v2.1.0

06 Aug 23:45
Compare
Choose a tag to compare

Features

  • Add Schema Composition utilities (#2): 04b22b5, c2a7394

    t.one(t.string(), t.number()); 
    //-> {
    //->   oneOf: [
    //->     { type: 'string' },
    //->     { type: 'number' },
    //->   ]
    //-> }
    
    t.any(t.string(), t.number()); 
    //-> {
    //->   anyOf: [
    //->     { type: 'string' },
    //->     { type: 'number' },
    //->   ]
    //-> }
    
    t.all(
      t.string({ minLength: 2 }), 
      t.string({ maxLength: 40 }),
    ); 
    //-> {
    //->   allOf: [
    //->     { type: 'string', minLength: 2 },
    //->     { type: 'string', maxLength: 40 },
    //->   ]
    //-> }
    
    t.not(t.string())
    //-> {
    //->  not: {
    //->    type: 'string'
    //->  }
    //->}

Chores

  • fix(ci): Add explicit registry-url to npm publisher: 08bae75
  • remove stale TODO note: 1614ac7

Full Changelog: v2.0.1...v2.1.0

v2.0.1

06 Aug 21:38
Compare
Choose a tag to compare

Patches

  • fix(tsc): Add missing t.Infer inference guards: 9fdf15b
    Solves the Type instantiation is excessively deep and possibly infinite error

Chores

  • Add benchmark: 15c18ba
  • fix(ci): publish to npm from /npm directory: 1efa30f

Full Changelog: v2.0.0...v2.0.1