-
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
Changes from 5 commits
2861a43
646faac
e7b15ea
d5544ac
2b730a4
085dfb0
fbf8362
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 |
|---|---|---|
| @@ -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. 테스트 코드의 가독성과 유지보수성을 높이기 위해, 여러 예를 들어, 다음과 같이 리팩토링할 수 있습니다: describe('isUppercase decorator', () => {
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();
// ...
});
// ... other tests using isUppercaseRule
});References
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') | ||
| }) | ||
| }) | ||
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.
@IsUppercase유효성 검사기의 현재 구현은 ASCII 소문자(a-z)만 확인하므로, 다른 언어의 소문자나 악센트가 있는 문자를 올바르게 처리하지 못합니다. 예를 들어, 'ü'나 'привет'과 같은 문자열이 유효성 검사를 통과하게 됩니다.String.prototype.toUpperCase()를 사용하여 모든 유니코드 문자를 올바르게 처리하는 것이 더 안정적인 방법입니다.