Master TypeBox schema validation for Google Antigravity IDE high-performance APIs
# TypeBox Schema Validation for Google Antigravity IDE
Build high-performance validation with TypeBox using Google Antigravity IDE. This guide covers schema creation, validation, and integration with Fastify and Elysia.
## Schema Definition
```typescript
// src/schemas/user.ts
import { Type, Static } from "@sinclair/typebox";
export const UserSchema = Type.Object({
id: Type.String({ format: "uuid" }),
email: Type.String({ format: "email" }),
name: Type.String({ minLength: 2, maxLength: 100 }),
role: Type.Union([Type.Literal("user"), Type.Literal("admin"), Type.Literal("moderator")]),
avatar: Type.Optional(Type.String({ format: "uri" })),
settings: Type.Object({
theme: Type.Union([Type.Literal("light"), Type.Literal("dark"), Type.Literal("system")]),
notifications: Type.Boolean(),
language: Type.String({ pattern: "^[a-z]{2}(-[A-Z]{2})?$" }),
}),
createdAt: Type.String({ format: "date-time" }),
updatedAt: Type.String({ format: "date-time" }),
});
export type User = Static<typeof UserSchema>;
export const CreateUserSchema = Type.Omit(UserSchema, ["id", "createdAt", "updatedAt"]);
export type CreateUser = Static<typeof CreateUserSchema>;
export const UpdateUserSchema = Type.Partial(Type.Omit(UserSchema, ["id", "createdAt", "updatedAt"]));
export type UpdateUser = Static<typeof UpdateUserSchema>;
export const UserResponseSchema = Type.Object({
success: Type.Boolean(),
data: UserSchema,
});
export const UsersListSchema = Type.Object({
success: Type.Boolean(),
data: Type.Array(UserSchema),
meta: Type.Object({
total: Type.Integer({ minimum: 0 }),
page: Type.Integer({ minimum: 1 }),
limit: Type.Integer({ minimum: 1, maximum: 100 }),
}),
});
```
## Validation with TypeCompiler
```typescript
// src/lib/validate.ts
import { TypeCompiler, ValueError } from "@sinclair/typebox/compiler";
import { TSchema } from "@sinclair/typebox";
export function createValidator<T extends TSchema>(schema: T) {
const compiled = TypeCompiler.Compile(schema);
return {
validate: (data: unknown): data is Static<T> => compiled.Check(data),
errors: (data: unknown): ValueError[] => [...compiled.Errors(data)],
parse: (data: unknown): Static<T> => {
if (compiled.Check(data)) return data;
const errors = [...compiled.Errors(data)];
throw new ValidationError(errors);
},
};
}
class ValidationError extends Error {
constructor(public errors: ValueError[]) {
super("Validation failed");
this.name = "ValidationError";
}
toResponse() {
return {
success: false,
errors: this.errors.map((e) => ({
path: e.path,
message: e.message,
value: e.value,
})),
};
}
}
```
## Elysia Integration
```typescript
// src/routes/users.ts
import { Elysia, t } from "elysia";
import { UserSchema, CreateUserSchema, UpdateUserSchema } from "@/schemas/user";
export const userRoutes = new Elysia({ prefix: "/users" })
.get("/", async () => {
const users = await db.user.findMany();
return { success: true, data: users };
}, {
response: t.Object({
success: t.Boolean(),
data: t.Array(UserSchema),
}),
})
.post("/", async ({ body }) => {
const user = await db.user.create({ data: body });
return { success: true, data: user };
}, {
body: CreateUserSchema,
response: t.Object({ success: t.Boolean(), data: UserSchema }),
})
.patch("/:id", async ({ params, body }) => {
const user = await db.user.update({ where: { id: params.id }, data: body });
return { success: true, data: user };
}, {
params: t.Object({ id: t.String({ format: "uuid" }) }),
body: UpdateUserSchema,
response: t.Object({ success: t.Boolean(), data: UserSchema }),
});
```
## Best Practices for Google Antigravity IDE
When using TypeBox with Google Antigravity, use TypeCompiler for maximum performance. Create reusable schema compositions. Generate OpenAPI from schemas. Let Gemini 3 generate TypeBox schemas from TypeScript interfaces.
Google Antigravity excels at converting existing types to TypeBox schemas.This TypeBox prompt is ideal for developers working on:
By using this prompt, you can save hours of manual coding and ensure best practices are followed from the start. It's particularly valuable for teams looking to maintain consistency across their typebox implementations.
Yes! All prompts on Antigravity AI Directory are free to use for both personal and commercial projects. No attribution required, though it's always appreciated.
This prompt works excellently with Claude, ChatGPT, Cursor, GitHub Copilot, and other modern AI coding assistants. For best results, use models with large context windows.
You can modify the prompt by adding specific requirements, constraints, or preferences. For TypeBox projects, consider mentioning your framework version, coding style, and any specific libraries you're using.