Pipes Guide
Overview
Pipes validate and transform input data before it reaches your tool handler.
Creating a Pipe
import { Pipe, PipeInterface } from 'nitrostack';
@Pipe()
export class ValidationPipe implements PipeInterface {
transform(value: any, metadata?: any): any {
// Validate and transform
if (!value || typeof value !== 'object') {
throw new Error('Invalid input: must be an object');
}
return value;
}
}
Using Pipes
@Tool({ name: 'create_user' })
@UsePipes(ValidationPipe, TransformPipe)
async createUser(input: any, ctx: ExecutionContext) {
// Input has been validated and transformed
}
Pipe Examples
Trim Strings
@Pipe()
export class TrimPipe implements PipeInterface {
transform(value: any): any {
if (typeof value === 'string') {
return value.trim();
}
if (Array.isArray(value)) {
return value.map(item => this.transform(item));
}
if (value && typeof value === 'object') {
const trimmed: any = {};
for (const [key, val] of Object.entries(value)) {
trimmed[key] = this.transform(val);
}
return trimmed;
}
return value;
}
}
Parse Integers
@Pipe()
export class ParseIntPipe implements PipeInterface {
transform(value: any): number {
const parsed = parseInt(value, 10);
if (isNaN(parsed)) {
throw new Error(`Cannot parse "${value}" as integer`);
}
return parsed;
}
}