Skip to content

ParseUUIDPipeOptions

reference
2 min readUpdated

Kind: Interface

Source: packages/common/pipes/parse-uuid.pipe.ts

Part of: Common

ParseUUIDPipeOptions configures the behavior of NestJS's ParseUUIDPipe, which validates incoming values as UUIDs. It lets callers restrict accepted UUID versions, customize validation errors, provide a custom exception factory, and allow optional values to bypass validation.

Properties

PropertyType
version`'3'
errorHttpStatusCodeErrorHttpStatusCode
exceptionFactory(errors: string) => any
optionalboolean

Diagram

mermaid
graph LR
  A[Incoming route value] --> B[ParseUUIDPipe]
  B --> C{Value is optional<br/>and empty?}
  C -->|Yes| D[Return value unchanged]
  C -->|No| E{Valid UUID?}
  E -->|Yes| F[Return validated UUID]
  E -->|No| G[Create validation exception]

  H[ParseUUIDPipeOptions] --> B
  H --> I[version: 3 | 4 | 5 | 7]
  H --> J[optional: boolean]
  H --> K[errorHttpStatusCode]
  H --> L[exceptionFactory]
  K --> G
  L --> G

Usage

ts
import {
  BadRequestException,
  ParseUUIDPipe,
} from '@nestjs/common';

const uuidPipe = new ParseUUIDPipe({
  version: '4',
  optional: false,
  exceptionFactory: (errors) =>
    new BadRequestException({
      message: 'Invalid user identifier',
      errors,
    }),
});

// Example controller usage
@Get(':id')
findOne(
  @Param('id', uuidPipe) id: string,
) {
  return this.usersService.findOne(id);
}

AI Coding Instructions

  • Use version to enforce the UUID format expected by the API, such as '4' for randomly generated identifiers.
  • Set optional: true only for parameters that may legitimately be absent; valid non-empty values must still be UUIDs.
  • Prefer exceptionFactory when the application requires a shared or custom error response format.
  • Use errorHttpStatusCode for standard HTTP error customization when a custom exception body is unnecessary.
  • Apply ParseUUIDPipe at controller boundaries (@Param, @Query, or @Body) before passing identifiers to services or repositories.

How it works

Type

ParseUUIDPipeOptions is the optional configuration interface accepted by ParseUUIDPipe’s constructor. The pipe stores these options and reads version, exceptionFactory, errorHttpStatusCode, and optional during construction or transformation. packages/common/pipes/parse-uuid.pipe.ts:17-38 packages/common/pipes/parse-uuid.pipe.ts:59-71

Members

Validation behavior controlled by the options

The accepted UUID patterns require hyphenated hexadecimal text with 8-4-4-4-12 groups. Version-specific patterns require 3, 4, 5, or 7 at the beginning of the third group; versions 4, 5, and 7 also require 8, 9, A, or B at the beginning of the fourth group. Matching is case-insensitive. packages/common/pipes/parse-uuid.pipe.ts:49-55

Except for the optional null/undefined path, a value must be a string before pattern matching. Non-string values cause the configured exception factory’s result to be thrown. packages/common/pipes/parse-uuid.pipe.ts:73-84 packages/common/pipes/parse-uuid.pipe.ts:87-92 packages/common/utils/shared.utils.ts:45

Was this page helpful?

Download as PDF
ParseUUIDPipeOptions — NestJS head-to-head