📚 Examples

Real-world usage patterns

Array of Objects

import { Interface } from "fortify-schema";

const CourseSchema = Interface({
  id: "string",
  title: "string",
  videos: [{
    id: "string",
    title: "string",
    duration: "number",
    url: "url.https"
  }]
});

// Valid
const course = {
  id: "course-1",
  title: "TypeScript Mastery",
  videos: [
    { id: "v1", title: "Intro", duration: 120, url: "https://example.com/v1.mp4" },
    { id: "v2", title: "Advanced", duration: 180, url: "https://example.com/v2.mp4" }
  ]
};

User Management

const UserSchema = Interface({
  id: "uuid",
  email: "email",
  name: "string(2,50)",
  role: Make.union("admin", "user", "guest"),
  profile: {
    bio: "string?",
    avatar: "url?",
    social: {
      twitter: "string?",
      github: "string?"
    }
  },
  createdAt: "date"
});

API Endpoint Validation

// Create endpoint
const CreatePostSchema = Interface({
  title: "string(1,200)",
  content: "string(1,10000)",
  tags: "string[]?",
  published: "boolean?"
});

// Update endpoint (partial)
const UpdatePostSchema = Mod.partial(CreatePostSchema);

// Use in Express
app.post('/posts', (req, res) => {
  const result = CreatePostSchema.safeParse(req.body);
  if (!result.success) {
    return res.status(400).json({ errors: result.errors });
  }
  // result.data is fully typed!
  const post = result.data;
});