forked from typestack/class-validator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add IsIpRange validator (fix typestack#727)
- Loading branch information
Ballinette
committed
Oct 6, 2023
1 parent
ca2ff26
commit 5a5211d
Showing
4 changed files
with
101 additions
and
0 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
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,35 @@ | ||
import { ValidationOptions } from '../ValidationOptions'; | ||
import { buildMessage, ValidateBy } from '../common/ValidateBy'; | ||
import isIPRangeValidator from 'validator/lib/isIPRange'; | ||
|
||
export type IsIpRangeVersion = '4' | '6' | 4 | 6; | ||
|
||
export const IS_IP_RANGE = 'isIpRange'; | ||
|
||
/** | ||
* Checks if the string is an IP Range (version 4 or 6). | ||
* If given value is not a string, then it returns false. | ||
*/ | ||
export function isIPRange(value: unknown, version?: IsIpRangeVersion): boolean { | ||
/* eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion */ | ||
const versionStr = version ? (`${version}` as '4' | '6') : undefined; | ||
return typeof value === 'string' && isIPRangeValidator(value, versionStr); | ||
} | ||
|
||
/** | ||
* Checks if the string is an IP Range (version 4 or 6). | ||
* If given value is not a string, then it returns false. | ||
*/ | ||
export function IsIPRange(version?: IsIpRangeVersion, validationOptions?: ValidationOptions): PropertyDecorator { | ||
return ValidateBy( | ||
{ | ||
name: IS_IP_RANGE, | ||
constraints: [version], | ||
validator: { | ||
validate: (value, args): boolean => isIP(value, args?.constraints[0]), | ||
defaultMessage: buildMessage(eachPrefix => eachPrefix + '$property must be an ip range', validationOptions), | ||
}, | ||
}, | ||
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