@nocobase/plugin-workflow-javascript 2.1.0-alpha.12 → 2.1.0-alpha.14
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/dist/client/index.js +1 -1
- package/dist/externalVersion.js +4 -4
- package/dist/node_modules/isolated-vm/.clang-tidy +13 -0
- package/dist/node_modules/isolated-vm/.dockerignore +9 -0
- package/dist/node_modules/isolated-vm/Dockerfile.alpine +9 -0
- package/dist/node_modules/isolated-vm/Dockerfile.debian +12 -0
- package/dist/node_modules/isolated-vm/LICENSE +13 -0
- package/dist/node_modules/isolated-vm/binding.gyp +120 -0
- package/dist/node_modules/isolated-vm/include.js +3 -0
- package/dist/node_modules/isolated-vm/inspector-example.js +59 -0
- package/dist/node_modules/isolated-vm/isolated-vm.d.ts +820 -0
- package/dist/node_modules/isolated-vm/isolated-vm.js +1 -0
- package/dist/node_modules/isolated-vm/native-example/binding.gyp +23 -0
- package/dist/node_modules/isolated-vm/native-example/example.cc +61 -0
- package/dist/node_modules/isolated-vm/native-example/package.json +13 -0
- package/dist/node_modules/isolated-vm/native-example/usage.js +35 -0
- package/dist/node_modules/isolated-vm/out/isolated_vm.node +0 -0
- package/dist/node_modules/isolated-vm/package.json +1 -0
- package/dist/node_modules/isolated-vm/src/external_copy/error.h +33 -0
- package/dist/node_modules/isolated-vm/src/external_copy/external_copy.cc +509 -0
- package/dist/node_modules/isolated-vm/src/external_copy/external_copy.h +117 -0
- package/dist/node_modules/isolated-vm/src/external_copy/serializer.cc +85 -0
- package/dist/node_modules/isolated-vm/src/external_copy/serializer.h +136 -0
- package/dist/node_modules/isolated-vm/src/external_copy/serializer_nortti.cc +73 -0
- package/dist/node_modules/isolated-vm/src/external_copy/string.cc +124 -0
- package/dist/node_modules/isolated-vm/src/external_copy/string.h +28 -0
- package/dist/node_modules/isolated-vm/src/isolate/allocator.h +32 -0
- package/dist/node_modules/isolated-vm/src/isolate/allocator_nortti.cc +142 -0
- package/dist/node_modules/isolated-vm/src/isolate/class_handle.h +334 -0
- package/dist/node_modules/isolated-vm/src/isolate/cpu_profile_manager.cc +220 -0
- package/dist/node_modules/isolated-vm/src/isolate/cpu_profile_manager.h +100 -0
- package/dist/node_modules/isolated-vm/src/isolate/environment.cc +626 -0
- package/dist/node_modules/isolated-vm/src/isolate/environment.h +381 -0
- package/dist/node_modules/isolated-vm/src/isolate/executor.cc +198 -0
- package/dist/node_modules/isolated-vm/src/isolate/executor.h +183 -0
- package/dist/node_modules/isolated-vm/src/isolate/external.h +64 -0
- package/dist/node_modules/isolated-vm/src/isolate/functor_runners.h +97 -0
- package/dist/node_modules/isolated-vm/src/isolate/generic/array.h +145 -0
- package/dist/node_modules/isolated-vm/src/isolate/generic/callbacks.h +272 -0
- package/dist/node_modules/isolated-vm/src/isolate/generic/error.h +140 -0
- package/dist/node_modules/isolated-vm/src/isolate/generic/extract_params.h +145 -0
- package/dist/node_modules/isolated-vm/src/isolate/generic/handle_cast.h +257 -0
- package/dist/node_modules/isolated-vm/src/isolate/generic/read_option.h +47 -0
- package/dist/node_modules/isolated-vm/src/isolate/holder.cc +88 -0
- package/dist/node_modules/isolated-vm/src/isolate/holder.h +63 -0
- package/dist/node_modules/isolated-vm/src/isolate/inspector.cc +200 -0
- package/dist/node_modules/isolated-vm/src/isolate/inspector.h +70 -0
- package/dist/node_modules/isolated-vm/src/isolate/node_wrapper.h +15 -0
- package/dist/node_modules/isolated-vm/src/isolate/platform_delegate.cc +22 -0
- package/dist/node_modules/isolated-vm/src/isolate/platform_delegate.h +46 -0
- package/dist/node_modules/isolated-vm/src/isolate/remote_handle.h +164 -0
- package/dist/node_modules/isolated-vm/src/isolate/run_with_timeout.h +171 -0
- package/dist/node_modules/isolated-vm/src/isolate/runnable.h +29 -0
- package/dist/node_modules/isolated-vm/src/isolate/scheduler.cc +191 -0
- package/dist/node_modules/isolated-vm/src/isolate/scheduler.h +165 -0
- package/dist/node_modules/isolated-vm/src/isolate/specific.h +35 -0
- package/dist/node_modules/isolated-vm/src/isolate/stack_trace.cc +219 -0
- package/dist/node_modules/isolated-vm/src/isolate/stack_trace.h +24 -0
- package/dist/node_modules/isolated-vm/src/isolate/strings.h +127 -0
- package/dist/node_modules/isolated-vm/src/isolate/three_phase_task.cc +385 -0
- package/dist/node_modules/isolated-vm/src/isolate/three_phase_task.h +136 -0
- package/dist/node_modules/isolated-vm/src/isolate/transferable.h +15 -0
- package/dist/node_modules/isolated-vm/src/isolate/util.h +45 -0
- package/dist/node_modules/isolated-vm/src/isolate/v8_inspector_wrapper.h +12 -0
- package/dist/node_modules/isolated-vm/src/isolate/v8_version.h +12 -0
- package/dist/node_modules/isolated-vm/src/isolated_vm.h +71 -0
- package/dist/node_modules/isolated-vm/src/lib/covariant.h +50 -0
- package/dist/node_modules/isolated-vm/src/lib/lockable.h +178 -0
- package/dist/node_modules/isolated-vm/src/lib/suspend.h +106 -0
- package/dist/node_modules/isolated-vm/src/lib/thread_pool.cc +98 -0
- package/dist/node_modules/isolated-vm/src/lib/thread_pool.h +45 -0
- package/dist/node_modules/isolated-vm/src/lib/timer.cc +233 -0
- package/dist/node_modules/isolated-vm/src/lib/timer.h +36 -0
- package/dist/node_modules/isolated-vm/src/module/callback.cc +151 -0
- package/dist/node_modules/isolated-vm/src/module/callback.h +64 -0
- package/dist/node_modules/isolated-vm/src/module/context_handle.cc +241 -0
- package/dist/node_modules/isolated-vm/src/module/context_handle.h +35 -0
- package/dist/node_modules/isolated-vm/src/module/evaluation.cc +109 -0
- package/dist/node_modules/isolated-vm/src/module/evaluation.h +99 -0
- package/dist/node_modules/isolated-vm/src/module/external_copy_handle.cc +119 -0
- package/dist/node_modules/isolated-vm/src/module/external_copy_handle.h +64 -0
- package/dist/node_modules/isolated-vm/src/module/isolate.cc +136 -0
- package/dist/node_modules/isolated-vm/src/module/isolate_handle.cc +611 -0
- package/dist/node_modules/isolated-vm/src/module/isolate_handle.h +47 -0
- package/dist/node_modules/isolated-vm/src/module/lib_handle.cc +77 -0
- package/dist/node_modules/isolated-vm/src/module/lib_handle.h +28 -0
- package/dist/node_modules/isolated-vm/src/module/module_handle.cc +475 -0
- package/dist/node_modules/isolated-vm/src/module/module_handle.h +68 -0
- package/dist/node_modules/isolated-vm/src/module/native_module_handle.cc +104 -0
- package/dist/node_modules/isolated-vm/src/module/native_module_handle.h +49 -0
- package/dist/node_modules/isolated-vm/src/module/reference_handle.cc +636 -0
- package/dist/node_modules/isolated-vm/src/module/reference_handle.h +106 -0
- package/dist/node_modules/isolated-vm/src/module/script_handle.cc +107 -0
- package/dist/node_modules/isolated-vm/src/module/script_handle.h +37 -0
- package/dist/node_modules/isolated-vm/src/module/session_handle.cc +173 -0
- package/dist/node_modules/isolated-vm/src/module/session_handle.h +31 -0
- package/dist/node_modules/isolated-vm/src/module/transferable.cc +268 -0
- package/dist/node_modules/isolated-vm/src/module/transferable.h +42 -0
- package/dist/node_modules/isolated-vm/vendor/v8_inspector/nodejs_v18.0.0.h +360 -0
- package/dist/node_modules/isolated-vm/vendor/v8_inspector/nodejs_v18.3.0.h +376 -0
- package/dist/node_modules/isolated-vm/vendor/v8_inspector/nodejs_v20.0.0.h +397 -0
- package/dist/node_modules/isolated-vm/vendor/v8_inspector/nodejs_v22.0.0.h +419 -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 +178 -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 +1067 -0
- package/dist/node_modules/joi/lib/types/link.js +168 -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/node_modules/winston-transport/package.json +1 -1
- package/dist/server/IsolatedVm.js +75 -0
- package/dist/server/ScriptInstruction.d.ts +8 -0
- package/dist/server/ScriptInstruction.js +23 -1
- package/dist/server/Vm.js +42 -27
- package/package.json +4 -2
|
@@ -0,0 +1,1067 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const ApplyToDefaults = require('@hapi/hoek/lib/applyToDefaults');
|
|
4
|
+
const Assert = require('@hapi/hoek/lib/assert');
|
|
5
|
+
const Clone = require('@hapi/hoek/lib/clone');
|
|
6
|
+
const Topo = require('@hapi/topo');
|
|
7
|
+
|
|
8
|
+
const Any = require('./any');
|
|
9
|
+
const Common = require('../common');
|
|
10
|
+
const Compile = require('../compile');
|
|
11
|
+
const Errors = require('../errors');
|
|
12
|
+
const Ref = require('../ref');
|
|
13
|
+
const Template = require('../template');
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
const internals = {
|
|
17
|
+
renameDefaults: {
|
|
18
|
+
alias: false, // Keep old value in place
|
|
19
|
+
multiple: false, // Allow renaming multiple keys into the same target
|
|
20
|
+
override: false // Overrides an existing key
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
module.exports = Any.extend({
|
|
26
|
+
|
|
27
|
+
type: '_keys',
|
|
28
|
+
|
|
29
|
+
properties: {
|
|
30
|
+
|
|
31
|
+
typeof: 'object'
|
|
32
|
+
},
|
|
33
|
+
|
|
34
|
+
flags: {
|
|
35
|
+
|
|
36
|
+
unknown: { default: undefined }
|
|
37
|
+
},
|
|
38
|
+
|
|
39
|
+
terms: {
|
|
40
|
+
|
|
41
|
+
dependencies: { init: null },
|
|
42
|
+
keys: { init: null, manifest: { mapped: { from: 'schema', to: 'key' } } },
|
|
43
|
+
patterns: { init: null },
|
|
44
|
+
renames: { init: null }
|
|
45
|
+
},
|
|
46
|
+
|
|
47
|
+
args(schema, keys) {
|
|
48
|
+
|
|
49
|
+
return schema.keys(keys);
|
|
50
|
+
},
|
|
51
|
+
|
|
52
|
+
validate(value, { schema, error, state, prefs }) {
|
|
53
|
+
|
|
54
|
+
if (!value ||
|
|
55
|
+
typeof value !== schema.$_property('typeof') ||
|
|
56
|
+
Array.isArray(value)) {
|
|
57
|
+
|
|
58
|
+
return { value, errors: error('object.base', { type: schema.$_property('typeof') }) };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Skip if there are no other rules to test
|
|
62
|
+
|
|
63
|
+
if (!schema.$_terms.renames &&
|
|
64
|
+
!schema.$_terms.dependencies &&
|
|
65
|
+
!schema.$_terms.keys && // null allows any keys
|
|
66
|
+
!schema.$_terms.patterns &&
|
|
67
|
+
!schema.$_terms.externals) {
|
|
68
|
+
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Shallow clone value
|
|
73
|
+
|
|
74
|
+
value = internals.clone(value, prefs);
|
|
75
|
+
const errors = [];
|
|
76
|
+
|
|
77
|
+
// Rename keys
|
|
78
|
+
|
|
79
|
+
if (schema.$_terms.renames &&
|
|
80
|
+
!internals.rename(schema, value, state, prefs, errors)) {
|
|
81
|
+
|
|
82
|
+
return { value, errors };
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Anything allowed
|
|
86
|
+
|
|
87
|
+
if (!schema.$_terms.keys && // null allows any keys
|
|
88
|
+
!schema.$_terms.patterns &&
|
|
89
|
+
!schema.$_terms.dependencies) {
|
|
90
|
+
|
|
91
|
+
return { value, errors };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Defined keys
|
|
95
|
+
|
|
96
|
+
const unprocessed = new Set(Object.keys(value));
|
|
97
|
+
|
|
98
|
+
if (schema.$_terms.keys) {
|
|
99
|
+
const ancestors = [value, ...state.ancestors];
|
|
100
|
+
|
|
101
|
+
for (const child of schema.$_terms.keys) {
|
|
102
|
+
const key = child.key;
|
|
103
|
+
const item = value[key];
|
|
104
|
+
|
|
105
|
+
unprocessed.delete(key);
|
|
106
|
+
|
|
107
|
+
const localState = state.localize([...state.path, key], ancestors, child);
|
|
108
|
+
const result = child.schema.$_validate(item, localState, prefs);
|
|
109
|
+
|
|
110
|
+
if (result.errors) {
|
|
111
|
+
if (prefs.abortEarly) {
|
|
112
|
+
return { value, errors: result.errors };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (result.value !== undefined) {
|
|
116
|
+
value[key] = result.value;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
errors.push(...result.errors);
|
|
120
|
+
}
|
|
121
|
+
else if (child.schema._flags.result === 'strip' ||
|
|
122
|
+
result.value === undefined && item !== undefined) {
|
|
123
|
+
|
|
124
|
+
delete value[key];
|
|
125
|
+
}
|
|
126
|
+
else if (result.value !== undefined) {
|
|
127
|
+
value[key] = result.value;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// Unknown keys
|
|
133
|
+
|
|
134
|
+
if (unprocessed.size ||
|
|
135
|
+
schema._flags._hasPatternMatch) {
|
|
136
|
+
|
|
137
|
+
const early = internals.unknown(schema, value, unprocessed, errors, state, prefs);
|
|
138
|
+
if (early) {
|
|
139
|
+
return early;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Validate dependencies
|
|
144
|
+
|
|
145
|
+
if (schema.$_terms.dependencies) {
|
|
146
|
+
for (const dep of schema.$_terms.dependencies) {
|
|
147
|
+
if (
|
|
148
|
+
dep.key !== null &&
|
|
149
|
+
internals.isPresent(dep.options)(dep.key.resolve(value, state, prefs, null, { shadow: false })) === false
|
|
150
|
+
) {
|
|
151
|
+
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const failed = internals.dependencies[dep.rel](schema, dep, value, state, prefs);
|
|
156
|
+
if (failed) {
|
|
157
|
+
const report = schema.$_createError(failed.code, value, failed.context, state, prefs);
|
|
158
|
+
if (prefs.abortEarly) {
|
|
159
|
+
return { value, errors: report };
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
errors.push(report);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return { value, errors };
|
|
168
|
+
},
|
|
169
|
+
|
|
170
|
+
rules: {
|
|
171
|
+
|
|
172
|
+
and: {
|
|
173
|
+
method(...peers /*, [options] */) {
|
|
174
|
+
|
|
175
|
+
Common.verifyFlat(peers, 'and');
|
|
176
|
+
|
|
177
|
+
return internals.dependency(this, 'and', null, peers);
|
|
178
|
+
}
|
|
179
|
+
},
|
|
180
|
+
|
|
181
|
+
append: {
|
|
182
|
+
method(schema) {
|
|
183
|
+
|
|
184
|
+
if (schema === null ||
|
|
185
|
+
schema === undefined ||
|
|
186
|
+
Object.keys(schema).length === 0) {
|
|
187
|
+
|
|
188
|
+
return this;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
return this.keys(schema);
|
|
192
|
+
}
|
|
193
|
+
},
|
|
194
|
+
|
|
195
|
+
assert: {
|
|
196
|
+
method(subject, schema, message) {
|
|
197
|
+
|
|
198
|
+
if (!Template.isTemplate(subject)) {
|
|
199
|
+
subject = Compile.ref(subject);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
Assert(message === undefined || typeof message === 'string', 'Message must be a string');
|
|
203
|
+
|
|
204
|
+
schema = this.$_compile(schema, { appendPath: true });
|
|
205
|
+
|
|
206
|
+
const obj = this.$_addRule({ name: 'assert', args: { subject, schema, message } });
|
|
207
|
+
obj.$_mutateRegister(subject);
|
|
208
|
+
obj.$_mutateRegister(schema);
|
|
209
|
+
return obj;
|
|
210
|
+
},
|
|
211
|
+
validate(value, { error, prefs, state }, { subject, schema, message }) {
|
|
212
|
+
|
|
213
|
+
const about = subject.resolve(value, state, prefs);
|
|
214
|
+
const path = Ref.isRef(subject) ? subject.absolute(state) : [];
|
|
215
|
+
if (schema.$_match(about, state.localize(path, [value, ...state.ancestors], schema), prefs)) {
|
|
216
|
+
return value;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
return error('object.assert', { subject, message });
|
|
220
|
+
},
|
|
221
|
+
args: ['subject', 'schema', 'message'],
|
|
222
|
+
multi: true
|
|
223
|
+
},
|
|
224
|
+
|
|
225
|
+
instance: {
|
|
226
|
+
method(constructor, name) {
|
|
227
|
+
|
|
228
|
+
Assert(typeof constructor === 'function', 'constructor must be a function');
|
|
229
|
+
|
|
230
|
+
name = name || constructor.name;
|
|
231
|
+
|
|
232
|
+
return this.$_addRule({ name: 'instance', args: { constructor, name } });
|
|
233
|
+
},
|
|
234
|
+
validate(value, helpers, { constructor, name }) {
|
|
235
|
+
|
|
236
|
+
if (value instanceof constructor) {
|
|
237
|
+
return value;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
return helpers.error('object.instance', { type: name, value });
|
|
241
|
+
},
|
|
242
|
+
args: ['constructor', 'name']
|
|
243
|
+
},
|
|
244
|
+
|
|
245
|
+
keys: {
|
|
246
|
+
method(schema) {
|
|
247
|
+
|
|
248
|
+
Assert(schema === undefined || typeof schema === 'object', 'Object schema must be a valid object');
|
|
249
|
+
Assert(!Common.isSchema(schema), 'Object schema cannot be a joi schema');
|
|
250
|
+
|
|
251
|
+
const obj = this.clone();
|
|
252
|
+
|
|
253
|
+
if (!schema) { // Allow all
|
|
254
|
+
obj.$_terms.keys = null;
|
|
255
|
+
}
|
|
256
|
+
else if (!Object.keys(schema).length) { // Allow none
|
|
257
|
+
obj.$_terms.keys = new internals.Keys();
|
|
258
|
+
}
|
|
259
|
+
else {
|
|
260
|
+
obj.$_terms.keys = obj.$_terms.keys ? obj.$_terms.keys.filter((child) => !schema.hasOwnProperty(child.key)) : new internals.Keys();
|
|
261
|
+
for (const key in schema) {
|
|
262
|
+
Common.tryWithPath(() => obj.$_terms.keys.push({ key, schema: this.$_compile(schema[key]) }), key);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
return obj.$_mutateRebuild();
|
|
267
|
+
}
|
|
268
|
+
},
|
|
269
|
+
|
|
270
|
+
length: {
|
|
271
|
+
method(limit) {
|
|
272
|
+
|
|
273
|
+
return this.$_addRule({ name: 'length', args: { limit }, operator: '=' });
|
|
274
|
+
},
|
|
275
|
+
validate(value, helpers, { limit }, { name, operator, args }) {
|
|
276
|
+
|
|
277
|
+
if (Common.compare(Object.keys(value).length, limit, operator)) {
|
|
278
|
+
return value;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
return helpers.error('object.' + name, { limit: args.limit, value });
|
|
282
|
+
},
|
|
283
|
+
args: [
|
|
284
|
+
{
|
|
285
|
+
name: 'limit',
|
|
286
|
+
ref: true,
|
|
287
|
+
assert: Common.limit,
|
|
288
|
+
message: 'must be a positive integer'
|
|
289
|
+
}
|
|
290
|
+
]
|
|
291
|
+
},
|
|
292
|
+
|
|
293
|
+
max: {
|
|
294
|
+
method(limit) {
|
|
295
|
+
|
|
296
|
+
return this.$_addRule({ name: 'max', method: 'length', args: { limit }, operator: '<=' });
|
|
297
|
+
}
|
|
298
|
+
},
|
|
299
|
+
|
|
300
|
+
min: {
|
|
301
|
+
method(limit) {
|
|
302
|
+
|
|
303
|
+
return this.$_addRule({ name: 'min', method: 'length', args: { limit }, operator: '>=' });
|
|
304
|
+
}
|
|
305
|
+
},
|
|
306
|
+
|
|
307
|
+
nand: {
|
|
308
|
+
method(...peers /*, [options] */) {
|
|
309
|
+
|
|
310
|
+
Common.verifyFlat(peers, 'nand');
|
|
311
|
+
|
|
312
|
+
return internals.dependency(this, 'nand', null, peers);
|
|
313
|
+
}
|
|
314
|
+
},
|
|
315
|
+
|
|
316
|
+
or: {
|
|
317
|
+
method(...peers /*, [options] */) {
|
|
318
|
+
|
|
319
|
+
Common.verifyFlat(peers, 'or');
|
|
320
|
+
|
|
321
|
+
return internals.dependency(this, 'or', null, peers);
|
|
322
|
+
}
|
|
323
|
+
},
|
|
324
|
+
|
|
325
|
+
oxor: {
|
|
326
|
+
method(...peers /*, [options] */) {
|
|
327
|
+
|
|
328
|
+
return internals.dependency(this, 'oxor', null, peers);
|
|
329
|
+
}
|
|
330
|
+
},
|
|
331
|
+
|
|
332
|
+
pattern: {
|
|
333
|
+
method(pattern, schema, options = {}) {
|
|
334
|
+
|
|
335
|
+
const isRegExp = pattern instanceof RegExp;
|
|
336
|
+
if (!isRegExp) {
|
|
337
|
+
pattern = this.$_compile(pattern, { appendPath: true });
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
Assert(schema !== undefined, 'Invalid rule');
|
|
341
|
+
Common.assertOptions(options, ['fallthrough', 'matches']);
|
|
342
|
+
|
|
343
|
+
if (isRegExp) {
|
|
344
|
+
Assert(!pattern.flags.includes('g') && !pattern.flags.includes('y'), 'pattern should not use global or sticky mode');
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
schema = this.$_compile(schema, { appendPath: true });
|
|
348
|
+
|
|
349
|
+
const obj = this.clone();
|
|
350
|
+
obj.$_terms.patterns = obj.$_terms.patterns || [];
|
|
351
|
+
const config = { [isRegExp ? 'regex' : 'schema']: pattern, rule: schema };
|
|
352
|
+
if (options.matches) {
|
|
353
|
+
config.matches = this.$_compile(options.matches);
|
|
354
|
+
if (config.matches.type !== 'array') {
|
|
355
|
+
config.matches = config.matches.$_root.array().items(config.matches);
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
obj.$_mutateRegister(config.matches);
|
|
359
|
+
obj.$_setFlag('_hasPatternMatch', true, { clone: false });
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
if (options.fallthrough) {
|
|
363
|
+
config.fallthrough = true;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
obj.$_terms.patterns.push(config);
|
|
367
|
+
obj.$_mutateRegister(schema);
|
|
368
|
+
return obj;
|
|
369
|
+
}
|
|
370
|
+
},
|
|
371
|
+
|
|
372
|
+
ref: {
|
|
373
|
+
method() {
|
|
374
|
+
|
|
375
|
+
return this.$_addRule('ref');
|
|
376
|
+
},
|
|
377
|
+
validate(value, helpers) {
|
|
378
|
+
|
|
379
|
+
if (Ref.isRef(value)) {
|
|
380
|
+
return value;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
return helpers.error('object.refType', { value });
|
|
384
|
+
}
|
|
385
|
+
},
|
|
386
|
+
|
|
387
|
+
regex: {
|
|
388
|
+
method() {
|
|
389
|
+
|
|
390
|
+
return this.$_addRule('regex');
|
|
391
|
+
},
|
|
392
|
+
validate(value, helpers) {
|
|
393
|
+
|
|
394
|
+
if (value instanceof RegExp) {
|
|
395
|
+
return value;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
return helpers.error('object.regex', { value });
|
|
399
|
+
}
|
|
400
|
+
},
|
|
401
|
+
|
|
402
|
+
rename: {
|
|
403
|
+
method(from, to, options = {}) {
|
|
404
|
+
|
|
405
|
+
Assert(typeof from === 'string' || from instanceof RegExp, 'Rename missing the from argument');
|
|
406
|
+
Assert(typeof to === 'string' || to instanceof Template, 'Invalid rename to argument');
|
|
407
|
+
Assert(to !== from, 'Cannot rename key to same name:', from);
|
|
408
|
+
|
|
409
|
+
Common.assertOptions(options, ['alias', 'ignoreUndefined', 'override', 'multiple']);
|
|
410
|
+
|
|
411
|
+
const obj = this.clone();
|
|
412
|
+
|
|
413
|
+
obj.$_terms.renames = obj.$_terms.renames || [];
|
|
414
|
+
for (const rename of obj.$_terms.renames) {
|
|
415
|
+
Assert(rename.from !== from, 'Cannot rename the same key multiple times');
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
if (to instanceof Template) {
|
|
419
|
+
obj.$_mutateRegister(to);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
obj.$_terms.renames.push({
|
|
423
|
+
from,
|
|
424
|
+
to,
|
|
425
|
+
options: ApplyToDefaults(internals.renameDefaults, options)
|
|
426
|
+
});
|
|
427
|
+
|
|
428
|
+
return obj;
|
|
429
|
+
}
|
|
430
|
+
},
|
|
431
|
+
|
|
432
|
+
schema: {
|
|
433
|
+
method(type = 'any') {
|
|
434
|
+
|
|
435
|
+
return this.$_addRule({ name: 'schema', args: { type } });
|
|
436
|
+
},
|
|
437
|
+
validate(value, helpers, { type }) {
|
|
438
|
+
|
|
439
|
+
if (Common.isSchema(value) &&
|
|
440
|
+
(type === 'any' || value.type === type)) {
|
|
441
|
+
|
|
442
|
+
return value;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
return helpers.error('object.schema', { type });
|
|
446
|
+
}
|
|
447
|
+
},
|
|
448
|
+
|
|
449
|
+
unknown: {
|
|
450
|
+
method(allow) {
|
|
451
|
+
|
|
452
|
+
return this.$_setFlag('unknown', allow !== false);
|
|
453
|
+
}
|
|
454
|
+
},
|
|
455
|
+
|
|
456
|
+
with: {
|
|
457
|
+
method(key, peers, options = {}) {
|
|
458
|
+
|
|
459
|
+
return internals.dependency(this, 'with', key, peers, options);
|
|
460
|
+
}
|
|
461
|
+
},
|
|
462
|
+
|
|
463
|
+
without: {
|
|
464
|
+
method(key, peers, options = {}) {
|
|
465
|
+
|
|
466
|
+
return internals.dependency(this, 'without', key, peers, options);
|
|
467
|
+
}
|
|
468
|
+
},
|
|
469
|
+
|
|
470
|
+
xor: {
|
|
471
|
+
method(...peers /*, [options] */) {
|
|
472
|
+
|
|
473
|
+
Common.verifyFlat(peers, 'xor');
|
|
474
|
+
|
|
475
|
+
return internals.dependency(this, 'xor', null, peers);
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
},
|
|
479
|
+
|
|
480
|
+
overrides: {
|
|
481
|
+
|
|
482
|
+
default(value, options) {
|
|
483
|
+
|
|
484
|
+
if (value === undefined) {
|
|
485
|
+
value = Common.symbols.deepDefault;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
return this.$_parent('default', value, options);
|
|
489
|
+
}
|
|
490
|
+
},
|
|
491
|
+
|
|
492
|
+
rebuild(schema) {
|
|
493
|
+
|
|
494
|
+
if (schema.$_terms.keys) {
|
|
495
|
+
const topo = new Topo.Sorter();
|
|
496
|
+
for (const child of schema.$_terms.keys) {
|
|
497
|
+
Common.tryWithPath(() => topo.add(child, { after: child.schema.$_rootReferences(), group: child.key }), child.key);
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
schema.$_terms.keys = new internals.Keys(...topo.nodes);
|
|
501
|
+
}
|
|
502
|
+
},
|
|
503
|
+
|
|
504
|
+
manifest: {
|
|
505
|
+
|
|
506
|
+
build(obj, desc) {
|
|
507
|
+
|
|
508
|
+
if (desc.keys) {
|
|
509
|
+
obj = obj.keys(desc.keys);
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
if (desc.dependencies) {
|
|
513
|
+
for (const { rel, key = null, peers, options } of desc.dependencies) {
|
|
514
|
+
obj = internals.dependency(obj, rel, key, peers, options);
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
if (desc.patterns) {
|
|
519
|
+
for (const { regex, schema, rule, fallthrough, matches } of desc.patterns) {
|
|
520
|
+
obj = obj.pattern(regex || schema, rule, { fallthrough, matches });
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
if (desc.renames) {
|
|
525
|
+
for (const { from, to, options } of desc.renames) {
|
|
526
|
+
obj = obj.rename(from, to, options);
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
return obj;
|
|
531
|
+
}
|
|
532
|
+
},
|
|
533
|
+
|
|
534
|
+
messages: {
|
|
535
|
+
'object.and': '{{#label}} contains {{#presentWithLabels}} without its required peers {{#missingWithLabels}}',
|
|
536
|
+
'object.assert': '{{#label}} is invalid because {if(#subject.key, `"` + #subject.key + `" failed to ` + (#message || "pass the assertion test"), #message || "the assertion failed")}',
|
|
537
|
+
'object.base': '{{#label}} must be of type {{#type}}',
|
|
538
|
+
'object.instance': '{{#label}} must be an instance of {{:#type}}',
|
|
539
|
+
'object.length': '{{#label}} must have {{#limit}} key{if(#limit == 1, "", "s")}',
|
|
540
|
+
'object.max': '{{#label}} must have less than or equal to {{#limit}} key{if(#limit == 1, "", "s")}',
|
|
541
|
+
'object.min': '{{#label}} must have at least {{#limit}} key{if(#limit == 1, "", "s")}',
|
|
542
|
+
'object.missing': '{{#label}} must contain at least one of {{#peersWithLabels}}',
|
|
543
|
+
'object.nand': '{{:#mainWithLabel}} must not exist simultaneously with {{#peersWithLabels}}',
|
|
544
|
+
'object.oxor': '{{#label}} contains a conflict between optional exclusive peers {{#peersWithLabels}}',
|
|
545
|
+
'object.pattern.match': '{{#label}} keys failed to match pattern requirements',
|
|
546
|
+
'object.refType': '{{#label}} must be a Joi reference',
|
|
547
|
+
'object.regex': '{{#label}} must be a RegExp object',
|
|
548
|
+
'object.rename.multiple': '{{#label}} cannot rename {{:#from}} because multiple renames are disabled and another key was already renamed to {{:#to}}',
|
|
549
|
+
'object.rename.override': '{{#label}} cannot rename {{:#from}} because override is disabled and target {{:#to}} exists',
|
|
550
|
+
'object.schema': '{{#label}} must be a Joi schema of {{#type}} type',
|
|
551
|
+
'object.unknown': '{{#label}} is not allowed',
|
|
552
|
+
'object.with': '{{:#mainWithLabel}} missing required peer {{:#peerWithLabel}}',
|
|
553
|
+
'object.without': '{{:#mainWithLabel}} conflict with forbidden peer {{:#peerWithLabel}}',
|
|
554
|
+
'object.xor': '{{#label}} contains a conflict between exclusive peers {{#peersWithLabels}}'
|
|
555
|
+
}
|
|
556
|
+
});
|
|
557
|
+
|
|
558
|
+
|
|
559
|
+
// Helpers
|
|
560
|
+
|
|
561
|
+
internals.clone = function (value, prefs) {
|
|
562
|
+
|
|
563
|
+
// Object
|
|
564
|
+
|
|
565
|
+
if (typeof value === 'object') {
|
|
566
|
+
if (prefs.nonEnumerables) {
|
|
567
|
+
return Clone(value, { shallow: true });
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
const clone = Object.create(Object.getPrototypeOf(value));
|
|
571
|
+
Object.assign(clone, value);
|
|
572
|
+
return clone;
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
// Function
|
|
576
|
+
|
|
577
|
+
const clone = function (...args) {
|
|
578
|
+
|
|
579
|
+
return value.apply(this, args);
|
|
580
|
+
};
|
|
581
|
+
|
|
582
|
+
clone.prototype = Clone(value.prototype);
|
|
583
|
+
Object.defineProperty(clone, 'name', { value: value.name, writable: false });
|
|
584
|
+
Object.defineProperty(clone, 'length', { value: value.length, writable: false });
|
|
585
|
+
Object.assign(clone, value);
|
|
586
|
+
return clone;
|
|
587
|
+
};
|
|
588
|
+
|
|
589
|
+
|
|
590
|
+
internals.dependency = function (schema, rel, key, peers, options) {
|
|
591
|
+
|
|
592
|
+
Assert(key === null || typeof key === 'string', rel, 'key must be a strings');
|
|
593
|
+
|
|
594
|
+
// Extract options from peers array
|
|
595
|
+
|
|
596
|
+
if (!options) {
|
|
597
|
+
options = peers.length > 1 && typeof peers[peers.length - 1] === 'object' ? peers.pop() : {};
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
Common.assertOptions(options, ['separator', 'isPresent']);
|
|
601
|
+
|
|
602
|
+
peers = [].concat(peers);
|
|
603
|
+
|
|
604
|
+
// Cast peer paths
|
|
605
|
+
|
|
606
|
+
const separator = Common.default(options.separator, '.');
|
|
607
|
+
const paths = [];
|
|
608
|
+
for (const peer of peers) {
|
|
609
|
+
Assert(typeof peer === 'string', rel, 'peers must be strings');
|
|
610
|
+
paths.push(Compile.ref(peer, { separator, ancestor: 0, prefix: false }));
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
// Cast key
|
|
614
|
+
|
|
615
|
+
if (key !== null) {
|
|
616
|
+
key = Compile.ref(key, { separator, ancestor: 0, prefix: false });
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
// Add rule
|
|
620
|
+
|
|
621
|
+
const obj = schema.clone();
|
|
622
|
+
obj.$_terms.dependencies = obj.$_terms.dependencies || [];
|
|
623
|
+
obj.$_terms.dependencies.push(new internals.Dependency(rel, key, paths, peers, options));
|
|
624
|
+
return obj;
|
|
625
|
+
};
|
|
626
|
+
|
|
627
|
+
|
|
628
|
+
internals.dependencies = {
|
|
629
|
+
|
|
630
|
+
and(schema, dep, value, state, prefs) {
|
|
631
|
+
|
|
632
|
+
const missing = [];
|
|
633
|
+
const present = [];
|
|
634
|
+
const count = dep.peers.length;
|
|
635
|
+
const isPresent = internals.isPresent(dep.options);
|
|
636
|
+
for (const peer of dep.peers) {
|
|
637
|
+
if (isPresent(peer.resolve(value, state, prefs, null, { shadow: false })) === false) {
|
|
638
|
+
missing.push(peer.key);
|
|
639
|
+
}
|
|
640
|
+
else {
|
|
641
|
+
present.push(peer.key);
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
if (missing.length !== count &&
|
|
646
|
+
present.length !== count) {
|
|
647
|
+
|
|
648
|
+
return {
|
|
649
|
+
code: 'object.and',
|
|
650
|
+
context: {
|
|
651
|
+
present,
|
|
652
|
+
presentWithLabels: internals.keysToLabels(schema, present),
|
|
653
|
+
missing,
|
|
654
|
+
missingWithLabels: internals.keysToLabels(schema, missing)
|
|
655
|
+
}
|
|
656
|
+
};
|
|
657
|
+
}
|
|
658
|
+
},
|
|
659
|
+
|
|
660
|
+
nand(schema, dep, value, state, prefs) {
|
|
661
|
+
|
|
662
|
+
const present = [];
|
|
663
|
+
const isPresent = internals.isPresent(dep.options);
|
|
664
|
+
for (const peer of dep.peers) {
|
|
665
|
+
if (isPresent(peer.resolve(value, state, prefs, null, { shadow: false }))) {
|
|
666
|
+
present.push(peer.key);
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
if (present.length !== dep.peers.length) {
|
|
671
|
+
return;
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
const main = dep.paths[0];
|
|
675
|
+
const values = dep.paths.slice(1);
|
|
676
|
+
return {
|
|
677
|
+
code: 'object.nand',
|
|
678
|
+
context: {
|
|
679
|
+
main,
|
|
680
|
+
mainWithLabel: internals.keysToLabels(schema, main),
|
|
681
|
+
peers: values,
|
|
682
|
+
peersWithLabels: internals.keysToLabels(schema, values)
|
|
683
|
+
}
|
|
684
|
+
};
|
|
685
|
+
},
|
|
686
|
+
|
|
687
|
+
or(schema, dep, value, state, prefs) {
|
|
688
|
+
|
|
689
|
+
const isPresent = internals.isPresent(dep.options);
|
|
690
|
+
for (const peer of dep.peers) {
|
|
691
|
+
if (isPresent(peer.resolve(value, state, prefs, null, { shadow: false }))) {
|
|
692
|
+
return;
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
return {
|
|
697
|
+
code: 'object.missing',
|
|
698
|
+
context: {
|
|
699
|
+
peers: dep.paths,
|
|
700
|
+
peersWithLabels: internals.keysToLabels(schema, dep.paths)
|
|
701
|
+
}
|
|
702
|
+
};
|
|
703
|
+
},
|
|
704
|
+
|
|
705
|
+
oxor(schema, dep, value, state, prefs) {
|
|
706
|
+
|
|
707
|
+
const present = [];
|
|
708
|
+
const isPresent = internals.isPresent(dep.options);
|
|
709
|
+
for (const peer of dep.peers) {
|
|
710
|
+
if (isPresent(peer.resolve(value, state, prefs, null, { shadow: false }))) {
|
|
711
|
+
present.push(peer.key);
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
if (!present.length ||
|
|
716
|
+
present.length === 1) {
|
|
717
|
+
|
|
718
|
+
return;
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
const context = { peers: dep.paths, peersWithLabels: internals.keysToLabels(schema, dep.paths) };
|
|
722
|
+
context.present = present;
|
|
723
|
+
context.presentWithLabels = internals.keysToLabels(schema, present);
|
|
724
|
+
return { code: 'object.oxor', context };
|
|
725
|
+
},
|
|
726
|
+
|
|
727
|
+
with(schema, dep, value, state, prefs) {
|
|
728
|
+
|
|
729
|
+
const isPresent = internals.isPresent(dep.options);
|
|
730
|
+
for (const peer of dep.peers) {
|
|
731
|
+
if (isPresent(peer.resolve(value, state, prefs, null, { shadow: false })) === false) {
|
|
732
|
+
return {
|
|
733
|
+
code: 'object.with',
|
|
734
|
+
context: {
|
|
735
|
+
main: dep.key.key,
|
|
736
|
+
mainWithLabel: internals.keysToLabels(schema, dep.key.key),
|
|
737
|
+
peer: peer.key,
|
|
738
|
+
peerWithLabel: internals.keysToLabels(schema, peer.key)
|
|
739
|
+
}
|
|
740
|
+
};
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
},
|
|
744
|
+
|
|
745
|
+
without(schema, dep, value, state, prefs) {
|
|
746
|
+
|
|
747
|
+
const isPresent = internals.isPresent(dep.options);
|
|
748
|
+
for (const peer of dep.peers) {
|
|
749
|
+
if (isPresent(peer.resolve(value, state, prefs, null, { shadow: false }))) {
|
|
750
|
+
return {
|
|
751
|
+
code: 'object.without',
|
|
752
|
+
context: {
|
|
753
|
+
main: dep.key.key,
|
|
754
|
+
mainWithLabel: internals.keysToLabels(schema, dep.key.key),
|
|
755
|
+
peer: peer.key,
|
|
756
|
+
peerWithLabel: internals.keysToLabels(schema, peer.key)
|
|
757
|
+
}
|
|
758
|
+
};
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
},
|
|
762
|
+
|
|
763
|
+
xor(schema, dep, value, state, prefs) {
|
|
764
|
+
|
|
765
|
+
const present = [];
|
|
766
|
+
const isPresent = internals.isPresent(dep.options);
|
|
767
|
+
for (const peer of dep.peers) {
|
|
768
|
+
if (isPresent(peer.resolve(value, state, prefs, null, { shadow: false }))) {
|
|
769
|
+
present.push(peer.key);
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
if (present.length === 1) {
|
|
774
|
+
return;
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
const context = { peers: dep.paths, peersWithLabels: internals.keysToLabels(schema, dep.paths) };
|
|
778
|
+
if (present.length === 0) {
|
|
779
|
+
return { code: 'object.missing', context };
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
context.present = present;
|
|
783
|
+
context.presentWithLabels = internals.keysToLabels(schema, present);
|
|
784
|
+
return { code: 'object.xor', context };
|
|
785
|
+
}
|
|
786
|
+
};
|
|
787
|
+
|
|
788
|
+
|
|
789
|
+
internals.keysToLabels = function (schema, keys) {
|
|
790
|
+
|
|
791
|
+
if (Array.isArray(keys)) {
|
|
792
|
+
return keys.map((key) => schema.$_mapLabels(key));
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
return schema.$_mapLabels(keys);
|
|
796
|
+
};
|
|
797
|
+
|
|
798
|
+
|
|
799
|
+
internals.isPresent = function (options) {
|
|
800
|
+
|
|
801
|
+
return typeof options.isPresent === 'function' ? options.isPresent : (resolved) => resolved !== undefined;
|
|
802
|
+
};
|
|
803
|
+
|
|
804
|
+
|
|
805
|
+
internals.rename = function (schema, value, state, prefs, errors) {
|
|
806
|
+
|
|
807
|
+
const renamed = {};
|
|
808
|
+
for (const rename of schema.$_terms.renames) {
|
|
809
|
+
const matches = [];
|
|
810
|
+
const pattern = typeof rename.from !== 'string';
|
|
811
|
+
|
|
812
|
+
if (!pattern) {
|
|
813
|
+
if (Object.prototype.hasOwnProperty.call(value, rename.from) &&
|
|
814
|
+
(value[rename.from] !== undefined || !rename.options.ignoreUndefined)) {
|
|
815
|
+
|
|
816
|
+
matches.push(rename);
|
|
817
|
+
}
|
|
818
|
+
}
|
|
819
|
+
else {
|
|
820
|
+
for (const from in value) {
|
|
821
|
+
if (value[from] === undefined &&
|
|
822
|
+
rename.options.ignoreUndefined) {
|
|
823
|
+
|
|
824
|
+
continue;
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
if (from === rename.to) {
|
|
828
|
+
continue;
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
const match = rename.from.exec(from);
|
|
832
|
+
if (!match) {
|
|
833
|
+
continue;
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
matches.push({ from, to: rename.to, match });
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
for (const match of matches) {
|
|
841
|
+
const from = match.from;
|
|
842
|
+
let to = match.to;
|
|
843
|
+
if (to instanceof Template) {
|
|
844
|
+
to = to.render(value, state, prefs, match.match);
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
if (from === to) {
|
|
848
|
+
continue;
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
if (!rename.options.multiple &&
|
|
852
|
+
renamed[to]) {
|
|
853
|
+
|
|
854
|
+
errors.push(schema.$_createError('object.rename.multiple', value, { from, to, pattern }, state, prefs));
|
|
855
|
+
if (prefs.abortEarly) {
|
|
856
|
+
return false;
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
if (Object.prototype.hasOwnProperty.call(value, to) &&
|
|
861
|
+
!rename.options.override &&
|
|
862
|
+
!renamed[to]) {
|
|
863
|
+
|
|
864
|
+
errors.push(schema.$_createError('object.rename.override', value, { from, to, pattern }, state, prefs));
|
|
865
|
+
if (prefs.abortEarly) {
|
|
866
|
+
return false;
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
if (value[from] === undefined) {
|
|
871
|
+
delete value[to];
|
|
872
|
+
}
|
|
873
|
+
else {
|
|
874
|
+
value[to] = value[from];
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
renamed[to] = true;
|
|
878
|
+
|
|
879
|
+
if (!rename.options.alias) {
|
|
880
|
+
delete value[from];
|
|
881
|
+
}
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
|
|
885
|
+
return true;
|
|
886
|
+
};
|
|
887
|
+
|
|
888
|
+
|
|
889
|
+
internals.unknown = function (schema, value, unprocessed, errors, state, prefs) {
|
|
890
|
+
|
|
891
|
+
if (schema.$_terms.patterns) {
|
|
892
|
+
let hasMatches = false;
|
|
893
|
+
const matches = schema.$_terms.patterns.map((pattern) => {
|
|
894
|
+
|
|
895
|
+
if (pattern.matches) {
|
|
896
|
+
hasMatches = true;
|
|
897
|
+
return [];
|
|
898
|
+
}
|
|
899
|
+
});
|
|
900
|
+
|
|
901
|
+
const ancestors = [value, ...state.ancestors];
|
|
902
|
+
|
|
903
|
+
for (const key of unprocessed) {
|
|
904
|
+
const item = value[key];
|
|
905
|
+
const path = [...state.path, key];
|
|
906
|
+
|
|
907
|
+
for (let i = 0; i < schema.$_terms.patterns.length; ++i) {
|
|
908
|
+
const pattern = schema.$_terms.patterns[i];
|
|
909
|
+
if (pattern.regex) {
|
|
910
|
+
const match = pattern.regex.test(key);
|
|
911
|
+
state.mainstay.tracer.debug(state, 'rule', `pattern.${i}`, match ? 'pass' : 'error');
|
|
912
|
+
if (!match) {
|
|
913
|
+
continue;
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
else {
|
|
917
|
+
if (!pattern.schema.$_match(key, state.nest(pattern.schema, `pattern.${i}`), prefs)) {
|
|
918
|
+
continue;
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
|
|
922
|
+
unprocessed.delete(key);
|
|
923
|
+
|
|
924
|
+
const localState = state.localize(path, ancestors, { schema: pattern.rule, key });
|
|
925
|
+
const result = pattern.rule.$_validate(item, localState, prefs);
|
|
926
|
+
if (result.errors) {
|
|
927
|
+
if (prefs.abortEarly) {
|
|
928
|
+
return { value, errors: result.errors };
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
errors.push(...result.errors);
|
|
932
|
+
}
|
|
933
|
+
|
|
934
|
+
if (pattern.matches) {
|
|
935
|
+
matches[i].push(key);
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
value[key] = result.value;
|
|
939
|
+
if (!pattern.fallthrough) {
|
|
940
|
+
break;
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
|
|
945
|
+
// Validate pattern matches rules
|
|
946
|
+
|
|
947
|
+
if (hasMatches) {
|
|
948
|
+
for (let i = 0; i < matches.length; ++i) {
|
|
949
|
+
const match = matches[i];
|
|
950
|
+
if (!match) {
|
|
951
|
+
continue;
|
|
952
|
+
}
|
|
953
|
+
|
|
954
|
+
const stpm = schema.$_terms.patterns[i].matches;
|
|
955
|
+
const localState = state.localize(state.path, ancestors, stpm);
|
|
956
|
+
const result = stpm.$_validate(match, localState, prefs);
|
|
957
|
+
if (result.errors) {
|
|
958
|
+
const details = Errors.details(result.errors, { override: false });
|
|
959
|
+
details.matches = match;
|
|
960
|
+
const report = schema.$_createError('object.pattern.match', value, details, state, prefs);
|
|
961
|
+
if (prefs.abortEarly) {
|
|
962
|
+
return { value, errors: report };
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
errors.push(report);
|
|
966
|
+
}
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
|
|
971
|
+
if (!unprocessed.size ||
|
|
972
|
+
!schema.$_terms.keys && !schema.$_terms.patterns) { // If no keys or patterns specified, unknown keys allowed
|
|
973
|
+
|
|
974
|
+
return;
|
|
975
|
+
}
|
|
976
|
+
|
|
977
|
+
if (prefs.stripUnknown && typeof schema._flags.unknown === 'undefined' ||
|
|
978
|
+
prefs.skipFunctions) {
|
|
979
|
+
|
|
980
|
+
const stripUnknown = prefs.stripUnknown ? (prefs.stripUnknown === true ? true : !!prefs.stripUnknown.objects) : false;
|
|
981
|
+
|
|
982
|
+
for (const key of unprocessed) {
|
|
983
|
+
if (stripUnknown) {
|
|
984
|
+
delete value[key];
|
|
985
|
+
unprocessed.delete(key);
|
|
986
|
+
}
|
|
987
|
+
else if (typeof value[key] === 'function') {
|
|
988
|
+
unprocessed.delete(key);
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
}
|
|
992
|
+
|
|
993
|
+
const forbidUnknown = !Common.default(schema._flags.unknown, prefs.allowUnknown);
|
|
994
|
+
if (forbidUnknown) {
|
|
995
|
+
for (const unprocessedKey of unprocessed) {
|
|
996
|
+
const localState = state.localize([...state.path, unprocessedKey], []);
|
|
997
|
+
const report = schema.$_createError('object.unknown', value[unprocessedKey], { child: unprocessedKey }, localState, prefs, { flags: false });
|
|
998
|
+
if (prefs.abortEarly) {
|
|
999
|
+
return { value, errors: report };
|
|
1000
|
+
}
|
|
1001
|
+
|
|
1002
|
+
errors.push(report);
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
};
|
|
1006
|
+
|
|
1007
|
+
|
|
1008
|
+
internals.Dependency = class {
|
|
1009
|
+
|
|
1010
|
+
constructor(rel, key, peers, paths, options) {
|
|
1011
|
+
|
|
1012
|
+
this.rel = rel;
|
|
1013
|
+
this.key = key;
|
|
1014
|
+
this.peers = peers;
|
|
1015
|
+
this.paths = paths;
|
|
1016
|
+
this.options = options;
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
describe() {
|
|
1020
|
+
|
|
1021
|
+
const desc = {
|
|
1022
|
+
rel: this.rel,
|
|
1023
|
+
peers: this.paths
|
|
1024
|
+
};
|
|
1025
|
+
|
|
1026
|
+
if (this.key !== null) {
|
|
1027
|
+
desc.key = this.key.key;
|
|
1028
|
+
}
|
|
1029
|
+
|
|
1030
|
+
if (this.peers[0].separator !== '.') {
|
|
1031
|
+
desc.options = { ...desc.options, separator: this.peers[0].separator };
|
|
1032
|
+
}
|
|
1033
|
+
|
|
1034
|
+
if (this.options.isPresent) {
|
|
1035
|
+
desc.options = { ...desc.options, isPresent: this.options.isPresent };
|
|
1036
|
+
}
|
|
1037
|
+
|
|
1038
|
+
return desc;
|
|
1039
|
+
}
|
|
1040
|
+
};
|
|
1041
|
+
|
|
1042
|
+
|
|
1043
|
+
internals.Keys = class extends Array {
|
|
1044
|
+
|
|
1045
|
+
concat(source) {
|
|
1046
|
+
|
|
1047
|
+
const result = this.slice();
|
|
1048
|
+
|
|
1049
|
+
const keys = new Map();
|
|
1050
|
+
for (let i = 0; i < result.length; ++i) {
|
|
1051
|
+
keys.set(result[i].key, i);
|
|
1052
|
+
}
|
|
1053
|
+
|
|
1054
|
+
for (const item of source) {
|
|
1055
|
+
const key = item.key;
|
|
1056
|
+
const pos = keys.get(key);
|
|
1057
|
+
if (pos !== undefined) {
|
|
1058
|
+
result[pos] = { key, schema: result[pos].schema.concat(item.schema) };
|
|
1059
|
+
}
|
|
1060
|
+
else {
|
|
1061
|
+
result.push(item);
|
|
1062
|
+
}
|
|
1063
|
+
}
|
|
1064
|
+
|
|
1065
|
+
return result;
|
|
1066
|
+
}
|
|
1067
|
+
};
|