-
-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): Added validation for reason field (#190)
- Loading branch information
1 parent
92ceb75
commit b227d88
Showing
10 changed files
with
133 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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 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,29 @@ | ||
import { AlphanumericReasonValidationPipe } from './alphanumeric-reason-pipe'; | ||
import { BadRequestException } from '@nestjs/common'; | ||
|
||
describe('AlphanumericReasonValidationPipe', () => { | ||
let pipe: AlphanumericReasonValidationPipe; | ||
|
||
beforeEach(() => { | ||
pipe = new AlphanumericReasonValidationPipe(); | ||
}); | ||
|
||
it('should allow alphanumeric string', () => { | ||
const validInput = 'Test123'; | ||
expect(pipe.transform(validInput)).toBe(validInput); | ||
}); | ||
|
||
it('should not allow strings with only spaces', () => { | ||
expect(() => pipe.transform(' ')).toThrow(BadRequestException); | ||
}); | ||
|
||
it('should throw BadRequestException for non-alphanumeric string', () => { | ||
const invalidInput = 'Test123$%^'; | ||
try { | ||
pipe.transform(invalidInput); | ||
} catch (e) { | ||
expect(e).toBeInstanceOf(BadRequestException); | ||
expect(e.message).toBe('Reason must contain only alphanumeric characters and no leading or trailing spaces.'); | ||
} | ||
}); | ||
}); |
This file contains 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,12 @@ | ||
import { PipeTransform, Injectable, BadRequestException } from '@nestjs/common'; | ||
|
||
@Injectable() | ||
export class AlphanumericReasonValidationPipe implements PipeTransform { | ||
transform(value: string) { | ||
if (/^[a-zA-Z0-9]+(?: [a-zA-Z0-9]+)*$/.test(value)) { | ||
return value; | ||
} else { | ||
throw new BadRequestException('Reason must contain only alphanumeric characters and no leading or trailing spaces.'); | ||
} | ||
} | ||
} |
This file contains 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 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 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 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 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 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
Oops, something went wrong.