UNPKG

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