-
Notifications
You must be signed in to change notification settings - Fork 1
/
validator.js
56 lines (53 loc) · 1.45 KB
/
validator.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
'use strict';
const ajv = require('ajv');
const validator = new ajv();
validator.addFormat('integer', (value) => {
// All numbers are floating point in JS, so here we're really just testing that it is a whole number
return !isNaN(value) && (Math.floor(value) === value);
});
validator.addFormat('double', (value) => {
// The base validation already would have checked that `type` is `number`, and they're floating point
// This simply flags ajv to skip the warning that would otherwise occur
return !isNaN(value);
});
// Custom validation keywords so that we can use
// buffers and stream in validation schema
validator.addKeyword('buffer', {
compile: () => {
return (data) => {
return Buffer.isBuffer(data);
};
},
});
validator.addKeyword('readableStream', {
compile: () => {
return (data) => {
return isReadableStream(data);
};
},
});
validator.addKeyword('writeableStream', {
compile: () => {
return (data) => {
return isWriteableStream(data);
};
},
});
function isStream (obj) {
return obj &&
(typeof obj === 'object') &&
(typeof obj.pipe === 'function');
}
function isReadableStream (obj) {
return isStream(obj) &&
(obj.readable !== false) &&
(typeof obj._read === 'function') &&
(typeof obj._readableState === 'object');
}
function isWriteableStream (obj) {
return isStream(obj) &&
(obj.writable !== false) &&
(typeof obj._write === 'function') &&
(typeof obj._writableState === 'object');
}
module.exports = validator;