plugin-workflow-portainer 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +176 -0
- package/README.md +31 -3
- package/client-v2.js +1 -0
- package/client.js +1 -0
- package/dist/client/127.5012da93825a6e5c.js +10 -0
- package/dist/client/57.ace9538136438718.js +10 -0
- package/dist/client/65.ec4a55173511c06d.js +10 -0
- package/dist/client/660.546c702d1ddf7eff.js +10 -0
- package/dist/client/index.js +10 -0
- package/dist/client-v2/127.0ec4dce7b13750e9.js +10 -0
- package/dist/client-v2/57.e7cfc50fb651ac14.js +10 -0
- package/dist/client-v2/65.9c745ea32ac96e98.js +10 -0
- package/dist/client-v2/660.29ae3c3b4040039d.js +10 -0
- package/dist/client-v2/index.js +10 -0
- package/dist/externalVersion.js +20 -0
- package/dist/index.js +48 -0
- package/dist/locale/en-US.json +151 -0
- package/dist/locale/vi-VN.json +151 -0
- package/dist/locale/zh-CN.json +151 -0
- package/dist/node_modules/joi/dist/joi-browser.min.js +1 -0
- package/dist/node_modules/joi/lib/annotate.js +175 -0
- package/dist/node_modules/joi/lib/base.js +1069 -0
- package/dist/node_modules/joi/lib/cache.js +143 -0
- package/dist/node_modules/joi/lib/common.js +216 -0
- package/dist/node_modules/joi/lib/compile.js +283 -0
- package/dist/node_modules/joi/lib/errors.js +271 -0
- package/dist/node_modules/joi/lib/extend.js +312 -0
- package/dist/node_modules/joi/lib/index.d.ts +2365 -0
- package/dist/node_modules/joi/lib/index.js +1 -0
- package/dist/node_modules/joi/lib/manifest.js +476 -0
- package/dist/node_modules/joi/lib/messages.js +207 -0
- package/dist/node_modules/joi/lib/modify.js +267 -0
- package/dist/node_modules/joi/lib/ref.js +414 -0
- package/dist/node_modules/joi/lib/schemas.js +302 -0
- package/dist/node_modules/joi/lib/state.js +166 -0
- package/dist/node_modules/joi/lib/template.js +463 -0
- package/dist/node_modules/joi/lib/trace.js +346 -0
- package/dist/node_modules/joi/lib/types/alternatives.js +364 -0
- package/dist/node_modules/joi/lib/types/any.js +174 -0
- package/dist/node_modules/joi/lib/types/array.js +809 -0
- package/dist/node_modules/joi/lib/types/binary.js +100 -0
- package/dist/node_modules/joi/lib/types/boolean.js +150 -0
- package/dist/node_modules/joi/lib/types/date.js +233 -0
- package/dist/node_modules/joi/lib/types/function.js +93 -0
- package/dist/node_modules/joi/lib/types/keys.js +1084 -0
- package/dist/node_modules/joi/lib/types/link.js +184 -0
- package/dist/node_modules/joi/lib/types/number.js +363 -0
- package/dist/node_modules/joi/lib/types/object.js +22 -0
- package/dist/node_modules/joi/lib/types/string.js +850 -0
- package/dist/node_modules/joi/lib/types/symbol.js +102 -0
- package/dist/node_modules/joi/lib/validator.js +750 -0
- package/dist/node_modules/joi/lib/values.js +263 -0
- package/dist/node_modules/joi/node_modules/@hapi/topo/lib/index.d.ts +60 -0
- package/dist/node_modules/joi/node_modules/@hapi/topo/lib/index.js +225 -0
- package/dist/node_modules/joi/node_modules/@hapi/topo/package.json +30 -0
- package/dist/node_modules/joi/package.json +1 -0
- package/dist/server/actions/portainer.js +192 -0
- package/dist/server/collections/portainer-instances.js +54 -0
- package/dist/server/index.js +54 -0
- package/dist/server/instructions/PortainerStackControlInstruction.js +156 -0
- package/dist/server/instructions/PortainerStackRedeployInstruction.js +254 -0
- package/dist/server/instructions/helpers.js +211 -0
- package/dist/server/plugin.js +170 -0
- package/dist/server/services/PortainerClient.js +497 -0
- package/dist/shared/collection.js +57 -0
- package/dist/shared/constants.js +66 -0
- package/dist/shared/env.js +221 -0
- package/dist/shared/types.js +24 -0
- package/package.json +50 -4
- package/server.js +1 -0
- package/src/client/index.tsx +50 -0
- package/src/client/instructions.tsx +76 -0
- package/src/client-v2/__tests__/workflow-registration.test.tsx +132 -0
- package/src/client-v2/components/ControlFieldset.tsx +46 -0
- package/src/client-v2/components/PortainerEnvEditor.tsx +223 -0
- package/src/client-v2/components/PortainerSettingsPage.tsx +671 -0
- package/src/client-v2/components/PortainerTargetFields.tsx +317 -0
- package/src/client-v2/components/RedeployFieldset.tsx +123 -0
- package/src/client-v2/index.tsx +50 -0
- package/src/client-v2/instructions.tsx +162 -0
- package/src/client-v2/locale.ts +24 -0
- package/src/index.ts +11 -0
- package/src/locale/en-US.json +151 -0
- package/src/locale/vi-VN.json +151 -0
- package/src/locale/zh-CN.json +151 -0
- package/src/server/__tests__/portainer-workflow.test.ts +455 -0
- package/src/server/actions/portainer.ts +196 -0
- package/src/server/collections/portainer-instances.ts +34 -0
- package/src/server/index.ts +13 -0
- package/src/server/instructions/PortainerStackControlInstruction.ts +139 -0
- package/src/server/instructions/PortainerStackRedeployInstruction.ts +255 -0
- package/src/server/instructions/helpers.ts +237 -0
- package/src/server/plugin.ts +167 -0
- package/src/server/services/PortainerClient.ts +589 -0
- package/src/shared/collection.ts +32 -0
- package/src/shared/constants.ts +25 -0
- package/src/shared/env.ts +228 -0
- package/src/shared/types.ts +105 -0
|
@@ -0,0 +1,476 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const Assert = require('@hapi/hoek/lib/assert');
|
|
4
|
+
const Clone = require('@hapi/hoek/lib/clone');
|
|
5
|
+
|
|
6
|
+
const Common = require('./common');
|
|
7
|
+
const Messages = require('./messages');
|
|
8
|
+
const Ref = require('./ref');
|
|
9
|
+
const Template = require('./template');
|
|
10
|
+
|
|
11
|
+
let Schemas;
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
const internals = {};
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
exports.describe = function (schema) {
|
|
18
|
+
|
|
19
|
+
const def = schema._definition;
|
|
20
|
+
|
|
21
|
+
// Type
|
|
22
|
+
|
|
23
|
+
const desc = {
|
|
24
|
+
type: schema.type,
|
|
25
|
+
flags: {},
|
|
26
|
+
rules: []
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
// Flags
|
|
30
|
+
|
|
31
|
+
for (const flag in schema._flags) {
|
|
32
|
+
if (flag[0] !== '_') {
|
|
33
|
+
desc.flags[flag] = internals.describe(schema._flags[flag]);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (!Object.keys(desc.flags).length) {
|
|
38
|
+
delete desc.flags;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Preferences
|
|
42
|
+
|
|
43
|
+
if (schema._preferences) {
|
|
44
|
+
desc.preferences = Clone(schema._preferences, { shallow: ['messages'] });
|
|
45
|
+
delete desc.preferences[Common.symbols.prefs];
|
|
46
|
+
if (desc.preferences.messages) {
|
|
47
|
+
desc.preferences.messages = Messages.decompile(desc.preferences.messages);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Allow / Invalid
|
|
52
|
+
|
|
53
|
+
if (schema._valids) {
|
|
54
|
+
desc.allow = schema._valids.describe();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (schema._invalids) {
|
|
58
|
+
desc.invalid = schema._invalids.describe();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Rules
|
|
62
|
+
|
|
63
|
+
for (const rule of schema._rules) {
|
|
64
|
+
const ruleDef = def.rules[rule.name];
|
|
65
|
+
if (ruleDef.manifest === false) { // Defaults to true
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const item = { name: rule.name };
|
|
70
|
+
|
|
71
|
+
for (const custom in def.modifiers) {
|
|
72
|
+
if (rule[custom] !== undefined) {
|
|
73
|
+
item[custom] = internals.describe(rule[custom]);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (rule.args) {
|
|
78
|
+
item.args = {};
|
|
79
|
+
for (const key in rule.args) {
|
|
80
|
+
const arg = rule.args[key];
|
|
81
|
+
if (key === 'options' &&
|
|
82
|
+
!Object.keys(arg).length) {
|
|
83
|
+
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
item.args[key] = internals.describe(arg, { assign: key });
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (!Object.keys(item.args).length) {
|
|
91
|
+
delete item.args;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
desc.rules.push(item);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (!desc.rules.length) {
|
|
99
|
+
delete desc.rules;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Terms (must be last to verify no name conflicts)
|
|
103
|
+
|
|
104
|
+
for (const term in schema.$_terms) {
|
|
105
|
+
if (term[0] === '_') {
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
Assert(!desc[term], 'Cannot describe schema due to internal name conflict with', term);
|
|
110
|
+
|
|
111
|
+
const items = schema.$_terms[term];
|
|
112
|
+
if (!items) {
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (items instanceof Map) {
|
|
117
|
+
if (items.size) {
|
|
118
|
+
desc[term] = [...items.entries()];
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (Common.isValues(items)) {
|
|
125
|
+
desc[term] = items.describe();
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
Assert(def.terms[term], 'Term', term, 'missing configuration');
|
|
130
|
+
const manifest = def.terms[term].manifest;
|
|
131
|
+
const mapped = typeof manifest === 'object';
|
|
132
|
+
if (!items.length &&
|
|
133
|
+
!mapped) {
|
|
134
|
+
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const normalized = [];
|
|
139
|
+
for (const item of items) {
|
|
140
|
+
normalized.push(internals.describe(item));
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Mapped
|
|
144
|
+
|
|
145
|
+
if (mapped) {
|
|
146
|
+
const { from, to } = manifest.mapped;
|
|
147
|
+
desc[term] = {};
|
|
148
|
+
for (const item of normalized) {
|
|
149
|
+
desc[term][item[to]] = item[from];
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Single
|
|
156
|
+
|
|
157
|
+
if (manifest === 'single') {
|
|
158
|
+
Assert(normalized.length === 1, 'Term', term, 'contains more than one item');
|
|
159
|
+
desc[term] = normalized[0];
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// Array
|
|
164
|
+
|
|
165
|
+
desc[term] = normalized;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
internals.validate(schema.$_root, desc);
|
|
169
|
+
return desc;
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
internals.describe = function (item, options = {}) {
|
|
174
|
+
|
|
175
|
+
if (Array.isArray(item)) {
|
|
176
|
+
return item.map(internals.describe);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
if (item === Common.symbols.deepDefault) {
|
|
180
|
+
return { special: 'deep' };
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (typeof item !== 'object' ||
|
|
184
|
+
item === null) {
|
|
185
|
+
|
|
186
|
+
return item;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
if (options.assign === 'options') {
|
|
190
|
+
return Clone(item);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (Buffer && Buffer.isBuffer(item)) { // $lab:coverage:ignore$
|
|
194
|
+
return { buffer: item.toString('binary') };
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (item instanceof Date) {
|
|
198
|
+
return item.toISOString();
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if (item instanceof Error) {
|
|
202
|
+
return item;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if (item instanceof RegExp) {
|
|
206
|
+
if (options.assign === 'regex') {
|
|
207
|
+
return item.toString();
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
return { regex: item.toString() };
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (item[Common.symbols.literal]) {
|
|
214
|
+
return { function: item.literal };
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
if (typeof item.describe === 'function') {
|
|
218
|
+
if (options.assign === 'ref') {
|
|
219
|
+
return item.describe().ref;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
return item.describe();
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const normalized = {};
|
|
226
|
+
for (const key in item) {
|
|
227
|
+
const value = item[key];
|
|
228
|
+
if (value === undefined) {
|
|
229
|
+
continue;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
normalized[key] = internals.describe(value, { assign: key });
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
return normalized;
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
exports.build = function (joi, desc) {
|
|
240
|
+
|
|
241
|
+
const builder = new internals.Builder(joi);
|
|
242
|
+
return builder.parse(desc);
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
internals.Builder = class {
|
|
247
|
+
|
|
248
|
+
constructor(joi) {
|
|
249
|
+
|
|
250
|
+
this.joi = joi;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
parse(desc) {
|
|
254
|
+
|
|
255
|
+
internals.validate(this.joi, desc);
|
|
256
|
+
|
|
257
|
+
// Type
|
|
258
|
+
|
|
259
|
+
let schema = this.joi[desc.type]()._bare();
|
|
260
|
+
const def = schema._definition;
|
|
261
|
+
|
|
262
|
+
// Flags
|
|
263
|
+
|
|
264
|
+
if (desc.flags) {
|
|
265
|
+
for (const flag in desc.flags) {
|
|
266
|
+
const setter = def.flags[flag] && def.flags[flag].setter || flag;
|
|
267
|
+
Assert(typeof schema[setter] === 'function', 'Invalid flag', flag, 'for type', desc.type);
|
|
268
|
+
schema = schema[setter](this.build(desc.flags[flag]));
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// Preferences
|
|
273
|
+
|
|
274
|
+
if (desc.preferences) {
|
|
275
|
+
schema = schema.preferences(this.build(desc.preferences));
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// Allow / Invalid
|
|
279
|
+
|
|
280
|
+
if (desc.allow) {
|
|
281
|
+
schema = schema.allow(...this.build(desc.allow));
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
if (desc.invalid) {
|
|
285
|
+
schema = schema.invalid(...this.build(desc.invalid));
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// Rules
|
|
289
|
+
|
|
290
|
+
if (desc.rules) {
|
|
291
|
+
for (const rule of desc.rules) {
|
|
292
|
+
Assert(typeof schema[rule.name] === 'function', 'Invalid rule', rule.name, 'for type', desc.type);
|
|
293
|
+
|
|
294
|
+
const args = [];
|
|
295
|
+
if (rule.args) {
|
|
296
|
+
const built = {};
|
|
297
|
+
for (const key in rule.args) {
|
|
298
|
+
built[key] = this.build(rule.args[key], { assign: key });
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
const keys = Object.keys(built);
|
|
302
|
+
const definition = def.rules[rule.name].args;
|
|
303
|
+
if (definition) {
|
|
304
|
+
Assert(keys.length <= definition.length, 'Invalid number of arguments for', desc.type, rule.name, '(expected up to', definition.length, ', found', keys.length, ')');
|
|
305
|
+
for (const { name } of definition) {
|
|
306
|
+
args.push(built[name]);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
else {
|
|
310
|
+
Assert(keys.length === 1, 'Invalid number of arguments for', desc.type, rule.name, '(expected up to 1, found', keys.length, ')');
|
|
311
|
+
args.push(built[keys[0]]);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// Apply
|
|
316
|
+
|
|
317
|
+
schema = schema[rule.name](...args);
|
|
318
|
+
|
|
319
|
+
// Ruleset
|
|
320
|
+
|
|
321
|
+
const options = {};
|
|
322
|
+
for (const custom in def.modifiers) {
|
|
323
|
+
if (rule[custom] !== undefined) {
|
|
324
|
+
options[custom] = this.build(rule[custom]);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
if (Object.keys(options).length) {
|
|
329
|
+
schema = schema.rule(options);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// Terms
|
|
335
|
+
|
|
336
|
+
const terms = {};
|
|
337
|
+
for (const key in desc) {
|
|
338
|
+
if (['allow', 'flags', 'invalid', 'whens', 'preferences', 'rules', 'type'].includes(key)) {
|
|
339
|
+
continue;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
Assert(def.terms[key], 'Term', key, 'missing configuration');
|
|
343
|
+
const manifest = def.terms[key].manifest;
|
|
344
|
+
|
|
345
|
+
if (manifest === 'schema') {
|
|
346
|
+
terms[key] = desc[key].map((item) => this.parse(item));
|
|
347
|
+
continue;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
if (manifest === 'values') {
|
|
351
|
+
terms[key] = desc[key].map((item) => this.build(item));
|
|
352
|
+
continue;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
if (manifest === 'single') {
|
|
356
|
+
terms[key] = this.build(desc[key]);
|
|
357
|
+
continue;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
if (typeof manifest === 'object') {
|
|
361
|
+
terms[key] = {};
|
|
362
|
+
for (const name in desc[key]) {
|
|
363
|
+
const value = desc[key][name];
|
|
364
|
+
terms[key][name] = this.parse(value);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
continue;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
terms[key] = this.build(desc[key]);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
if (desc.whens) {
|
|
374
|
+
terms.whens = desc.whens.map((when) => this.build(when));
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
schema = def.manifest.build(schema, terms);
|
|
378
|
+
schema.$_temp.ruleset = false;
|
|
379
|
+
return schema;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
build(desc, options = {}) {
|
|
383
|
+
|
|
384
|
+
if (desc === null) {
|
|
385
|
+
return null;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
if (Array.isArray(desc)) {
|
|
389
|
+
return desc.map((item) => this.build(item));
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
if (desc instanceof Error) {
|
|
393
|
+
return desc;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
if (options.assign === 'options') {
|
|
397
|
+
return Clone(desc);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
if (options.assign === 'regex') {
|
|
401
|
+
return internals.regex(desc);
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
if (options.assign === 'ref') {
|
|
405
|
+
return Ref.build(desc);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
if (typeof desc !== 'object') {
|
|
409
|
+
return desc;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
if (Object.keys(desc).length === 1) {
|
|
413
|
+
if (desc.buffer) {
|
|
414
|
+
Assert(Buffer, 'Buffers are not supported');
|
|
415
|
+
return Buffer && Buffer.from(desc.buffer, 'binary'); // $lab:coverage:ignore$
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
if (desc.function) {
|
|
419
|
+
return { [Common.symbols.literal]: true, literal: desc.function };
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
if (desc.override) {
|
|
423
|
+
return Common.symbols.override;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
if (desc.ref) {
|
|
427
|
+
return Ref.build(desc.ref);
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
if (desc.regex) {
|
|
431
|
+
return internals.regex(desc.regex);
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
if (desc.special) {
|
|
435
|
+
Assert(['deep'].includes(desc.special), 'Unknown special value', desc.special);
|
|
436
|
+
return Common.symbols.deepDefault;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
if (desc.value) {
|
|
440
|
+
return Clone(desc.value);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
if (desc.type) {
|
|
445
|
+
return this.parse(desc);
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
if (desc.template) {
|
|
449
|
+
return Template.build(desc);
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
const normalized = {};
|
|
453
|
+
for (const key in desc) {
|
|
454
|
+
normalized[key] = this.build(desc[key], { assign: key });
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
return normalized;
|
|
458
|
+
}
|
|
459
|
+
};
|
|
460
|
+
|
|
461
|
+
|
|
462
|
+
internals.regex = function (string) {
|
|
463
|
+
|
|
464
|
+
const end = string.lastIndexOf('/');
|
|
465
|
+
const exp = string.slice(1, end);
|
|
466
|
+
const flags = string.slice(end + 1);
|
|
467
|
+
return new RegExp(exp, flags);
|
|
468
|
+
};
|
|
469
|
+
|
|
470
|
+
|
|
471
|
+
internals.validate = function (joi, desc) {
|
|
472
|
+
|
|
473
|
+
Schemas = Schemas || require('./schemas');
|
|
474
|
+
|
|
475
|
+
joi.assert(desc, Schemas.description);
|
|
476
|
+
};
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const Assert = require('@hapi/hoek/lib/assert');
|
|
4
|
+
const Clone = require('@hapi/hoek/lib/clone');
|
|
5
|
+
|
|
6
|
+
const Template = require('./template');
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
const internals = {};
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
exports.compile = function (messages, target) {
|
|
13
|
+
|
|
14
|
+
// Single value string ('plain error message', 'template {error} message')
|
|
15
|
+
|
|
16
|
+
if (typeof messages === 'string') {
|
|
17
|
+
Assert(!target, 'Cannot set single message string');
|
|
18
|
+
return new Template(messages);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// Single value template
|
|
22
|
+
|
|
23
|
+
if (Template.isTemplate(messages)) {
|
|
24
|
+
Assert(!target, 'Cannot set single message template');
|
|
25
|
+
return messages;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// By error code { 'number.min': <string | template> }
|
|
29
|
+
|
|
30
|
+
Assert(typeof messages === 'object' && !Array.isArray(messages), 'Invalid message options');
|
|
31
|
+
|
|
32
|
+
target = target ? Clone(target) : {};
|
|
33
|
+
|
|
34
|
+
for (let code in messages) {
|
|
35
|
+
const message = messages[code];
|
|
36
|
+
|
|
37
|
+
if (code === 'root' ||
|
|
38
|
+
Template.isTemplate(message)) {
|
|
39
|
+
|
|
40
|
+
// A flat code named __proto__ triggers the legacy accessor on plain-object assignment, and
|
|
41
|
+
// what we assign is always a Template, i.e. an object the accessor accepts, so it replaces
|
|
42
|
+
// target's own prototype instead of creating an own property. That in turn makes
|
|
43
|
+
// Template.isTemplate(target) true, so every code renders that one message
|
|
44
|
+
|
|
45
|
+
Assert(code !== '__proto__', 'Cannot use __proto__ as a message code');
|
|
46
|
+
|
|
47
|
+
target[code] = message;
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (typeof message === 'string') {
|
|
52
|
+
|
|
53
|
+
Assert(code !== '__proto__', 'Cannot use __proto__ as a message code');
|
|
54
|
+
|
|
55
|
+
target[code] = new Template(message);
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// By language { english: { 'number.min': <string | template> } }
|
|
60
|
+
|
|
61
|
+
Assert(typeof message === 'object' && !Array.isArray(message), 'Invalid message for', code);
|
|
62
|
+
|
|
63
|
+
const language = code;
|
|
64
|
+
|
|
65
|
+
// Don't reuse an inherited object, otherwise a language named __proto__ or constructor writes on the prototype
|
|
66
|
+
|
|
67
|
+
const localizedTarget = Object.prototype.hasOwnProperty.call(target, language) ? target[language] : {};
|
|
68
|
+
target[language] = localizedTarget;
|
|
69
|
+
|
|
70
|
+
for (code in message) {
|
|
71
|
+
const localized = message[code];
|
|
72
|
+
|
|
73
|
+
Assert(code !== '__proto__', 'Cannot use __proto__ as a message code');
|
|
74
|
+
|
|
75
|
+
if (code === 'root' ||
|
|
76
|
+
Template.isTemplate(localized)) {
|
|
77
|
+
|
|
78
|
+
localizedTarget[code] = localized;
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
Assert(typeof localized === 'string', 'Invalid message for', code, 'in', language);
|
|
83
|
+
localizedTarget[code] = new Template(localized);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return target;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
exports.decompile = function (messages) {
|
|
92
|
+
|
|
93
|
+
// By error code { 'number.min': <string | template> }
|
|
94
|
+
|
|
95
|
+
const target = {};
|
|
96
|
+
for (let code in messages) {
|
|
97
|
+
const message = messages[code];
|
|
98
|
+
|
|
99
|
+
if (code === 'root') {
|
|
100
|
+
target.root = message;
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (Template.isTemplate(message)) {
|
|
105
|
+
target[code] = message.describe({ compact: true });
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// By language { english: { 'number.min': <string | template> } }
|
|
110
|
+
|
|
111
|
+
const language = code;
|
|
112
|
+
target[language] = {};
|
|
113
|
+
|
|
114
|
+
for (code in message) {
|
|
115
|
+
const localized = message[code];
|
|
116
|
+
|
|
117
|
+
if (code === 'root') {
|
|
118
|
+
target[language].root = localized;
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
target[language][code] = localized.describe({ compact: true });
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return target;
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
exports.merge = function (base, extended) {
|
|
131
|
+
|
|
132
|
+
if (!base) {
|
|
133
|
+
return exports.compile(extended);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (!extended) {
|
|
137
|
+
return base;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// Single value string
|
|
141
|
+
|
|
142
|
+
if (typeof extended === 'string') {
|
|
143
|
+
return new Template(extended);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// Single value template
|
|
147
|
+
|
|
148
|
+
if (Template.isTemplate(extended)) {
|
|
149
|
+
return extended;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// By error code { 'number.min': <string | template> }
|
|
153
|
+
|
|
154
|
+
const target = Clone(base);
|
|
155
|
+
|
|
156
|
+
for (let code in extended) {
|
|
157
|
+
const message = extended[code];
|
|
158
|
+
|
|
159
|
+
if (code === 'root' ||
|
|
160
|
+
Template.isTemplate(message)) {
|
|
161
|
+
|
|
162
|
+
// Same as in compile(), a flat code named __proto__ replaces target's own prototype
|
|
163
|
+
|
|
164
|
+
Assert(code !== '__proto__', 'Cannot use __proto__ as a message code');
|
|
165
|
+
|
|
166
|
+
target[code] = message;
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (typeof message === 'string') {
|
|
171
|
+
|
|
172
|
+
Assert(code !== '__proto__', 'Cannot use __proto__ as a message code');
|
|
173
|
+
|
|
174
|
+
target[code] = new Template(message);
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// By language { english: { 'number.min': <string | template> } }
|
|
179
|
+
|
|
180
|
+
Assert(typeof message === 'object' && !Array.isArray(message), 'Invalid message for', code);
|
|
181
|
+
|
|
182
|
+
const language = code;
|
|
183
|
+
|
|
184
|
+
// Same as in compile(), don't reuse an inherited object
|
|
185
|
+
|
|
186
|
+
const localizedTarget = Object.prototype.hasOwnProperty.call(target, language) ? target[language] : {};
|
|
187
|
+
target[language] = localizedTarget;
|
|
188
|
+
|
|
189
|
+
for (code in message) {
|
|
190
|
+
const localized = message[code];
|
|
191
|
+
|
|
192
|
+
Assert(code !== '__proto__', 'Cannot use __proto__ as a message code');
|
|
193
|
+
|
|
194
|
+
if (code === 'root' ||
|
|
195
|
+
Template.isTemplate(localized)) {
|
|
196
|
+
|
|
197
|
+
localizedTarget[code] = localized;
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
Assert(typeof localized === 'string', 'Invalid message for', code, 'in', language);
|
|
202
|
+
localizedTarget[code] = new Template(localized);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
return target;
|
|
207
|
+
};
|