UNPKG

2.79 kBJavaScriptView Raw
1const test = require('tap').test
2
3const Ajv = require('ajv')
4const AjvKeys = require('ajv-keywords/keywords/typeof')
5
6const type = {
7 func: { typeof: 'function' },
8 num : { type: 'number' },
9 str : { type: 'string' },
10 bool: { type: 'boolean' }
11}
12
13const app = {
14 type : 'object',
15 properties: {
16 debug : type.bool,
17 storage: {
18 type : 'object',
19 required : ['get', 'set', 'remove', 'clear'],
20 properties: {
21 get : type.func,
22 set : type.func,
23 remove: type.func,
24 clear : type.func
25 },
26 additionalProperties: true
27 }
28 },
29 additionalProperties: false
30}
31
32const api = {
33 type : 'object',
34 required : ['layer', 'api_id'],
35 properties: {
36 invokeWithLayer: type.num,
37 layer : type.num,
38 initConnection : type.num,
39 api_id : type.num,
40 device_model : type.str,
41 system_version : type.str,
42 app_version : type.str,
43 lang_code : type.str
44 },
45 additionalProperties: false
46}
47
48const dc = {
49 type : 'object',
50 required : ['id', 'host'],
51 properties: {
52 id : type.num,
53 host: type.str,
54 port: type.num
55 },
56 additionalProperties: false
57}
58
59const server = {
60 type : 'object',
61 properties: {
62 dev : type.bool,
63 webogram: type.bool,
64 dcList : {
65 type : 'array',
66 uniqueItems: true,
67 items : dc
68 }
69 },
70 additionalProperties: false
71}
72
73const schema = {
74 properties: {
75 app,
76 api,
77 server,
78 schema : { type: 'object' },
79 mtSchema: { type: 'object' }
80 },
81 additionalProperties: false
82}
83
84test('full module config validation', t => {
85 t.plan(1)
86 const ajv = new Ajv()
87 AjvKeys(ajv)
88
89 const validate = ajv.compile(schema)
90
91 const opts = {
92 app: {
93 debug : true,
94 storage: {
95 get() {},
96 set() {},
97 remove() {},
98 //eslint-disable-next-line
99 clear: () => {}
100 }
101 },
102 api: {
103 invokeWithLayer: 0xda9b0d0d,
104 layer : 57,
105 initConnection : 0x69796de9,
106 api_id : 49631,
107 device_model : 'Unknown UserAgent',
108 system_version : 'Unknown Platform',
109 app_version : '1.0.1',
110 lang_code : 'en'
111 },
112 server: {
113 dev : true,
114 webogram: true,
115 dcList : [
116 { id: 1, host: '149.154.175.10', port: 80 },
117 { id: 2, host: '149.154.167.40', port: 80 },
118 { id: 3, host: '149.154.175.117', port: 80 }
119 ]
120 }
121 }
122
123 const valid = validate(opts)
124
125 const print = res => res
126 ? res
127 : validate.errors
128
129 console.log(print(valid))
130 t.equal(valid, true, 'validation result is true')
131})
\No newline at end of file