| Total Complexity | 1 |
| Complexity/F | 1 |
| Lines of Code | 19 |
| Function Count | 1 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | import { buildMessage, isPhoneNumber, ValidateBy } from 'class-validator'; |
||
| 2 | |||
| 3 | // Don't validate if '' is received. |
||
| 4 | // See: https://github.com/typestack/class-validator/issues/232 |
||
| 5 | export function IsPhoneNumberOrEmpty(region?: string): any { |
||
| 6 | return ValidateBy({ |
||
| 7 | name: 'isPhoneNumberOrEmpty', |
||
| 8 | constraints: [region], |
||
| 9 | validator: { |
||
| 10 | validate: (value, args) => { |
||
| 11 | return value === '' || isPhoneNumber(value, args.constraints[0]); |
||
| 12 | }, |
||
| 13 | defaultMessage: buildMessage( |
||
| 14 | eachPrefix => eachPrefix + '$property must be a valid phone number' |
||
| 15 | ) |
||
| 16 | } |
||
| 17 | }); |
||
| 18 | } |
||
| 19 |