Passed
Pull Request — master (#107)
by Mathieu
01:27
created

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

Complexity

Total Complexity 1
Complexity/F 1

Size

Lines of Code 29
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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

1 Function

Rating   Name   Duplication   Size   Complexity  
A DateGreaterOrEqualThan.ts ➔ validate 0 5 1
1
import {
2
  registerDecorator,
3
  ValidationOptions,
4
  ValidationArguments
5
} from 'class-validator';
6
7
export const DateGreaterOrEqualThan = (property: string) => {
8
  return (object: any, propertyName: string) => {
9
    const options: ValidationOptions = {
10
      message: `${propertyName} should be greater or equal than ${property}`
11
    };
12
    registerDecorator({
13
      name: 'dateGreaterOrEqualThan',
14
      target: object.constructor,
15
      propertyName,
16
      constraints: [property],
17
      options,
18
      validator: {
19
        validate(value: any, args: ValidationArguments) {
20
          const [relatedPropertyName] = args.constraints;
21
          const relatedValue = (args.object as any)[relatedPropertyName];
22
23
          return new Date(value) >= new Date(relatedValue);
24
        }
25
      }
26
    });
27
  };
28
};
29