Error message

Learn how to configure validation error messages with automatic i18n translation, manual field-level messages, or hardcoded custom messages in your DTOs using the `@tradinos/cms-backend-entity`

Validation Messages

A comprehensive guide for handling validation error messages in DTOs with i18n support using the @tradinos/cms-backend-entity package.

Overview

This guide covers different approaches to configure validation error messages in your NestJS application. You can configure validation messages at the global level (metadata) or at the field level (DTOs), with support for automatic translation, manual i18n, or hardcoded messages.


Configuration

To enable automatic translation for all validation messages across your application, configure the metadata option in EntityModule.register:

import { i18nValidatorBasedValidationErrorMessage } from '@tradinos/cms-backend-entity';

EntityModule.register({
  metadata: {
    authorization: true,
    validationErrorMessage: i18nValidatorBasedValidationErrorMessage(),
  },
});

How it works

  • Source: Imported from @tradinos/cms-backend-entity

  • Translation: Uses the i18n configuration defined in app.module.ts

  • Language Support: Automatically translates based on the request language (Arabic/English)

  • Translation Files: Works with src/i18n/en/validation.json and src/i18n/ar/validation.json

Benefits

  • โœ… Automatically translates all validation messages

  • โœ… No need to specify message in each decorator

  • โœ… Consistent translation across all entities

  • โœ… Easy to maintain and update


Field-Level Validation in DTOs

You have three options for field-level validation messages:

Option A: i18n Translated Messages

Use i18nValidationMessage for field-specific translated messages:

Example from codebase:

How it works:

  • Import from nestjs-i18n

  • Looks up translations in src/i18n/en/validation.json and src/i18n/ar/validation.json

  • Translates based on current request language


Option B: Hardcoded Custom Messages

Use @ValidationErrorMessage decorator for field-specific hardcoded messages:

How it works:

  • Import from @tradinos/cms-backend-entity

  • Hardcoded messages (no translation)

  • Specify message for each validation rule

Use cases:

  • โœ… Custom hardcoded message

  • โŒ No translation (same message in all languages)

  • โœ… Useful for static messages that don't need translation


Option C: Default class-validator Messages

Use class-validator decorators without specifying a message:

Example from codebase:

How it works:

  • Uses default class-validator messages (English only)

  • Examples: "must be a string", "should not be empty"

  • Inherits from metadata if i18nValidatorBasedValidationErrorMessage() is configured


Summary Table

Scenario
In metadata
In DTO
Translation

Auto-translated

i18nValidatorBasedValidationErrorMessage()

No message (inherits from metadata)

โœ… Yes

Manual i18n

i18nValidatorBasedValidationErrorMessage()

i18nValidationMessage('validation.XXX')

โœ… Yes

Hardcoded custom

i18nValidatorBasedValidationErrorMessage()

@ValidationErrorMessage({ "max-length": "..." })

โŒ No

Default

No validationErrorMessage

No message

โŒ No


Complete Example

Configuration in app.module.ts

Field-level examples

Using i18n in DTO:

Inheriting from metadata:

Result:

  • โœ… Messages automatically translated from metadata

  • โœ… Some fields use i18nValidationMessage directly

  • โœ… Some fields without message (inherit from metadata)

  • โœ… Can use @ValidationErrorMessage for hardcoded custom messages


i18n Translation Files

English (src/i18n/en/validation.json)

Arabic (src/i18n/ar/validation.json)


Best Practices

  1. Use global i18n (i18nValidatorBasedValidationErrorMessage) for consistent translation across all entities

  2. Use field-level i18n (i18nValidationMessage) when you need specific translations for certain fields

  3. Use hardcoded messages (@ValidationErrorMessage) only for static messages that don't need translation

  4. Avoid default messages when building multilingual applications


  • Entity Options - Learn about other EntityModule configuration options

  • Localization - Understand how localization works in the Entity module

  • Error Handling - See how validation errors are handled

Last updated