Passed
Pull Request — master (#216)
by
unknown
02:29
created

server/src/Infrastructure/Common/Validator/IsPhoneNumberOrEmpty.ts   A

Complexity

Total Complexity 1
Complexity/F 1

Size

Lines of Code 19
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 19
rs 10
c 0
b 0
f 0
wmc 1
mnd 0
bc 0
fnc 1
bpm 0
cpm 1
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A IsPhoneNumberOrEmpty.ts ➔ IsPhoneNumberOrEmpty 0 13 1
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