-
Notifications
You must be signed in to change notification settings - Fork 223
/
validation.js
287 lines (252 loc) · 9.32 KB
/
validation.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
var _my;
var is_empty;
var is_digits;
var to_number;
var require_match;
var require_object_id;
var require_object_type;
var get_instance;
var require_relative_type;
var get_relative_instance;
var require_protocol_type;
var get_protocol_instance;
var get_protocol_type;
var require_implementation_type;
var get_implementation_instance;
var Long = require('bytebuffer').Long;
// var BigInteger = require('bigi');
var chain_types = require('./ChainTypes');
var MAX_SAFE_INT = 9007199254740991;
var MIN_SAFE_INT =-9007199254740991;
/**
Most validations are skipped and the value returned unchanged when an empty string, null, or undefined is encountered (except "required").
Validations support a string format for dealing with large numbers.
*/
module.exports = _my = {
is_empty: is_empty=function(value){
return value === null || value === undefined;
},
required(value, field_name=""){
if (is_empty(value) ){
throw new Error(`value required ${field_name} ${value}`);
}
return value;
},
require_long(value, field_name=""){
if (!Long.isLong(value)) {
throw new Error(`Long value required ${field_name} ${value}`);
}
return value;
},
string(value){
if (is_empty(value) ){ return value; }
if (typeof value !== "string") {
throw new Error(`string required: ${value}`);
}
return value;
},
number(value){
if (is_empty(value) ){ return value; }
if (typeof value !== "number") {
throw new Error(`number required: ${value}`);
}
return value;
},
whole_number(value, field_name=""){
if (is_empty(value) ){ return value; }
if (/\./.test(value) ){
throw new Error(`whole number required ${field_name} ${value}`);
}
return value;
},
unsigned(value, field_name=""){
if (is_empty(value) ){ return value; }
if (/-/.test(value) ){
throw new Error(`unsigned required ${field_name} ${value}`);
}
return value;
},
is_digits: is_digits=function(value){
if (typeof value === "numeric") { return true; }
return /^[0-9]+$/.test(value);
},
to_number: to_number=function(value, field_name=""){
if (is_empty(value) ){ return value; }
_my.no_overflow53(value, field_name);
var int_value = (() => {
if (typeof value === "number") {
return value;
} else {
return parseInt(value);
}
})();
return int_value;
},
to_long(value, field_name=""){
if (is_empty(value) ){ return value; }
if (Long.isLong(value) ){ return value; }
_my.no_overflow64(value, field_name);
if (typeof value === "number") {
value = ""+value;
}
return Long.fromString(value);
},
to_string(value, field_name=""){
if (is_empty(value) ){ return value; }
if (typeof value === "string") { return value; }
if (typeof value === "number") {
_my.no_overflow53(value, field_name);
return ""+value;
}
if (Long.isLong(value) ){
return value.toString();
}
throw `unsupported type ${field_name}: (${typeof value}) ${value}`;
},
require_test(regex, value, field_name=""){
if (is_empty(value) ){ return value; }
if (!regex.test(value)) {
throw new Error(`unmatched ${regex} ${field_name} ${value}`);
}
return value;
},
require_match: require_match=function(regex, value, field_name=""){
if (is_empty(value) ){ return value; }
var match = value.match(regex);
if (match === null) {
throw new Error(`unmatched ${regex} ${field_name} ${value}`);
}
return match;
},
// require_object_id: require_object_id=function(value, field_name){
// return require_match(
// /^([0-9]+)\.([0-9]+)\.([0-9]+)$/,
// value,
// field_name
// );
// },
// Does not support over 53 bits
require_range(min,max,value, field_name=""){
if (is_empty(value) ){ return value; }
var number = to_number(value);
if (value < min || value > max) {
throw new Error(`out of range ${value} ${field_name} ${value}`);
}
return value;
},
require_object_type: require_object_type=function(
reserved_spaces = 1, type, value,
field_name=""
){
if (is_empty(value) ){ return value; }
var object_type = chain_types.object_type[type];
if (!object_type) {
throw new Error(`Unknown object type: ${type}, ${field_name}, ${value}`);
}
var re = new RegExp(`${reserved_spaces}\.${object_type}\.[0-9]+$`);
if (!re.test(value)) {
throw new Error(`Expecting ${type} in format `+ `${reserved_spaces}.${object_type}.[0-9]+ `+ `instead of ${value} ${field_name} ${value}`);
}
return value;
},
get_instance: get_instance=function(reserve_spaces, type, value, field_name){
if (is_empty(value) ){ return value; }
require_object_type(reserve_spaces, type, value, field_name);
return to_number(value.split('.')[2]);
},
require_relative_type: require_relative_type=function(type, value, field_name){
require_object_type(0, type, value, field_name);
return value;
},
get_relative_instance: get_relative_instance=function(type, value, field_name){
if (is_empty(value) ){ return value; }
require_object_type(0, type, value, field_name);
return to_number(value.split('.')[2]);
},
require_protocol_type: require_protocol_type=function(type, value, field_name){
require_object_type(1, type, value, field_name);
return value;
},
get_protocol_instance: get_protocol_instance=function(type, value, field_name){
if (is_empty(value) ){ return value; }
require_object_type(1, type, value, field_name);
return to_number(value.split('.')[2]);
},
get_protocol_type: get_protocol_type=function(value, field_name){
if (is_empty(value) ){ return value; }
require_object_id(value, field_name);
var values = value.split('.');
return to_number(values[1]);
},
get_protocol_type_name(value, field_name){
if (is_empty(value) ){ return value; }
var type_id = get_protocol_type(value, field_name);
return (Object.keys(chain_types.object_type))[type_id];
},
require_implementation_type: require_implementation_type=function(type, value, field_name){
require_object_type(2, type, value, field_name);
return value;
},
get_implementation_instance: get_implementation_instance=function(type, value, field_name){
if (is_empty(value) ){ return value; }
require_object_type(2, type, value, field_name);
return to_number(value.split('.')[2]);
},
// signed / unsigned decimal
no_overflow53(value, field_name=""){
if (typeof value === "number") {
if (value > MAX_SAFE_INT || value < MIN_SAFE_INT) {
throw new Error(`overflow ${field_name} ${value}`);
}
return;
}
if (typeof value === "string") {
var int = parseInt(value);
if (value > MAX_SAFE_INT || value < MIN_SAFE_INT) {
throw new Error(`overflow ${field_name} ${value}`);
}
return;
}
if (Long.isLong(value) ){
// typeof value.toInt() is 'number'
_my.no_overflow53(value.toInt(), field_name);
return;
}
throw `unsupported type ${field_name}: (${typeof value}) ${value}`;
},
// signed / unsigned whole numbers only
no_overflow64(value, field_name=""){
// https://github.com/dcodeIO/Long.js/issues/20
if (Long.isLong(value) ){ return; }
// BigInteger#isBigInteger https://github.com/cryptocoinjs/bigi/issues/20
if (value.t !== undefined && value.s !== undefined) {
_my.no_overflow64(value.toString(), field_name);
return;
}
if (typeof value === "string") {
// remove leading zeros, will cause a false positive
value = value.replace(/^0+/,'');
// remove trailing zeros
while (/0$/.test(value) ){
value = value.substring(0, value.length - 1);
}
if (/\.$/.test(value) ){
// remove trailing dot
value = value.substring(0, value.length - 1);
}
if (value === "") { value = "0"; }
var long_string = Long.fromString(value).toString();
if (long_string !== value.trim()) {
throw new Error(`overflow ${field_name} ${value}`);
}
return;
}
if (typeof value === "number") {
if (value > MAX_SAFE_INT || value < MIN_SAFE_INT) {
throw new Error(`overflow ${field_name} ${value}`);
}
return;
}
throw `unsupported type ${field_name}: (${typeof value}) ${value}`;
}
};