-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschemas.js
59 lines (49 loc) · 1.42 KB
/
schemas.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
'use strict';
const jwtSchema = {
scopes: 'array'
, app: 'string'
, jti: 'string'
}
, app = {
key: 'string'
, createdDate: 'date'
, contact: 'object'
, payment: 'object'
, url: 'string'
, name: 'string'
, description: 'string'
}
, contact = {
name: 'string'
, email: 'string'
, phone: 'string'
}
, payment = {
token: 'string'
, expires: 'date'
}
, schemaCheck = function (objectIn, schemaIn) {
const schemaKeys = Object.keys(schemaIn)
, objectKeys = Object.keys(objectIn);
// must have the same number of keys
let check = schemaKeys.length === objectKeys.length;
// and all the keys must match in name and type
schemaKeys.forEach((key) => {
const isObject = typeof objectIn[key] === 'object';
if (schemaIn[key] === 'array') {
check = check && isObject && Array.isArray(objectIn[key]);
} else if (schemaIn[key] === 'date') {
check = check && isObject && !isNaN(new Date(objectIn[key]).getTime());
} else {
check = check && typeof objectIn[key] === schemaIn[key];
}
});
return check;
};
module.exports = {
jwt: jwtSchema
, account: app
, contact: contact
, payment: payment
, check: schemaCheck
};