1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | var _Schema = require('./Schema');
|
8 |
|
9 | var _Schema2 = _interopRequireDefault(_Schema);
|
10 |
|
11 | var _Error = require('../Error');
|
12 |
|
13 | var _Error2 = _interopRequireDefault(_Error);
|
14 |
|
15 | var _Reference = require('../Reference');
|
16 |
|
17 | var _Reference2 = _interopRequireDefault(_Reference);
|
18 |
|
19 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
20 |
|
21 | class FunctionSchema extends _Schema2.default {
|
22 | constructor(base) {
|
23 | super(base);
|
24 |
|
25 | let raw = this._rules.descriptor.pop();
|
26 | this._rules.descriptor.push(this._typeDescriptor, this._lengthDescriptor, raw);
|
27 | raw = this._rules.validator.pop();
|
28 | this._rules.validator.push(this._typeValidator, this._lengthValidator, raw);
|
29 | }
|
30 |
|
31 | _typeDescriptor() {
|
32 | return 'Here a function is needed.\n';
|
33 | }
|
34 |
|
35 | _typeValidator(data) {
|
36 | if (typeof data.value !== 'function') {
|
37 | return Promise.reject(new _Error2.default(this, data, `Only a function is allowed, a \
|
38 | ${typeof data.value} is given here`));
|
39 | }
|
40 | return Promise.resolve();
|
41 | }
|
42 |
|
43 | min(limit) {
|
44 | const set = this._setting;
|
45 | if (limit) {
|
46 | if (!(limit instanceof _Reference2.default)) {
|
47 | if (set.max && !this._isReference('max') && limit > set.max) {
|
48 | throw new Error('Min length can´t be greater than max length');
|
49 | }
|
50 | if (limit < 0) throw new Error('Argument length for min() has to be positive');
|
51 | }
|
52 | set.min = limit;
|
53 | } else delete set.min;
|
54 | return this;
|
55 | }
|
56 |
|
57 | max(limit) {
|
58 | const set = this._setting;
|
59 | if (limit) {
|
60 | if (!(limit instanceof _Reference2.default)) {
|
61 | if (set.min && !this._isReference('min') && limit < set.min) {
|
62 | throw new Error('Max length can´t be less than min length');
|
63 | }
|
64 | if (limit < 0) throw new Error('Argument length for max() has to be positive');
|
65 | }
|
66 | set.max = limit;
|
67 | } else delete set.max;
|
68 | return this;
|
69 | }
|
70 |
|
71 | length(limit) {
|
72 | const set = this._setting;
|
73 | if (limit) {
|
74 | if (!(limit instanceof _Reference2.default)) {
|
75 | if (limit < 0) throw new Error('Length has to be positive');
|
76 | }
|
77 | set.min = limit;
|
78 | set.max = limit;
|
79 | } else {
|
80 | delete set.min;
|
81 | delete set.max;
|
82 | }
|
83 | return this;
|
84 | }
|
85 |
|
86 | _lengthDescriptor() {
|
87 | const set = this._setting;
|
88 | let msg = '';
|
89 | if (set.min instanceof _Reference2.default) {
|
90 | msg += `Minimum number of arguments depends on ${set.min.description}. `;
|
91 | }
|
92 | if (set.max instanceof _Reference2.default) {
|
93 | msg += `Maximum number of arguments depends on ${set.max.description}. `;
|
94 | }
|
95 | if (!this._isReference('min') && !this._isReference('max') && set.min && set.max) {
|
96 | msg = set.min === set.max ? `The function has to contain exactly ${set.min} arguments. ` : `The function can have between ${set.min} and ${set.max} arguments. `;
|
97 | } else if (!this._isReference('min') && set.min) {
|
98 | msg = `The function needs at least ${set.min} arguments. `;
|
99 | } else if (!this._isReference('max') && set.max) {
|
100 | msg = `The function allows up to ${set.min} arguments. `;
|
101 | }
|
102 | return msg.length ? `${msg.trim()}\n` : msg;
|
103 | }
|
104 |
|
105 | _lengthValidator(data) {
|
106 | const check = this._check;
|
107 | try {
|
108 | this._checkNumber('min');
|
109 | this._checkNumber('max');
|
110 | if (check.max && check.min && check.min > check.max) {
|
111 | throw new Error('Min arhuments can´t be greater than max arguments');
|
112 | }
|
113 | } catch (err) {
|
114 | return Promise.reject(new _Error2.default(this, data, err.message));
|
115 | }
|
116 |
|
117 | const num = data.value.length;
|
118 |
|
119 | if (check.min && num < check.min) {
|
120 | return Promise.reject(new _Error2.default(this, data, `The function has ${num} arguments. \
|
121 | This is too less, at least ${check.min} are needed.`));
|
122 | }
|
123 | if (check.max && num > check.max) {
|
124 | return Promise.reject(new _Error2.default(this, data, `The function has ${num} arguments. \
|
125 | This is too much, not more than ${check.max} are allowed.`));
|
126 | }
|
127 | return Promise.resolve();
|
128 | }
|
129 | }
|
130 |
|
131 | exports.default = FunctionSchema; |
\ | No newline at end of file |