npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

ts-to-laravel-validate

v0.1.1

Published

Convert TypeScript interfaces/types into Laravel validation rule arrays.

Readme

ts2laravel-validate

Convert TypeScript interfaces and types into Laravel validation rule arrays.

Features

  • 🔄 Automatically converts TypeScript types to Laravel validation rules
  • 📝 Support for JSDoc @laravel annotations for custom rules
  • 🎯 Handles enums, unions, arrays, nested objects, and optional fields
  • 🔧 CLI and programmatic API
  • 📦 Output as PHP array or JSON format

Installation

npm install ts-to-laravel-validate

Quick Start

CLI Usage

# Generate validation rules from a TypeScript interface
npx ts2laravel-validate --file src/types/User.ts --type CreateUser

# Save to a file
npx ts2laravel-validate --file src/types/User.ts --type CreateUser --out validation/user.php

# Output as JSON
npx ts2laravel-validate --file src/types/User.ts --type CreateUser --format json

Programmatic Usage

import { generateLaravelRules } from 'ts-to-laravel-validate'

const rules = generateLaravelRules({
  entryFile: 'src/types/User.ts',
  rootTypeName: 'CreateUser',
  outFormat: 'php', // or 'json'
  strictArrays: false
})

console.log(rules)

Type Mapping

TypeScript types are automatically mapped to Laravel validation rules:

| TypeScript Type | Laravel Rule | |----------------|--------------| | string | string | | number | numeric | | bigint | integer | | boolean | boolean | | Date | date | | string[] | array with string elements | | 'a' \| 'b' \| 'c' | in:a,b,c | | field? | sometimes | | field: T \| null | nullable |

Examples

Basic Example

// src/types/User.ts
export interface CreateUser {
  name: string
  email: string
  age?: number
  role: 'admin' | 'user' | 'guest'
}

Output:

return [
  'name' => 'required|string',
  'email' => 'required|string',
  'age' => 'sometimes|numeric',
  'role' => 'required|in:admin,user,guest',
];

Using JSDoc Annotations

You can add custom Laravel validation rules using @laravel JSDoc comments:

export interface CreateUser {
  /** @laravel required|string|min:3|max:50 */
  name: string

  /** @laravel required|email|unique:users,email */
  email: string

  /** @laravel required|string|min:8|confirmed */
  password: string

  age?: number
}

Output:

return [
  'name' => 'required|string|min:3|max:50',
  'email' => 'required|email|unique:users,email',
  'password' => 'required|string|min:8|confirmed',
  'age' => 'sometimes|numeric',
];

Enums and Unions

export enum Role {
  Admin = "admin",
  User = "user"
}

export interface CreateUser {
  role: Role | 'guest'
  status: 'active' | 'inactive' | 'pending'
}

Output:

return [
  'role' => 'required|in:admin,user,guest',
  'status' => 'required|in:active,inactive,pending',
];

Arrays and Nested Objects

export interface CreateUser {
  name: string
  tags?: string[]
  meta?: {
    newsletter: boolean
    score: number | null
  }
  profiles?: Profile[]
}

export interface Profile {
  /** @laravel string|max:140 */
  bio?: string | null
  birthDate?: Date | null
}

Output:

return [
  'name' => 'required|string',
  'tags' => 'sometimes|array',
  'tags.*' => 'string',
  'meta' => 'sometimes|array',
  'meta.newsletter' => 'required|boolean',
  'meta.score' => 'required|numeric',
  'profiles' => 'sometimes|array',
  'profiles.*' => 'array',
  'profiles.*.bio' => 'sometimes|string|max:140',
  'profiles.*.birthDate' => 'sometimes|date',
];

CLI Options

ts2laravel-validate [options]

Options:
  -f, --file <path>           TypeScript entry file (required)
  -t, --type <name>           Root exported interface/type (required)
  -p, --project <tsconfig>    Path to tsconfig.json
  -o, --out <path>            Write output to file
  --format <php|json>         Output format (default: "php")
  --strict-arrays             Add 'present' to array parents (default: false)
  -h, --help                  Display help

API Options

interface GenerateOptions {
  entryFile: string        // Path to TypeScript file
  rootTypeName: string     // Name of the exported type/interface
  tsconfigPath?: string    // Optional path to tsconfig.json
  outFormat?: 'php' | 'json'  // Output format (default: 'php')
  strictArrays?: boolean   // Add 'present' rule to arrays (default: false)
}

Integration with Vue/TypeScript Projects

Add to package.json scripts

{
  "scripts": {
    "generate:validation": "ts2laravel-validate --file src/types/User.ts --type CreateUserDTO --out ../backend/validation/user.php"
  }
}

Use in Build Process

// scripts/generate-validation.ts
import { generateLaravelRules } from 'ts-to-laravel-validate'
import { writeFileSync } from 'fs'
import { glob } from 'glob'

// Generate validation for all DTO files
const dtoFiles = glob.sync('src/types/*DTO.ts')

dtoFiles.forEach(file => {
  const typeName = file.match(/\/(\w+)\.ts$/)?.[1]
  if (!typeName) return

  const rules = generateLaravelRules({
    entryFile: file,
    rootTypeName: typeName,
    outFormat: 'php'
  })

  writeFileSync(`../backend/validation/${typeName}.php`, rules)
})

How It Works

  1. Parses TypeScript using ts-morph
  2. Analyzes types to determine appropriate Laravel validation rules
  3. Expands enums to their literal values for in: rules
  4. Handles unions by combining string/number literals into validation lists
  5. Processes nested structures using Laravel's dot notation
  6. Merges custom rules from @laravel JSDoc comments

Limitations

  • Only processes exported types/interfaces
  • Limited to types that map to Laravel validation rules
  • Complex conditional types may not be supported

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.