forked from apigee-127/swagger-test-templates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom-formats.js
41 lines (33 loc) · 1.2 KB
/
custom-formats.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
module.exports = function(zSchema) {
// Placeholder file for all custom-formats in known to swagger.json
// as found on
// https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#dataTypeFormat
var decimalPattern = /^\d{0,8}.?\d{0,4}[0]+$/;
/** Validates floating point as decimal / money (i.e: 12345678.123400..) */
zSchema.registerFormat('double', function(val) {
return !decimalPattern.test(val.toString());
});
/** Validates value is a 32bit integer */
zSchema.registerFormat('int32', function(val) {
// the 32bit shift (>>) truncates any bits beyond max of 32
return Number.isInteger(val) && ((val >> 0) === val);
});
zSchema.registerFormat('int64', function(val) {
return Number.isInteger(val);
});
zSchema.registerFormat('float', function(val) {
// should parse
return Number.isInteger(val);
});
zSchema.registerFormat('date', function(val) {
// should parse a a date
return !isNaN(Date.parse(val));
});
zSchema.registerFormat('dateTime', function(val) {
return !isNaN(Date.parse(val));
});
zSchema.registerFormat('password', function(val) {
// should parse as a string
return typeof val === 'string';
});
};