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