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"}); // β Failuretuple
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); // β FailuretsEnum
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); // β Failurelazy
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"]); // β
Successintersection
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({}); // β Failureobject
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({}); // β Failureproperty
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({}); // β
Successpartial
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({}); // β
Successrequired
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({}); // β Failurepick
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({}); // β Failureomit
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({}); // β Failureliteral
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.blueundefined
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); // β Failurenullable
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); // β Failurenumber
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({}); // β Failurestring
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([]); // β Failureboolean
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); // β Failureunknown
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); // β
Successany
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); // β
Successkeyof
Gets the keys of a record.
// TBDandThen
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); // β
SuccessdiscriminatedUnion
Same as union but only check the member schema that makes sense for the input.
// TODO