bun-server-validation

📁 dangaogit/bun-server-skills 📅 9 days ago
1
总安装量
1
周安装量
#41471
全站排名
安装命令
npx skills add https://github.com/dangaogit/bun-server-skills --skill bun-server-validation

Agent 安装分布

trae 1
qoder 1
trae-cn 1
cursor 1
claude-code 1

Skill 文档

Bun Server Data Validation

Basic Usage

Define DTO with Validation Rules

import {
  IsString,
  IsEmail,
  IsNumber,
  IsOptional,
  MinLength,
  MaxLength,
  Min,
  Max,
  IsArray,
  IsBoolean,
  IsEnum,
  Matches,
} from "@dangao/bun-server";

class CreateUserDto {
  @IsString()
  @MinLength(2)
  @MaxLength(50)
  name: string;

  @IsEmail()
  email: string;

  @IsNumber()
  @Min(0)
  @Max(150)
  @IsOptional()
  age?: number;

  @IsString()
  @MinLength(8)
  @Matches(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)/)
  password: string;
}

Use in Controller

import { Controller, POST, Body, Validate } from "@dangao/bun-server";

@Controller("/api/users")
class UserController {
  @POST("/")
  @Validate(CreateUserDto)
  createUser(@Body() data: CreateUserDto) {
    return this.userService.create(data);
  }
}

Common Validation Decorators

Type Validation

@IsString()      // Must be string
@IsNumber()      // Must be number
@IsBoolean()     // Must be boolean
@IsArray()       // Must be array
@IsObject()      // Must be object
@IsDate()        // Must be date

String Validation

@IsEmail()                       // Email format
@IsUrl()                         // URL format
@IsUUID()                        // UUID format
@MinLength(5)                    // Minimum length
@MaxLength(100)                  // Maximum length
@Matches(/^[a-z]+$/)             // Regex match
@IsNotEmpty()                    // Non-empty string
@IsAlpha()                       // Letters only
@IsAlphanumeric()                // Letters and numbers

Number Validation

@Min(0)          // Minimum value
@Max(100)        // Maximum value
@IsPositive()    // Positive number
@IsNegative()    // Negative number
@IsInt()         // Integer

Array Validation

@IsArray()
@ArrayMinSize(1)
@ArrayMaxSize(10)
@ArrayUnique()
items: string[];

Enum Validation

enum UserRole {
  ADMIN = "admin",
  USER = "user",
}

class UpdateRoleDto {
  @IsEnum(UserRole)
  role: UserRole;
}

Optional and Conditional

@IsOptional()           // Optional field
@IsNotEmpty()           // Cannot be empty
@ValidateIf(o => o.type === "email")  // Conditional validation

Nested Object Validation

import { ValidateNested, Type } from "@dangao/bun-server";

class AddressDto {
  @IsString()
  street: string;

  @IsString()
  city: string;
}

class CreateUserDto {
  @IsString()
  name: string;

  @ValidateNested()
  @Type(() => AddressDto)
  address: AddressDto;
}

Array Element Validation

class CreateOrderDto {
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => OrderItemDto)
  items: OrderItemDto[];
}

Custom Validators

import { registerDecorator, ValidationArguments } from "@dangao/bun-server";

function IsStrongPassword() {
  return function (object: any, propertyName: string) {
    registerDecorator({
      name: "isStrongPassword",
      target: object.constructor,
      propertyName: propertyName,
      validator: {
        validate(value: any) {
          if (typeof value !== "string") return false;
          return (
            value.length >= 8 &&
            /[a-z]/.test(value) &&
            /[A-Z]/.test(value) &&
            /\d/.test(value)
          );
        },
        defaultMessage() {
          return "Password must be at least 8 characters with uppercase, lowercase and number";
        },
      },
    });
  };
}

// Usage
class RegisterDto {
  @IsStrongPassword()
  password: string;
}

Custom Error Messages

class CreateUserDto {
  @IsString({ message: "Name must be a string" })
  @MinLength(2, { message: "Name must be at least 2 characters" })
  name: string;

  @IsEmail({}, { message: "Please enter a valid email address" })
  email: string;
}

Validation Error Handling

Validation failures throw ValidationError:

import { ValidationError, HttpException } from "@dangao/bun-server";

// Global error handler
function errorHandler(ctx: Context, next: NextFunction) {
  try {
    return next();
  } catch (error) {
    if (error instanceof ValidationError) {
      return new Response(JSON.stringify({
        statusCode: 400,
        message: "Validation failed",
        errors: error.errors,
      }), { status: 400 });
    }
    throw error;
  }
}

Partial Validation (Update Scenarios)

import { PartialType } from "@dangao/bun-server";

// Inherit from CreateUserDto, all fields become optional
class UpdateUserDto extends PartialType(CreateUserDto) {}

// Or define manually
class UpdateUserDto {
  @IsOptional()
  @IsString()
  @MinLength(2)
  name?: string;

  @IsOptional()
  @IsEmail()
  email?: string;
}

Query Parameter Validation

class PaginationDto {
  @IsOptional()
  @IsNumber()
  @Min(1)
  page?: number = 1;

  @IsOptional()
  @IsNumber()
  @Min(1)
  @Max(100)
  limit?: number = 10;
}

@Controller("/api/users")
class UserController {
  @GET("/")
  @Validate(PaginationDto, "query")
  list(@Query() query: PaginationDto) {
    return this.userService.paginate(query.page, query.limit);
  }
}

Related Resources