-
Notifications
You must be signed in to change notification settings - Fork 0
/
NegativeGuard.ts
35 lines (28 loc) · 935 Bytes
/
NegativeGuard.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { Guarder } from '../Guarder'
import { Guard } from '../types/Guard'
import { NumberGuard } from './NumberGuard'
import { Instantiable } from '../types/Instantiable'
import { ArgumentError } from '../errors/ArgumentError'
/**
* Negative Guard validates a property is not a negative number
*/
class NegativeGuard implements Guard {
/**
* @inheritDoc
*/
public guard<T = unknown>(property: T, errorMessage?: string, error?: Instantiable<Error>): T {
Guarder.guard(NumberGuard, property, errorMessage, error)
const int = parseInt(Guarder.empty(property).toString())
if (int < 0) {
this.throwError(errorMessage ?? 'Property not allowed to be negative number', error)
}
return property
}
private throwError(message: string, error?: Instantiable<Error>): never {
if (error) {
throw new error(message)
}
throw new ArgumentError(message)
}
}
export { NegativeGuard }