-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add isUpperCase validator #193
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
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2861a43
feat: add @IsUppercase validator decorator
dami0806 646faac
feat: add @IsUppercase example to example app
dami0806 e7b15ea
docs: add @IsUppercase to documentation
dami0806 d5544ac
refactor: simplify isUppercase test by extracting shared rule to desc…
dami0806 2b730a4
refactor: replace toUpperCase comparison with regex in @IsUppercase f…
dami0806 085dfb0
fix: use toUpperCase for unicode-aware uppercase validation in @IsUpp…
dami0806 fbf8362
Merge remote-tracking branch 'origin/develop' into CARGO-365
dami0806 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
packages/express-cargo/tests/validator/isUppercase.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import { CargoFieldError, IsUppercase } from '../../src' | ||
| import { CargoClassMetadata } from '../../src/metadata' | ||
|
|
||
| describe('isUppercase decorator', () => { | ||
|
Contributor
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.
아래와 같은 테스트 케이스를 추가하여 악센트가 있는 문자나 다른 언어의 대소문자를 올바르게 처리하는지 확인할 수 있습니다. it('should handle unicode characters correctly', () => {
// The following will pass with the suggested implementation change
expect(isUppercaseRule?.validate('ÄÖÜ')).toBeNull(); // Uppercase with accents
expect(isUppercaseRule?.validate('ÄöÜ')).toBeInstanceOf(CargoFieldError); // Mixed case with accents
expect(isUppercaseRule?.validate('ПРИВЕТ')).toBeNull(); // Uppercase Cyrillic
expect(isUppercaseRule?.validate('Привет')).toBeInstanceOf(CargoFieldError); // Mixed case Cyrillic
expect(isUppercaseRule?.validate('你好')).toBeNull(); // Non-cased characters should pass
}); |
||
| class Sample { | ||
| @IsUppercase() | ||
| uppercaseValue!: string | ||
|
|
||
| noValidatorValue!: string | ||
| } | ||
|
|
||
| const classMeta = new CargoClassMetadata(Sample.prototype) | ||
| const meta = classMeta.getFieldMetadata('uppercaseValue') | ||
| const isUppercaseRule = meta.getValidators()?.find(v => v.type === 'isUppercase') | ||
|
|
||
| it('should have isUppercase validator', () => { | ||
| expect(isUppercaseRule).toBeDefined() | ||
| expect(isUppercaseRule?.message).toBe('uppercaseValue should be uppercase') | ||
| }) | ||
|
|
||
| it('should pass for all-uppercase strings', () => { | ||
| expect(isUppercaseRule?.validate('HELLO')).toBeNull() | ||
| expect(isUppercaseRule?.validate('HELLO WORLD')).toBeNull() | ||
| expect(isUppercaseRule?.validate('HELLO123')).toBeNull() | ||
| expect(isUppercaseRule?.validate('')).toBeNull() | ||
| }) | ||
|
|
||
| it('should fail for strings with lowercase characters', () => { | ||
| expect(isUppercaseRule?.validate('hello')).toBeInstanceOf(CargoFieldError) | ||
| expect(isUppercaseRule?.validate('Hello')).toBeInstanceOf(CargoFieldError) | ||
| expect(isUppercaseRule?.validate('helloWorld')).toBeInstanceOf(CargoFieldError) | ||
| }) | ||
|
|
||
| it('should fail for non-string values', () => { | ||
| expect(isUppercaseRule?.validate(null)).toBeInstanceOf(CargoFieldError) | ||
| expect(isUppercaseRule?.validate(undefined)).toBeInstanceOf(CargoFieldError) | ||
| expect(isUppercaseRule?.validate(123)).toBeInstanceOf(CargoFieldError) | ||
| }) | ||
|
|
||
| it('should not have isUppercase validator on undecorated field', () => { | ||
| const meta = classMeta.getFieldMetadata('noValidatorValue') | ||
| const isUppercaseRule = meta.getValidators()?.find(v => v.type === 'isUppercase') | ||
|
|
||
| expect(isUppercaseRule).toBeUndefined() | ||
| }) | ||
|
|
||
| it('should support custom error message', () => { | ||
| class CustomMessage { | ||
| @IsUppercase('custom error') | ||
| value!: string | ||
| } | ||
|
|
||
| const customMeta = new CargoClassMetadata(CustomMessage.prototype) | ||
| const meta = customMeta.getFieldMetadata('value') | ||
| const rule = meta.getValidators()?.find(v => v.type === 'isUppercase') | ||
|
|
||
| const error = rule?.validate('lower') | ||
| expect(error).toBeInstanceOf(CargoFieldError) | ||
| expect(error?.message).toBe('custom error') | ||
| }) | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
테스트 코드의 가독성과 유지보수성을 높이기 위해, 여러
it블록에서 반복되는isUppercaseRule을 가져오는 로직을describe블록 상단으로 옮기는 것을 제안합니다. 이렇게 하면 중복 코드를 줄이고 각 테스트의 의도를 더 명확하게 파악할 수 있습니다.예를 들어, 다음과 같이 리팩토링할 수 있습니다:
References