Getting Started with Luq
Luq is a lightweight, type-first validation library for TypeScript. Get started in minutes with your existing types.
Type-First
Use existing TypeScript types
Tree-Shakeable
Import only what you need
Type-Safe
Full TypeScript support
Prerequisites
Before installing Luq, ensure you have:
- Node.js 14.0 or higher
- TypeScript 4.1 or higher
- Strict mode enabled in your
tsconfig.json
(recommended)
Installation
Install Luq using your preferred package manager:
npm install @maroonedog/luq
TypeScript Configuration
For the best development experience, ensure your tsconfig.json
includes:
{
"compilerOptions": {
"strict": true,
"strictNullChecks": true,
"esModuleInterop": true,
"moduleResolution": "node",
"target": "ES2015" // or higher
}
}
Basic Usage
Luq follows a type-first approach. Start with your existing TypeScript types and build validators using plugins:
import { Builder } from '@maroonedog/luq';
import { requiredPlugin } from '@maroonedog/luq/plugins/required';
import { stringMinPlugin } from '@maroonedog/luq/plugins/stringMin';
import { numberMinPlugin } from '@maroonedog/luq/plugins/numberMin';
import { stringEmailPlugin } from '@maroonedog/luq/plugins/stringEmail';
// 1. Define your TypeScript type
type User = {
name: string;
email: string;
age: number;
};
// 2. Create a validator with only the plugins you need
const userValidator = Builder()
.use(requiredPlugin)
.use(stringMinPlugin)
.use(stringEmailPlugin)
.use(numberMinPlugin)
.for<User>()
.v('name', b => b.string.required().min(2))
.v('email', b => b.string.required().email())
.v('age', b => b.number.required().min(18))
.build();
// 3. Validate your data
const result = userValidator.validate({
name: 'John Doe',
email: 'john@example.com',
age: 25
});
if (result.isValid()) {
console.log('Valid user:', result.value);
} else {
console.log('Validation errors:', result.getErrors());
}
Key Concepts
Plugin Architecture
Each validation rule is a separate plugin. Import only what you need for optimal bundle size.
Type-First Design
Use existing TypeScript types instead of defining schemas. No duplication needed.
Builder Pattern
Fluent API with full TypeScript support and intelligent autocomplete.