-
-
Notifications
You must be signed in to change notification settings - Fork 115
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
Showing
10 changed files
with
123 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.