Skip to content

array

The value must be an array and all items must pass the given schema.

 
import { t, Infer } from "typegate";
 
const schema = t.array(t.string);
type Schema = Infer<typeof schema>;
//     πŸ”Ž string[]
 
schema.parse(["abc","def"]);   // βœ… Success 
schema.parse(["abc", 12]);     // ❌ Failure
schema.parse([]);              // βœ… Success
schema.parse([{}, {}, {}]);    // ❌ Failure
schema.parse({name: "bob"});   // ❌ Failure

tuple

The value must be an array of N items. Each item must pass the schema corresponding to its position.

 
import { t, Infer } from "typegate";
 
const schema = t.tuple(t.string, t.number);
type Schema = Infer<typeof schema>;
//     πŸ”Ž [string, number]
 
schema.parse(["abc", 123]);   // βœ… Success
schema.parse(["abc", "def"]); // ❌ Failure
schema.parse(["abc"])         // ❌ Failure
schema.parse([])              // ❌ Failure
schema.parse(null);           // ❌ Failure

tsEnum

The value must be a member of

 
import { t, Infer } from "typegate";
 
enum NumberBasedColor { red, blue };
const schema = t.enum(NumberBasedColor);
type Schema = Infer<typeof schema>;
//     πŸ”Ž NumberBasedColor
 
schema.parse(NumberBasedColor.red); // βœ… Succcess
schema.parse(0);                    // βœ… Success
schema.parse("red");                // ❌ Failure
schema.parse(null);                 // ❌ Failure
 
enum StringBasedColor { red = "red", blue = "blue"};
const schema2 = t.enum(StringBasedColor);
type Schema2 = Infer<typeof schema2>;
//     πŸ”Ž StringBasedColor
 
schema2.parse(StringBasedColor.red); // βœ… Succcess
schema2.parse("red");                // βœ… Succcess
schema2.parse(0);                    // ❌ Failure
schema2.parse(null);                 // ❌ Failure

lazy

Useful when you want to define a schema that calls itself.

The type annonation is mandatory and inference is unavailable unfortunately.

 
import { t, Infer } from "typegate";
 
type Person = {
  name: string;
  father?: Person; // This type is recursive.
};
 
const schema = t.object(
  t.property('name', t.string),
  t.optionalProperty('father', t.lazy<Person>(() => schema)),
);
 
type Schema = Infer<typeof schema>;
//     πŸ”Ž { name: string, father?: Person | undefined }

noCheck

noCheck accepts a Type and will return a parser that always succeeds to parse.

It is not validating anything.

It's useful when you are migrating big types and one of the type does not have a parser yet.

 
import { t, Infer } from "typegate";
 
const schema = t.noCheck<number>();
type Schema = Infer<typeof schema>;
//     πŸ”Ž number
 
schema.parse(123);            // βœ… Success
schema.parse(null);           // βœ… Success  
schema.parse(["abc", "def"]); // βœ… Success
 
// ----------------------------------------------
 
type Person = { name: string, age: number};
const personSchema = t.noCheck<Person>();
type InferredPerson = Infer<typeof personSchema>;
//       πŸ”Ž Person
 
personSchema.parse(123);            // βœ… Success
personSchema.parse(null);           // βœ… Success  
personSchema.parse(["abc", "def"]); // βœ… Success
personSchema.parse(["abc", "def"]); // βœ… Success

intersection

Merge two object schemas together.

 
import { t, Infer } from "typegate"; 
 
const withName = t.object(t.property('name', t.string));
const withAge = t.object(t.property('age', t.number));
 
const schema = t.intersection(withName, withAge);
type Schema = Infer<typeof schema>;
//     πŸ”Ž { name: string, age: number }
 
schema.parse({ name: 'mike', age: 24 }); // βœ… Success
schema.parse({ name: 'mike' });          // ❌ Failure
schema.parse({ age: 24 });               // ❌ Failure
schema.parse({});                        // ❌ Failure

object

Define an object that has some properties and optional properties.

 
import { t, Infer } from "typegate"; 
 
const schema = t.object(
    t.property('name', t.string),
    t.property('age', t.number),
    t.optionalProperty('isSmoker', t.boolean),
);
type Schema = Infer<typeof schema>;
//     πŸ”Ž { name: string, age: number, isSmoker?: boolean }
 
schema.parse({ name: 'mike', age: 24, isSmoker: true }); // βœ… Success
schema.parse({ name: 'mike', age: 24 });                 // βœ… Success
schema.parse({ age: 24 });                               // ❌ Failure
schema.parse({});                                        // ❌ Failure

property

Define a required property on an object. Meaning the key is always present and the value must match the schema.

 
import { t, Infer } from "typegate"; 
 
const schema = t.object(
    t.property('name', t.string),
);
type Schema = Infer<typeof schema>;
//     πŸ”Ž { name: string }
 
schema.parse({ name: 'mike' }); // βœ… Success
schema.parse({ name: 123 });    // ❌ Failure
schema.parse({});               // ❌ Failure 

optionalProperty

Define an optional property on an object. Meaning the key can be omited but given its present the value must match the schema.

 
import { t, Infer } from "typegate"; 
 
const schema = t.object(
    t.optionalProperty('name', t.string),
);
type Schema = Infer<typeof schema>;
//     πŸ”Ž { name?: string }
 
schema.parse({ name: 'Mike' }); // βœ… Success
schema.parse({ name: 123 });    // ❌ Failure
schema.parse({});               // βœ… Success

partial

