Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: The @IsByteLength() decorator does not validate correctly for multi-byte characters such as Japanese and Chinese. #2530

Open
Suyama-Daichi opened this issue Sep 9, 2024 · 0 comments
Labels
status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature.

Comments

@Suyama-Daichi
Copy link

Description

I am developing a service that targets Japanese.
I used the IsByteLength() decorator to limit the message length of the request body to 2048 bytes.
However, it is not being validated as expected.

Minimal code-snippet showcasing the problem

I created a reproducible repository.
https://github.com/Suyama-Daichi/class-validator-2byte

The following code is an excerpt.

import { ApiProperty } from '@nestjs/swagger';
import { IsByteLength, IsNotEmpty, IsString } from 'class-validator';

export class RequestDTO {
  @ApiProperty({
    example: 'テスト',
    description: 'message (Japanese)',
    type: String,
  })
  @IsNotEmpty()
  @IsString()
  @IsByteLength(0, 6)
  message: string;
}
import { Body, Controller, Post } from '@nestjs/common';
import { RequestDTO } from './RequestDTO';
import { ApiOperation } from '@nestjs/swagger';

@Controller()
export class AppController {
  constructor() {}

  @ApiOperation({ summary: 'Post name' })
  @Post()
  postName(@Body() body: RequestDTO) {
    return body;
  }
}

Expected behavior

If you send the following request:

curl -X 'POST' \
  'http://localhost:3000/' \
  -H 'accept: */*' \
  -H 'Content-Type: application/json' \
  -d '{
  "message": "テスト"
}'

The validation should pass and you should get the following response:

{
  "message": "テスト"
}

Actual behavior

If you send the following request:

curl -X 'POST' \
  'http://localhost:3000/' \
  -H 'accept: */*' \
  -H 'Content-Type: application/json' \
  -d '{
  "message": "テスト"
}'

A validation error will occur.
テスト should be 6 bytes, so it should pass validation.

{
  "message": [
    "message's byte length must fall into (0, 6) range"
  ],
  "error": "Bad Request",
  "statusCode": 400
}

Related issue

#775

@Suyama-Daichi Suyama-Daichi added status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature. labels Sep 9, 2024
@Suyama-Daichi Suyama-Daichi changed the title fix: @IsByteLength()デコレータを使用した場合、日本語や中国語のようなマルチバイト文字では正しく検証されません fix: @IsByteLength()デコレータを使用した場合、日本語や中国語のようなマルチバイト文字では正しく検証されません Sep 9, 2024
@Suyama-Daichi Suyama-Daichi changed the title fix: @IsByteLength()デコレータを使用した場合、日本語や中国語のようなマルチバイト文字では正しく検証されません fix: The @IsByteLength() decorator does not validate correctly for multi-byte characters such as Japanese and Chinese. Sep 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature.
Development

No branches or pull requests

1 participant