-
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 4 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.
현재 구현은
value.toUpperCase()를 호출하여 새로운 문자열을 할당합니다. 이 방식은 매우 긴 문자열을 검증할 때 미미한 성능 저하를 유발할 수 있습니다.Alpha,Alphanumeric와 같은 다른 검증기와의 일관성을 위해, 그리고 잠재적인 성능 향상을 위해 정규식을 사용하는 것을 제안합니다.References