Releases: lukeed/tschema
v3.2.0
Features
-
Add
t.dict
export: 20d2bbd, d40b952
Adds a JSON Schema builder for theRecord<string, T>
TypeScript equivalent.import * as t from 'tschema'; let names = t.dict(t.string()); type Names = t.Infer<typeof names>; //-> Record<string, string> let mates = t.dict( t.object({ name: t.string(), age: t.integer(), }) ); type Mates = t.Infer<typeof mates>; //-> Record<string, { //-> name: string; //-> age: number; //-> }>
Patches
- Add explicit return type for
t.partial
signature: 11b12c3
Chores
- Add type assertion tests for readonly arrays w/ enums (#10): 09c541e
- (CI) Use Deno 2.0 in Actions: 0bb98d0, e50e379, 70f964f
- (CI) Add
Publish (dry-run)
step: 5bcb4ef
Full Changelog: v3.1.0...v3.2.0
v3.1.0
Features
-
// NOTE: Allows any value! // --- let x = t.unknown(); //-> { } type X = t.Infer<typeof x>; //-> unknown
-
Add
partial
modifier: 56041e6
Accepts at.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 thet.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
Breaking
-
(
t.object
) AddadditionalProperties: false
by default when properties are defined (#5): bc4dc26
This is breaking because whenadditionalProperties
is left unspecified, JSON schema treats it asadditionalProperties: 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 addadditionalProperties: 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 default2.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'st.tuple()
default output has been changed.
However,minItems
andmaxItems
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
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
Full Changelog: v2.0.1...v2.1.0