-
Notifications
You must be signed in to change notification settings - Fork 40
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
[FEAT] Improve error message of isEnum validator #830
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
941b4da
refactor: improve error message of isenum validator
Bendakh ec2142b
refactor: refactor the new is enum function
Bendakh eec2950
refactor: create a new package for the class validators
Bendakh 82b4c06
feat: re export the nestjs class validators
Bendakh 59b4482
chore: change new folder name
Bendakh 722ad71
chore: config cleanup
Bendakh 5909a1d
chore: update readme
Bendakh f961358
chore: add readme file to the class validator package
Bendakh ea8e090
chore: update the class validators readme
Bendakh d084e4b
chore: add jsdoc to the isenum validator
Bendakh 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 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.
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,21 @@ | ||
# Nestjs class validators | ||
|
||
A set of class validators based on the class-validator package (https://www.npmjs.com/package/@nestjs/class-validator/v/0.13.1). | ||
|
||
## Installation | ||
|
||
```bash | ||
npm install --save @algoan/nestjs-class-validators | ||
``` | ||
|
||
## IsEnum | ||
|
||
A class validator that validates the enum type. | ||
|
||
### Usage | ||
|
||
```ts | ||
@IsEnum(UserType) | ||
public userType: UserType; | ||
``` | ||
|
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,14 @@ | ||
{ | ||
"name": "@algoan/nestjs-class-validators", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"class-validator": "^0.14.0" | ||
} | ||
} |
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,2 @@ | ||
export { IsEnum } from './is-enum.decorator'; | ||
export * from 'class-validator'; |
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,13 @@ | ||
import { IsEnum as OriginalIsEnum, ValidationOptions } from 'class-validator'; | ||
|
||
/** | ||
* Checks if a given value is the member of the provided enum. | ||
* | ||
* This an override for the class-validator IsEnum validator. | ||
* The error message is enhanced with the invalid value | ||
*/ | ||
export const IsEnum = (entity: object, validationOptions?: ValidationOptions): PropertyDecorator => | ||
OriginalIsEnum(entity, { | ||
message: '$property has the value $value but must be one of the following values: $constraint2', | ||
...validationOptions, | ||
}); |
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,79 @@ | ||
import { IsEnum } from '../src/is-enum.decorator'; | ||
import { Validator, ValidatorOptions } from 'class-validator'; | ||
|
||
function validateValues( | ||
object: { testProperty: any }, | ||
values: any[], | ||
validatorOptions?: ValidatorOptions, | ||
): Promise<any> { | ||
const validator = new Validator(); | ||
const promises = values.map((value) => { | ||
object.testProperty = value; | ||
return validator.validate(object, validatorOptions).then((errors) => { | ||
expect(errors.length).toEqual(0); | ||
if (errors.length > 0) { | ||
console.log(`Unexpected errors: ${JSON.stringify(errors)}`); | ||
throw new Error('Unexpected validation errors'); | ||
} | ||
}); | ||
}); | ||
|
||
return Promise.all(promises); | ||
} | ||
|
||
function checkReturnedError( | ||
object: { testProperty: any }, | ||
values: any[], | ||
validationType: string, | ||
messages: string[], | ||
validatorOptions?: ValidatorOptions, | ||
): Promise<any> { | ||
let messagesIncrementor: number = 0; | ||
const validator = new Validator(); | ||
const promises = values.map((value) => { | ||
object.testProperty = value; | ||
return validator.validate(object, validatorOptions).then((errors) => { | ||
expect(errors.length).toEqual(1); | ||
expect(errors[0].target).toEqual(object); | ||
expect(errors[0].property).toEqual('testProperty'); | ||
expect(errors[0].constraints).toEqual({ [validationType]: messages[messagesIncrementor] }); | ||
expect(errors[0].value).toEqual(value); | ||
messagesIncrementor++; | ||
}); | ||
}); | ||
|
||
return Promise.all(promises); | ||
} | ||
|
||
describe('Tests related to the custom IsEnum Decorator', () => { | ||
enum CustomEnum { | ||
FIRST_ITEM = 'first-item', | ||
SECOND_ITEM = 'second-item', | ||
} | ||
|
||
class TestClass { | ||
@IsEnum(CustomEnum) | ||
testProperty: CustomEnum = CustomEnum.FIRST_ITEM; | ||
} | ||
|
||
it('should validate the correct values', () => { | ||
return validateValues(new TestClass(), [ | ||
CustomEnum.FIRST_ITEM, | ||
CustomEnum.SECOND_ITEM, | ||
'first-item', | ||
'second-item', | ||
]); | ||
}); | ||
|
||
it('should not validate invalid values and return the correct errors', () => { | ||
const validationType = 'isEnum'; | ||
const firstMessage = | ||
'testProperty has the value false-value-1 but must be one of the following values: first-item, second-item'; | ||
const secondMessage = | ||
'testProperty has the value false-value-2 but must be one of the following values: first-item, second-item'; | ||
return checkReturnedError(new TestClass(), ['false-value-1', 'false-value-2'], validationType, [ | ||
firstMessage, | ||
secondMessage, | ||
]); | ||
}); | ||
}); |
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.
Since this function is part of the public API, you should probably add JSDoc.