Make all properties of an object optional.

 
import { t, Infer } from "typegate";
 
const person = t.object(
  t.property('name', t.string),
  t.property('age', t.number),
);
 
const schema = t.partial(person);
type Schema = Infer<typeof schema>;
//     πŸ”Ž { name?: string, age?: number };
 
schema.parse({ name: 'Bob', age: 2 }); // βœ… Success
schema.parse({ name: 'Mike' });        // βœ… Success
schema.parse({ age: 24 });             // βœ… Success
schema.parse({});                      // βœ… Success

required

Make all properties of an object required.

 
import { t, Infer } from "typegate";
 
const person = t.object(
  t.optionalProperty('name', t.string),
  t.optionalProperty('age', t.number),
);
 
const schema = t.required(person);
type Schema = Infer<typeof schema>;
//     πŸ”Ž { name: string, age: number };
 
schema.parse({ name: 'Bob', age: 24 }); // βœ… Success
schema.parse({ name: 'Mike' });          // ❌ Failure
schema.parse({ age: 24 });               // ❌ Failure
schema.parse({});                        // ❌ Failure

pick

Only keep the mentionned properties of an object.

 
import { t, Infer } from "typegate";
 
const person = t.object(
  t.property('name', t.string),
  t.property('age', t.number),
);
 
const schema = t.pick(person, ['name']);
type Schema = Infer<typeof schema>;
//     πŸ”Ž { name: string };
 
schema.parse({ name: 'Mike' }); // βœ… Success
schema.parse({ age: 24 });      // ❌ Failure
schema.parse({});               // ❌ Failure

omit

Remove the mentionned properties of an object.

 
import { t, Infer } from "typegate";
 
const person = t.object(
  t.property('name', t.string),
  t.property('age', t.number),
);
 
const schema = t.omit(person, ['name']);
type Schema = Infer<typeof schema>;
//     πŸ”Ž { age: number };
 
schema.parse({ name: 'mike' }); // ❌ Failure
schema.parse({ age: 24 });      // βœ… Success
schema.parse({});               // ❌ Failure

literal

You can use literal when you know the value is supposed to be a specific one.

 
import { t, Infer } from "typegate";
 
const schema = t.literal(1);
type Schema = Infer<typeof schema>;
//     πŸ”Ž 1
 
schema.parse(1);     // βœ… Success
schema.parse(true); // ❌ Failure
 
enum Color { red, blue };
 
const schema2 = t.literal(Color.blue);
 
schema2.parse(Color.blue); // βœ… Success
schema2.parse(Color.red);  // ❌ Failure
 
type Schema2 = Infer<typeof schema2>;
//     πŸ”Ž Color.blue

undefined

A schema that passes for "undefined" and nothing else.

 
import { t, Infer } from "typegate";
 
const schema = t.undefined;
//      πŸ”Ž undefined
 
schema.parse(undefined); // βœ… Success
schema.parse(null);      // ❌ Failure
schema.parse(false);     // ❌ Failure

nullable

A schema that passes for "null" and nothing else.

 
import { t, Infer } from "typegate";
 
const schema = t.null;
//      πŸ”Ž null
 
schema.parse(null);      // βœ… Success
schema.parse(undefined); // ❌ Failure
schema.parse(false);     // ❌ Failure

number

A schema that passes for "number" and nothing else.

 
import { t, Infer } from "typegate";
 
const schema = t.number;
//      πŸ”Ž number
 
schema.parse(123);   // βœ… Success
schema.parse(1.01);  // βœ… Success
schema.parse(1_000); // βœ… Success
schema.parse("123"); // ❌ Failure
schema.parse({});    // ❌ Failure

string

A schema that passes for "string" and nothing else.

 
import { t, Infer } from "typegate";
 
const schema = t.string;
//      πŸ”Ž string
 
schema.parse("abc");     // βœ… Success
schema.parse("");        // βœ… Success
schema.parse(123);       // ❌ Failure
schema.parse(undefined); // ❌ Failure
schema.parse([]);        // ❌ Failure

boolean

A schema that passes for "boolean" and nothing else.

 
import { t, Infer } from "typegate";
 
const schema = t.boolean;
//      πŸ”Ž boolean
 
schema.parse(true);     // βœ… Success
schema.parse(false);    // βœ… Success
schema.parse(1);        // ❌ Failure

unknown

A schema that passes for everything and sets the type to "unknown".

 
import { t, Infer } from "typegate";
 
const schema = t.unknown;
//      πŸ”Ž unknown
 
schema.parse(true);  // βœ… Success
schema.parse(false); // βœ… Success
schema.parse(1);     // βœ… Success
schema.parse(null);  // βœ… Success

any

A schema that passes for everything and sets the type to "any".

 
import { t, Infer } from "typegate";
 
const schema = t.any;
//      πŸ”Ž any
 
schema.parse(true);  // βœ… Success
schema.parse(false); // βœ… Success
schema.parse(1);     // βœ… Success
schema.parse(null);  // βœ… Success

keyof

Gets the keys of a record.

 
// TBD

andThen

 

union

Schema to validate that the data is a member of an union.

 
import { t, Infer } from "typegate";
 
const schema = t.union(t.string, t.number);
//      πŸ”Ž string | number
 
schema.parse("abc"); // βœ… Success
schema.parse(123);   // ❌ Failure
schema.parse(123);   // βœ… Success

discriminatedUnion

Same as union but only check the member schema that makes sense for the input.

 
// TODO
HTTPS Β· www.typegate.dev
← Home