-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonSchema.js
49 lines (44 loc) · 1.1 KB
/
jsonSchema.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
const {Validator} = require('jsonschema');
const string = {"type": "string"};
const boolean = {"type": "boolean"};
const integer = {"type": "number", "multipleOf": 1};
const timestamp = {...integer, "minimum": 1};
const SshKey = {
"id": "/SshKey",
"type": "object",
"properties": {
name: string,
publicKey: string
}
};
const UserLog = {
"id": "/UserLog",
"type": "object",
"properties": {
at: timestamp,
type: string
}
};
const User = {
"id": "/User",
"type": "object",
"properties": {
username: string,
name: string,
emails: {"type": "array", "items": string},
sshKeys: {"type": "array", "items": SshKey},
systemUser: boolean,
logs: {"type": "array", "items": UserLog},
},
"required": []
};
let _userValidator;
const validateUser = (json) => {
if (!_userValidator) {
const v = new Validator();
// v.addSchema(sshKey, sshKey.id);
_userValidator = json => v.validate(json, User).errors;
}
return _userValidator(json);
};
module.exports = {validateUser};