-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbbf-validators.js
142 lines (136 loc) · 4.56 KB
/
bbf-validators.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
Backbone-Forms validators 1.0.1
Copyright (c) 2016 Tomasz Jakub Rup
https://github.com/tomi77/backbone-forms-validators
Released under the MIT license
*/
var indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
(function(root, factory) {
/* istanbul ignore next */
switch (false) {
case !(typeof define === 'function' && define.amd):
define(['underscore', 'backbone-forms'], factory);
break;
case typeof exports !== 'object':
factory(require('underscore'), require('backbone-forms'));
break;
default:
factory(root._, root.Backbone.Form);
}
})(this, function(_, Form) {
Form.validators.multiple = function(options) {
var base_validator;
options = _.extend({
separator: ';'
}, options);
base_validator = Form.validators[options.base_type](options);
return function(value) {
var out;
if (value.trim() === '') {
value = null;
}
if (value == null) {
return;
}
options.value = value;
value = value.split(options.separator).map(function(email) {
return email.trim();
});
out = _.compact(_.map(value, base_validator));
if (out.length > 0) {
return {
type: options.base_type,
message: options.message || out[0].message
};
}
};
};
Form.validators.errMessages.phone = 'Incorrect phone number';
Form.validators.phone = function(options) {
options = _.extend({
type: 'phone',
message: Form.validators.errMessages.phone,
regexp: /^(?:(?:\(?(?:00|\+)(?:[1-4]\d{2}|[1-9]\d?)\)?)?[\-\. \\\/]?)?(?:\(?\d+\)?[\-\. \\\/]?)+(?:[\-\. \\\/]?(?:\#|ext\.?|extension|x)[\-\. \\\/]?\d+)?$/
}, options);
return Form.validators.regexp(options);
};
Form.validators.errMessages.minlength = _.template('The minimum length is <%= minlength %> characters', null, Form.templateSettings);
Form.validators.minlength = function(options) {
options = _.extend({
type: 'minlength',
maxlength: 0,
message: Form.validators.errMessages.minlength
}, options);
options.regexp = new RegExp("^.{" + options.minlength + ",}$");
return Form.validators.regexp(options);
};
Form.validators.errMessages.maxlength = _.template('The maximum length is <%= maxlength %> characters', null, Form.templateSettings);
Form.validators.maxlength = function(options) {
options = _.extend({
type: 'maxlength',
maxlength: 0,
message: Form.validators.errMessages.maxlength
}, options);
options.regexp = new RegExp("^.{0," + options.maxlength + "}$");
return Form.validators.regexp(options);
};
Form.validators.errMessages.table_weights = 'Invalid control code';
Form.validators.table_weights = function(options) {
options = _.extend({
type: 'table_weights',
message: Form.validators.errMessages.table_weights
}, options);
if (options.lengths == null) {
throw new Error('Option "lengths" is required');
}
if (!_.isArray(options.lengths)) {
options.lengths = [options.lengths];
}
if (options.weights == null) {
throw new Error('Option "weights" is required');
}
if (!((options.modulo_values != null) && _.isArray(options.modulo_values))) {
throw new Error('Option "modulo_values" is required');
}
if (_.isArray(options.weights)) {
if (options.lengths.length === 1) {
options.weights = _.object([options.lengths[0]], [options.weights]);
} else {
throw new Error('Incorrect options weights and lengths');
}
}
if (!_.isArray(options.excepts)) {
options.excepts = [options.excepts];
}
return function(value) {
var control, err, ref, sum;
if (value.trim() === '') {
value = null;
}
if (value == null) {
return;
}
options.value = value;
err = {
type: options.type,
message: options.message
};
value = value.replace(/[\s-]/g, '');
if (indexOf.call(options.excepts, value) >= 0) {
return err;
}
value = value.split('');
if (ref = value.length, indexOf.call(options.lengths, ref) < 0) {
return err;
}
control = value.pop();
sum = _.reduce(_.zip(value, options.weights[value.length + 1]), function(memo, val) {
return memo + val[1] * parseInt(val[0], 10);
}, 0);
value = '' + options.modulo_values[sum % options.modulo_values.length];
if (value !== control) {
return err;
}
};
};
});