-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
min.js
44 lines (35 loc) · 971 Bytes
/
min.js
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
36
37
38
39
40
41
42
43
44
import { isNumber, isString, isArray } from 'myrmidon';
import {
WRONG_FORMAT,
TOO_LOW,
TOO_SHORT
} from '../errors';
import Base from './Base';
/**
* Checks value to be more than threshold.
* @param {integer} threshold min possible value
* @error TOO_LOW
* @error TOO_SHORT
* @error WRONG_FORMAT
* @returns {any} valid string, number or array
* @rule
*/
export default class MinRule extends Base {
static schema = 'min';
validate(input) {
const threshold = this.params;
if (isNumber(input)) {
if (input < threshold) throw new TOO_LOW(threshold);
return input;
}
if (isString(input)) {
if (input.length < threshold) throw new TOO_SHORT(threshold);
return input;
}
if (isArray(input)) {
if (input.length < threshold) throw new TOO_SHORT(threshold);
return input;
}
throw new WRONG_FORMAT();
}
}