-
Notifications
You must be signed in to change notification settings - Fork 24
fix(nestJs): icon is optional #197
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
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| { | ||
| "NOT_EMPTY": "{property} should not to be empty", | ||
| "IS_ARRAY": "{property} should to be array", | ||
| "NOT_EMPTY_HUMAN": "{name} should not to be empty" | ||
| "NOT_EMPTY_HUMAN": "{name} should not to be empty", | ||
| "TYPE_ERROR": "{property} should to be {type}" | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| { | ||
| "NOT_EMPTY": "{property} 不能为空", | ||
| "IS_ARRAY": "{property} 应该为数组", | ||
| "NOT_EMPTY_HUMAN": "{name} 不能为空" | ||
| "NOT_EMPTY_HUMAN": "{name} 不能为空", | ||
| "TYPE_ERROR": "{property} 应该为 {type}" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import { IsNotEmpty } from 'class-validator'; | ||
| import { IsNotEmpty, IsString } from 'class-validator'; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
set -e
# Inspect ValidationPipe setup and whether missing properties are skipped globally.
rg -n -C3 'ValidationPipe|skipMissingProperties' --type tsRepository: opentiny/tiny-pro Length of output: 1019 🏁 Script executed: #!/bin/bash
# Read the actual create-menu.dto.ts file to see the current state
cat -n template/nestJs/src/menu/dto/create-menu.dto.ts | head -60Repository: opentiny/tiny-pro Length of output: 1992 🌐 Web query:
💡 Result:
So the default behavior is the same as To enable “skip missing properties” behavior: app.useGlobalPipes(new I18nValidationPipe({ skipMissingProperties: true }));
``` ([docs.nestjs.com](https://docs.nestjs.com/techniques/validation?utm_source=openai))
Citations:
- 1: https://app.unpkg.com/nestjs-i18n%4010.4.9/files/dist/pipes/i18n-validation.pipe.d.ts
- 2: https://docs.nestjs.com/techniques/validation?utm_source=openai
- 3: https://docs.nestjs.com/techniques/validation?utm_source=openai
---
</details>
**`icon` is not actually optional without `@IsOptional()`.**
`required: false` only affects OpenAPI docs. Since `I18nValidationPipe` uses default NestJS behavior (`skipMissingProperties: false`), `@IsString()` will reject requests that omit `icon`. The field is marked optional in Swagger but required in both validation and TypeScript. Fix the inconsistency by adding `@IsOptional()`, making the property optional in the DTO signature, and keeping the Swagger metadata consistent.
<details>
<summary>Suggested fix</summary>
```diff
-import { IsNotEmpty, IsString } from 'class-validator';
+import { IsNotEmpty, IsOptional, IsString } from 'class-validator';
…
`@ApiProperty`({
description: '菜单图标',
required: false,
})
+ `@IsOptional`()
`@IsString`({
- message: i18nValidationMessage<I18nTranslations>('validation.TYPE_ERROR', {type: 'string'}),
+ message: i18nValidationMessage<I18nTranslations>('validation.TYPE_ERROR', { type: 'string' }),
})
- icon: string;
+ icon?: string;Also applies to: 42-49 🤖 Prompt for AI Agents |
||
| import { i18nValidationMessage } from 'nestjs-i18n'; | ||
| import { I18nTranslations } from '../../.generate/i18n.generated'; | ||
| import { ApiProperty } from '@nestjs/swagger'; | ||
|
|
@@ -40,10 +40,11 @@ export class CreateMenuDto { | |
| }) | ||
| component: string; | ||
| @ApiProperty({ | ||
| description: '菜单图标' | ||
| description: '菜单图标', | ||
| required: false, | ||
| }) | ||
| @IsNotEmpty({ | ||
| message: i18nValidationMessage<I18nTranslations>('validation.NOT_EMPTY'), | ||
| @IsString({ | ||
| message: i18nValidationMessage<I18nTranslations>('validation.TYPE_ERROR', {type: 'string'}), | ||
| }) | ||
| icon: string; | ||
| @ApiProperty({ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix the English validation copy.
"should to be"reads incorrectly in API errors. Please change it to"should be {type}"or"must be {type}".🤖 Prompt for AI Agents