@nocobase/plugin-workflow 2.1.0-alpha.4 → 2.1.0-alpha.45

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.
Files changed (185) hide show
  1. package/LICENSE +201 -661
  2. package/README.md +79 -10
  3. package/dist/client/214.7e602cfe7a8251b8.js +10 -0
  4. package/dist/client/618.19af7f84261c815d.js +10 -0
  5. package/dist/client/67.452743ce8ec30617.js +10 -0
  6. package/dist/client/964.ffbf5b47ed12bbdc.js +10 -0
  7. package/dist/client/Branch.d.ts +7 -3
  8. package/dist/client/BranchContext.d.ts +18 -0
  9. package/dist/client/components/TimeoutInput.d.ts +11 -0
  10. package/dist/client/constants.d.ts +13 -0
  11. package/dist/client/flows/triggerWorkflows.d.ts +14 -1
  12. package/dist/client/hooks/{useWorkflowFilterActionProps.d.ts → useResourceFilterActionProps.d.ts} +1 -1
  13. package/dist/client/index.js +1 -1
  14. package/dist/client/nodes/create.d.ts +10 -0
  15. package/dist/client/nodes/destroy.d.ts +10 -0
  16. package/dist/client/nodes/index.d.ts +5 -0
  17. package/dist/client/nodes/query.d.ts +18 -2
  18. package/dist/client/nodes/update.d.ts +10 -0
  19. package/dist/client/schemas/collection.d.ts +8 -2
  20. package/dist/client/schemas/executions.d.ts +62 -2
  21. package/dist/client/triggers/collection.d.ts +14 -1
  22. package/dist/client/triggers/index.d.ts +4 -0
  23. package/dist/client/triggers/schedule/constants.d.ts +4 -0
  24. package/dist/client/triggers/schedule/index.d.ts +15 -0
  25. package/dist/client/utils.d.ts +17 -0
  26. package/dist/common/collections/executions.d.ts +43 -1
  27. package/dist/common/collections/executions.js +62 -1
  28. package/dist/common/collections/jobs.js +7 -0
  29. package/dist/common/collections/workflows.d.ts +65 -11
  30. package/dist/common/collections/workflows.js +34 -2
  31. package/dist/common/constants.d.ts +5 -0
  32. package/dist/common/constants.js +7 -0
  33. package/dist/externalVersion.js +15 -13
  34. package/dist/locale/de-DE.json +4 -0
  35. package/dist/locale/en-US.json +7 -0
  36. package/dist/locale/es-ES.json +4 -0
  37. package/dist/locale/fr-FR.json +4 -0
  38. package/dist/locale/hu-HU.json +7 -3
  39. package/dist/locale/id-ID.json +4 -0
  40. package/dist/locale/it-IT.json +4 -0
  41. package/dist/locale/ja-JP.json +5 -1
  42. package/dist/locale/ko-KR.json +4 -0
  43. package/dist/locale/nl-NL.json +7 -3
  44. package/dist/locale/pt-BR.json +4 -0
  45. package/dist/locale/ru-RU.json +4 -0
  46. package/dist/locale/tr-TR.json +4 -0
  47. package/dist/locale/uk-UA.json +7 -3
  48. package/dist/locale/vi-VN.json +7 -3
  49. package/dist/locale/zh-CN.json +12 -1
  50. package/dist/locale/zh-TW.json +7 -3
  51. package/dist/node_modules/cron-parser/lib/parser.js +1 -1
  52. package/dist/node_modules/cron-parser/package.json +1 -1
  53. package/dist/node_modules/joi/dist/joi-browser.min.js +1 -0
  54. package/dist/node_modules/joi/lib/annotate.js +175 -0
  55. package/dist/node_modules/joi/lib/base.js +1069 -0
  56. package/dist/node_modules/joi/lib/cache.js +143 -0
  57. package/dist/node_modules/joi/lib/common.js +216 -0
  58. package/dist/node_modules/joi/lib/compile.js +283 -0
  59. package/dist/node_modules/joi/lib/errors.js +271 -0
  60. package/dist/node_modules/joi/lib/extend.js +312 -0
  61. package/dist/node_modules/joi/lib/index.d.ts +2365 -0
  62. package/dist/node_modules/joi/lib/index.js +1 -0
  63. package/dist/node_modules/joi/lib/manifest.js +476 -0
  64. package/dist/node_modules/joi/lib/messages.js +178 -0
  65. package/dist/node_modules/joi/lib/modify.js +267 -0
  66. package/dist/node_modules/joi/lib/ref.js +414 -0
  67. package/dist/node_modules/joi/lib/schemas.js +302 -0
  68. package/dist/node_modules/joi/lib/state.js +166 -0
  69. package/dist/node_modules/joi/lib/template.js +463 -0
  70. package/dist/node_modules/joi/lib/trace.js +346 -0
  71. package/dist/node_modules/joi/lib/types/alternatives.js +364 -0
  72. package/dist/node_modules/joi/lib/types/any.js +174 -0
  73. package/dist/node_modules/joi/lib/types/array.js +809 -0
  74. package/dist/node_modules/joi/lib/types/binary.js +100 -0
  75. package/dist/node_modules/joi/lib/types/boolean.js +150 -0
  76. package/dist/node_modules/joi/lib/types/date.js +233 -0
  77. package/dist/node_modules/joi/lib/types/function.js +93 -0
  78. package/dist/node_modules/joi/lib/types/keys.js +1067 -0
  79. package/dist/node_modules/joi/lib/types/link.js +168 -0
  80. package/dist/node_modules/joi/lib/types/number.js +363 -0
  81. package/dist/node_modules/joi/lib/types/object.js +22 -0
  82. package/dist/node_modules/joi/lib/types/string.js +850 -0
  83. package/dist/node_modules/joi/lib/types/symbol.js +102 -0
  84. package/dist/node_modules/joi/lib/validator.js +750 -0
  85. package/dist/node_modules/joi/lib/values.js +263 -0
  86. package/dist/node_modules/joi/node_modules/@hapi/topo/lib/index.d.ts +60 -0
  87. package/dist/node_modules/joi/node_modules/@hapi/topo/lib/index.js +225 -0
  88. package/dist/node_modules/joi/node_modules/@hapi/topo/package.json +30 -0
  89. package/dist/node_modules/joi/package.json +1 -0
  90. package/dist/node_modules/lru-cache/dist/commonjs/diagnostics-channel.d.ts +5 -0
  91. package/dist/node_modules/lru-cache/dist/commonjs/diagnostics-channel.js +10 -0
  92. package/dist/node_modules/lru-cache/dist/commonjs/index.d.ts +1381 -0
  93. package/dist/node_modules/lru-cache/dist/commonjs/index.js +1692 -0
  94. package/dist/node_modules/lru-cache/dist/commonjs/index.min.js +1 -0
  95. package/dist/node_modules/lru-cache/dist/esm/browser/diagnostics-channel.d.ts +5 -0
  96. package/dist/node_modules/lru-cache/dist/esm/browser/diagnostics-channel.js +4 -0
  97. package/dist/node_modules/lru-cache/dist/esm/browser/index.d.ts +1381 -0
  98. package/dist/node_modules/lru-cache/dist/{mjs → esm/browser}/index.js +537 -179
  99. package/dist/node_modules/lru-cache/dist/esm/browser/index.min.js +2 -0
  100. package/dist/node_modules/lru-cache/dist/esm/diagnostics-channel.d.ts +5 -0
  101. package/dist/node_modules/lru-cache/dist/esm/diagnostics-channel.js +19 -0
  102. package/dist/node_modules/lru-cache/dist/esm/index.d.ts +1381 -0
  103. package/dist/node_modules/lru-cache/dist/{cjs → esm}/index.js +538 -184
  104. package/dist/node_modules/lru-cache/dist/esm/index.min.js +2 -0
  105. package/dist/node_modules/lru-cache/dist/esm/node/diagnostics-channel.d.ts +5 -0
  106. package/dist/node_modules/lru-cache/dist/esm/node/diagnostics-channel.js +7 -0
  107. package/dist/node_modules/lru-cache/dist/esm/node/index.d.ts +1381 -0
  108. package/dist/node_modules/lru-cache/dist/esm/node/index.js +1688 -0
  109. package/dist/node_modules/lru-cache/dist/esm/node/index.min.js +2 -0
  110. package/dist/node_modules/lru-cache/package.json +1 -1
  111. package/dist/node_modules/nodejs-snowflake/nodejs_snowflake.js +1 -1
  112. package/dist/node_modules/nodejs-snowflake/package.json +1 -1
  113. package/dist/server/Dispatcher.d.ts +9 -6
  114. package/dist/server/Dispatcher.js +241 -160
  115. package/dist/server/ExecutionTimeoutManager.d.ts +45 -0
  116. package/dist/server/ExecutionTimeoutManager.js +312 -0
  117. package/dist/server/Plugin.d.ts +13 -0
  118. package/dist/server/Plugin.js +49 -4
  119. package/dist/server/Processor.d.ts +65 -9
  120. package/dist/server/Processor.js +285 -33
  121. package/dist/server/RunningExecutionRegistry.d.ts +18 -0
  122. package/dist/server/RunningExecutionRegistry.js +48 -0
  123. package/dist/server/actions/executions.d.ts +4 -3
  124. package/dist/server/actions/executions.js +42 -21
  125. package/dist/server/actions/jobs.d.ts +2 -1
  126. package/dist/server/actions/jobs.js +28 -1
  127. package/dist/server/actions/nodes.d.ts +5 -0
  128. package/dist/server/actions/nodes.js +38 -5
  129. package/dist/server/actions/workflows.d.ts +6 -0
  130. package/dist/server/actions/workflows.js +38 -0
  131. package/dist/server/constants.d.ts +2 -0
  132. package/dist/server/constants.js +3 -0
  133. package/dist/server/index.d.ts +2 -0
  134. package/dist/server/index.js +2 -0
  135. package/dist/server/instructions/ConditionInstruction.d.ts +2 -0
  136. package/dist/server/instructions/ConditionInstruction.js +17 -0
  137. package/dist/server/instructions/CreateInstruction.d.ts +3 -0
  138. package/dist/server/instructions/CreateInstruction.js +25 -0
  139. package/dist/server/instructions/DestroyInstruction.d.ts +3 -0
  140. package/dist/server/instructions/DestroyInstruction.js +25 -0
  141. package/dist/server/instructions/EndInstruction.d.ts +2 -0
  142. package/dist/server/instructions/EndInstruction.js +4 -0
  143. package/dist/server/instructions/MultiConditionsInstruction.d.ts +2 -0
  144. package/dist/server/instructions/MultiConditionsInstruction.js +23 -0
  145. package/dist/server/instructions/OutputInstruction.d.ts +2 -0
  146. package/dist/server/instructions/OutputInstruction.js +14 -0
  147. package/dist/server/instructions/QueryInstruction.d.ts +3 -0
  148. package/dist/server/instructions/QueryInstruction.js +32 -7
  149. package/dist/server/instructions/UpdateInstruction.d.ts +3 -0
  150. package/dist/server/instructions/UpdateInstruction.js +27 -0
  151. package/dist/server/instructions/index.d.ts +24 -4
  152. package/dist/server/instructions/index.js +18 -0
  153. package/dist/server/migrations/20260423225800-fill-workflow-created-updated-by.d.ts +13 -0
  154. package/dist/server/migrations/20260423225800-fill-workflow-created-updated-by.js +57 -0
  155. package/dist/server/migrations/20260501120000-workflow-timeout.d.ts +13 -0
  156. package/dist/server/migrations/20260501120000-workflow-timeout.js +63 -0
  157. package/dist/server/timeout-errors.d.ts +13 -0
  158. package/dist/server/timeout-errors.js +47 -0
  159. package/dist/server/triggers/CollectionTrigger.d.ts +3 -0
  160. package/dist/server/triggers/CollectionTrigger.js +28 -0
  161. package/dist/server/triggers/ScheduleTrigger/index.d.ts +3 -0
  162. package/dist/server/triggers/ScheduleTrigger/index.js +18 -3
  163. package/dist/server/triggers/index.d.ts +3 -0
  164. package/dist/server/triggers/index.js +18 -0
  165. package/dist/server/types/Execution.d.ts +6 -0
  166. package/dist/server/types/Job.d.ts +3 -3
  167. package/dist/server/types/Workflow.d.ts +6 -1
  168. package/dist/server/utils.d.ts +27 -0
  169. package/dist/server/utils.js +138 -2
  170. package/dist/swagger/index.d.ts +849 -106
  171. package/dist/swagger/index.js +969 -208
  172. package/package.json +6 -5
  173. package/dist/client/27bd65abee87cafa.js +0 -10
  174. package/dist/client/478692c1637f2742.js +0 -10
  175. package/dist/client/c1347b9d21f864d9.js +0 -10
  176. package/dist/client/f39e94207f92e352.js +0 -10
  177. package/dist/node_modules/lru-cache/LICENSE +0 -15
  178. package/dist/node_modules/lru-cache/dist/cjs/index-cjs.d.ts +0 -7
  179. package/dist/node_modules/lru-cache/dist/cjs/index-cjs.js +0 -1
  180. package/dist/node_modules/lru-cache/dist/cjs/index.d.ts +0 -807
  181. package/dist/node_modules/lru-cache/dist/cjs/index.min.js +0 -2
  182. package/dist/node_modules/lru-cache/dist/mjs/index.d.ts +0 -807
  183. package/dist/node_modules/lru-cache/dist/mjs/index.min.js +0 -2
  184. /package/dist/node_modules/lru-cache/dist/{cjs → commonjs}/package.json +0 -0
  185. /package/dist/node_modules/lru-cache/dist/{mjs → esm}/package.json +0 -0
@@ -0,0 +1,750 @@
1
+ 'use strict';
2
+
3
+ const Assert = require('@hapi/hoek/lib/assert');
4
+ const Clone = require('@hapi/hoek/lib/clone');
5
+ const Ignore = require('@hapi/hoek/lib/ignore');
6
+ const Reach = require('@hapi/hoek/lib/reach');
7
+
8
+ const Common = require('./common');
9
+ const Errors = require('./errors');
10
+ const State = require('./state');
11
+
12
+
13
+ const internals = {
14
+ result: Symbol('result')
15
+ };
16
+
17
+
18
+ exports.entry = function (value, schema, prefs) {
19
+
20
+ let settings = Common.defaults;
21
+ if (prefs) {
22
+ Assert(prefs.warnings === undefined, 'Cannot override warnings preference in synchronous validation');
23
+ Assert(prefs.artifacts === undefined, 'Cannot override artifacts preference in synchronous validation');
24
+ settings = Common.preferences(Common.defaults, prefs);
25
+ }
26
+
27
+ const result = internals.entry(value, schema, settings);
28
+ Assert(!result.mainstay.externals.length, 'Schema with external rules must use validateAsync()');
29
+ const outcome = { value: result.value };
30
+
31
+ if (result.error) {
32
+ outcome.error = result.error;
33
+ }
34
+
35
+ if (result.mainstay.warnings.length) {
36
+ outcome.warning = Errors.details(result.mainstay.warnings);
37
+ }
38
+
39
+ if (result.mainstay.debug) {
40
+ outcome.debug = result.mainstay.debug;
41
+ }
42
+
43
+ if (result.mainstay.artifacts) {
44
+ outcome.artifacts = result.mainstay.artifacts;
45
+ }
46
+
47
+ return outcome;
48
+ };
49
+
50
+
51
+ exports.entryAsync = async function (value, schema, prefs) {
52
+
53
+ let settings = Common.defaults;
54
+ if (prefs) {
55
+ settings = Common.preferences(Common.defaults, prefs);
56
+ }
57
+
58
+ const result = internals.entry(value, schema, settings);
59
+ const mainstay = result.mainstay;
60
+ if (result.error) {
61
+ if (mainstay.debug) {
62
+ result.error.debug = mainstay.debug;
63
+ }
64
+
65
+ throw result.error;
66
+ }
67
+
68
+ if (mainstay.externals.length) {
69
+ let root = result.value;
70
+ const errors = [];
71
+ for (const external of mainstay.externals) {
72
+ const path = external.state.path;
73
+ const linked = external.schema.type === 'link' ? mainstay.links.get(external.schema) : null;
74
+ let node = root;
75
+ let key;
76
+ let parent;
77
+
78
+ const ancestors = path.length ? [root] : [];
79
+ const original = path.length ? Reach(value, path) : value;
80
+
81
+ if (path.length) {
82
+ key = path[path.length - 1];
83
+
84
+ let current = root;
85
+ for (const segment of path.slice(0, -1)) {
86
+ current = current[segment];
87
+ ancestors.unshift(current);
88
+ }
89
+
90
+ parent = ancestors[0];
91
+ node = parent[key];
92
+ }
93
+
94
+ try {
95
+ const createError = (code, local) => (linked || external.schema).$_createError(code, node, local, external.state, settings);
96
+ const output = await external.method(node, {
97
+ schema: external.schema,
98
+ linked,
99
+ state: external.state,
100
+ prefs,
101
+ original,
102
+ error: createError,
103
+ errorsArray: internals.errorsArray,
104
+ warn: (code, local) => mainstay.warnings.push((linked || external.schema).$_createError(code, node, local, external.state, settings)),
105
+ message: (messages, local) => (linked || external.schema).$_createError('external', node, local, external.state, settings, { messages })
106
+ });
107
+
108
+ if (output === undefined ||
109
+ output === node) {
110
+
111
+ continue;
112
+ }
113
+
114
+ if (output instanceof Errors.Report) {
115
+ mainstay.tracer.log(external.schema, external.state, 'rule', 'external', 'error');
116
+ errors.push(output);
117
+
118
+ if (settings.abortEarly) {
119
+ break;
120
+ }
121
+
122
+ continue;
123
+ }
124
+
125
+ if (Array.isArray(output) &&
126
+ output[Common.symbols.errors]) {
127
+ mainstay.tracer.log(external.schema, external.state, 'rule', 'external', 'error');
128
+ errors.push(...output);
129
+
130
+ if (settings.abortEarly) {
131
+ break;
132
+ }
133
+
134
+ continue;
135
+ }
136
+
137
+ if (parent) {
138
+ mainstay.tracer.value(external.state, 'rule', node, output, 'external');
139
+ parent[key] = output;
140
+ }
141
+ else {
142
+ mainstay.tracer.value(external.state, 'rule', root, output, 'external');
143
+ root = output;
144
+ }
145
+ }
146
+ catch (err) {
147
+ if (settings.errors.label) {
148
+ err.message += ` (${(external.label)})`; // Change message to include path
149
+ }
150
+
151
+ throw err;
152
+ }
153
+ }
154
+
155
+ result.value = root;
156
+
157
+ if (errors.length) {
158
+ result.error = Errors.process(errors, value, settings);
159
+
160
+ if (mainstay.debug) {
161
+ result.error.debug = mainstay.debug;
162
+ }
163
+
164
+ throw result.error;
165
+ }
166
+ }
167
+
168
+ if (!settings.warnings &&
169
+ !settings.debug &&
170
+ !settings.artifacts) {
171
+
172
+ return result.value;
173
+ }
174
+
175
+ const outcome = { value: result.value };
176
+ if (mainstay.warnings.length) {
177
+ outcome.warning = Errors.details(mainstay.warnings);
178
+ }
179
+
180
+ if (mainstay.debug) {
181
+ outcome.debug = mainstay.debug;
182
+ }
183
+
184
+ if (mainstay.artifacts) {
185
+ outcome.artifacts = mainstay.artifacts;
186
+ }
187
+
188
+ return outcome;
189
+ };
190
+
191
+
192
+ internals.Mainstay = class {
193
+
194
+ constructor(tracer, debug, links) {
195
+
196
+ this.externals = [];
197
+ this.warnings = [];
198
+ this.tracer = tracer;
199
+ this.debug = debug;
200
+ this.links = links;
201
+ this.shadow = null;
202
+ this.artifacts = null;
203
+
204
+ this._snapshots = [];
205
+ }
206
+
207
+ snapshot() {
208
+
209
+ this._snapshots.push({
210
+ externals: this.externals.slice(),
211
+ warnings: this.warnings.slice()
212
+ });
213
+ }
214
+
215
+ restore() {
216
+
217
+ const snapshot = this._snapshots.pop();
218
+ this.externals = snapshot.externals;
219
+ this.warnings = snapshot.warnings;
220
+ }
221
+
222
+ commit() {
223
+
224
+ this._snapshots.pop();
225
+ }
226
+ };
227
+
228
+
229
+ internals.entry = function (value, schema, prefs) {
230
+
231
+ // Prepare state
232
+
233
+ const { tracer, cleanup } = internals.tracer(schema, prefs);
234
+ const debug = prefs.debug ? [] : null;
235
+ const links = schema._ids._schemaChain ? new Map() : null;
236
+ const mainstay = new internals.Mainstay(tracer, debug, links);
237
+ const schemas = schema._ids._schemaChain ? [{ schema }] : null;
238
+ const state = new State([], [], { mainstay, schemas });
239
+
240
+ // Validate value
241
+
242
+ const result = exports.validate(value, schema, state, prefs);
243
+
244
+ // Process value and errors
245
+
246
+ if (cleanup) {
247
+ schema.$_root.untrace();
248
+ }
249
+
250
+ const error = Errors.process(result.errors, value, prefs);
251
+ return { value: result.value, error, mainstay };
252
+ };
253
+
254
+
255
+ internals.tracer = function (schema, prefs) {
256
+
257
+ if (schema.$_root._tracer) {
258
+ return { tracer: schema.$_root._tracer._register(schema) };
259
+ }
260
+
261
+ if (prefs.debug) {
262
+ Assert(schema.$_root.trace, 'Debug mode not supported');
263
+ return { tracer: schema.$_root.trace()._register(schema), cleanup: true };
264
+ }
265
+
266
+ return { tracer: internals.ignore };
267
+ };
268
+
269
+
270
+ exports.validate = function (value, schema, state, prefs, overrides = {}) {
271
+
272
+ if (schema.$_terms.whens) {
273
+ schema = schema._generate(value, state, prefs).schema;
274
+ }
275
+
276
+ // Setup state and settings
277
+
278
+ if (schema._preferences) {
279
+ prefs = internals.prefs(schema, prefs);
280
+ }
281
+
282
+ // Cache
283
+
284
+ if (schema._cache &&
285
+ prefs.cache) {
286
+
287
+ const result = schema._cache.get(value);
288
+ state.mainstay.tracer.debug(state, 'validate', 'cached', !!result);
289
+ if (result) {
290
+ return result;
291
+ }
292
+ }
293
+
294
+ // Helpers
295
+
296
+ const createError = (code, local, localState) => schema.$_createError(code, value, local, localState || state, prefs);
297
+ const helpers = {
298
+ original: value,
299
+ prefs,
300
+ schema,
301
+ state,
302
+ error: createError,
303
+ errorsArray: internals.errorsArray,
304
+ warn: (code, local, localState) => state.mainstay.warnings.push(createError(code, local, localState)),
305
+ message: (messages, local) => schema.$_createError('custom', value, local, state, prefs, { messages })
306
+ };
307
+
308
+ // Prepare
309
+
310
+ state.mainstay.tracer.entry(schema, state);
311
+
312
+ const def = schema._definition;
313
+ if (def.prepare &&
314
+ value !== undefined &&
315
+ prefs.convert) {
316
+
317
+ const prepared = def.prepare(value, helpers);
318
+ if (prepared) {
319
+ state.mainstay.tracer.value(state, 'prepare', value, prepared.value);
320
+ if (prepared.errors) {
321
+ return internals.finalize(prepared.value, [].concat(prepared.errors), helpers); // Prepare error always aborts early
322
+ }
323
+
324
+ value = prepared.value;
325
+ }
326
+ }
327
+
328
+ // Type coercion
329
+
330
+ if (def.coerce &&
331
+ value !== undefined &&
332
+ prefs.convert &&
333
+ (!def.coerce.from || def.coerce.from.includes(typeof value))) {
334
+
335
+ const coerced = def.coerce.method(value, helpers);
336
+ if (coerced) {
337
+ state.mainstay.tracer.value(state, 'coerced', value, coerced.value);
338
+ if (coerced.errors) {
339
+ return internals.finalize(coerced.value, [].concat(coerced.errors), helpers); // Coerce error always aborts early
340
+ }
341
+
342
+ value = coerced.value;
343
+ }
344
+ }
345
+
346
+ // Empty value
347
+
348
+ const empty = schema._flags.empty;
349
+ if (empty &&
350
+ empty.$_match(internals.trim(value, schema), state.nest(empty), Common.defaults)) {
351
+
352
+ state.mainstay.tracer.value(state, 'empty', value, undefined);
353
+ value = undefined;
354
+ }
355
+
356
+ // Presence requirements (required, optional, forbidden)
357
+
358
+ const presence = overrides.presence || schema._flags.presence || (schema._flags._endedSwitch ? null : prefs.presence);
359
+ if (value === undefined) {
360
+ if (presence === 'forbidden') {
361
+ return internals.finalize(value, null, helpers);
362
+ }
363
+
364
+ if (presence === 'required') {
365
+ return internals.finalize(value, [schema.$_createError('any.required', value, null, state, prefs)], helpers);
366
+ }
367
+
368
+ if (presence === 'optional') {
369
+ if (schema._flags.default !== Common.symbols.deepDefault) {
370
+ return internals.finalize(value, null, helpers);
371
+ }
372
+
373
+ state.mainstay.tracer.value(state, 'default', value, {});
374
+ value = {};
375
+ }
376
+ }
377
+ else if (presence === 'forbidden') {
378
+ return internals.finalize(value, [schema.$_createError('any.unknown', value, null, state, prefs)], helpers);
379
+ }
380
+
381
+ // Allowed values
382
+
383
+ const errors = [];
384
+
385
+ if (schema._valids) {
386
+ const match = schema._valids.get(value, state, prefs, schema._flags.insensitive);
387
+ if (match) {
388
+ if (prefs.convert) {
389
+ state.mainstay.tracer.value(state, 'valids', value, match.value);
390
+ value = match.value;
391
+ }
392
+
393
+ state.mainstay.tracer.filter(schema, state, 'valid', match);
394
+ return internals.finalize(value, null, helpers);
395
+ }
396
+
397
+ if (schema._flags.only) {
398
+ const report = schema.$_createError('any.only', value, { valids: schema._valids.values({ display: true }) }, state, prefs);
399
+ if (prefs.abortEarly) {
400
+ return internals.finalize(value, [report], helpers);
401
+ }
402
+
403
+ errors.push(report);
404
+ }
405
+ }
406
+
407
+ // Denied values
408
+
409
+ if (schema._invalids) {
410
+ const match = schema._invalids.get(value, state, prefs, schema._flags.insensitive);
411
+ if (match) {
412
+ state.mainstay.tracer.filter(schema, state, 'invalid', match);
413
+ const report = schema.$_createError('any.invalid', value, { invalids: schema._invalids.values({ display: true }) }, state, prefs);
414
+ if (prefs.abortEarly) {
415
+ return internals.finalize(value, [report], helpers);
416
+ }
417
+
418
+ errors.push(report);
419
+ }
420
+ }
421
+
422
+ // Base type
423
+
424
+ if (def.validate) {
425
+ const base = def.validate(value, helpers);
426
+ if (base) {
427
+ state.mainstay.tracer.value(state, 'base', value, base.value);
428
+ value = base.value;
429
+
430
+ if (base.errors) {
431
+ if (!Array.isArray(base.errors)) {
432
+ errors.push(base.errors);
433
+ return internals.finalize(value, errors, helpers); // Base error always aborts early
434
+ }
435
+
436
+ if (base.errors.length) {
437
+ errors.push(...base.errors);
438
+ return internals.finalize(value, errors, helpers); // Base error always aborts early
439
+ }
440
+ }
441
+ }
442
+ }
443
+
444
+ // Validate tests
445
+
446
+ if (!schema._rules.length) {
447
+ return internals.finalize(value, errors, helpers);
448
+ }
449
+
450
+ return internals.rules(value, errors, helpers);
451
+ };
452
+
453
+
454
+ internals.rules = function (value, errors, helpers) {
455
+
456
+ const { schema, state, prefs } = helpers;
457
+
458
+ for (const rule of schema._rules) {
459
+ const definition = schema._definition.rules[rule.method];
460
+
461
+ // Skip rules that are also applied in coerce step
462
+
463
+ if (definition.convert &&
464
+ prefs.convert) {
465
+
466
+ state.mainstay.tracer.log(schema, state, 'rule', rule.name, 'full');
467
+ continue;
468
+ }
469
+
470
+ // Resolve references
471
+
472
+ let ret;
473
+ let args = rule.args;
474
+ if (rule._resolve.length) {
475
+ args = Object.assign({}, args); // Shallow copy
476
+ for (const key of rule._resolve) {
477
+ const resolver = definition.argsByName.get(key);
478
+
479
+ const resolved = args[key].resolve(value, state, prefs);
480
+ const normalized = resolver.normalize ? resolver.normalize(resolved) : resolved;
481
+
482
+ const invalid = Common.validateArg(normalized, null, resolver);
483
+ if (invalid) {
484
+ ret = schema.$_createError('any.ref', resolved, { arg: key, ref: args[key], reason: invalid }, state, prefs);
485
+ break;
486
+ }
487
+
488
+ args[key] = normalized;
489
+ }
490
+ }
491
+
492
+ // Test rule
493
+
494
+ ret = ret || definition.validate(value, helpers, args, rule); // Use ret if already set to reference error
495
+
496
+ const result = internals.rule(ret, rule);
497
+ if (result.errors) {
498
+ state.mainstay.tracer.log(schema, state, 'rule', rule.name, 'error');
499
+
500
+ if (rule.warn) {
501
+ state.mainstay.warnings.push(...result.errors);
502
+ continue;
503
+ }
504
+
505
+ if (prefs.abortEarly) {
506
+ return internals.finalize(value, result.errors, helpers);
507
+ }
508
+
509
+ errors.push(...result.errors);
510
+ }
511
+ else {
512
+ state.mainstay.tracer.log(schema, state, 'rule', rule.name, 'pass');
513
+ state.mainstay.tracer.value(state, 'rule', value, result.value, rule.name);
514
+ value = result.value;
515
+ }
516
+ }
517
+
518
+ return internals.finalize(value, errors, helpers);
519
+ };
520
+
521
+
522
+ internals.rule = function (ret, rule) {
523
+
524
+ if (ret instanceof Errors.Report) {
525
+ internals.error(ret, rule);
526
+ return { errors: [ret], value: null };
527
+ }
528
+
529
+ if (Array.isArray(ret) &&
530
+ ret[Common.symbols.errors]) {
531
+
532
+ ret.forEach((report) => internals.error(report, rule));
533
+ return { errors: ret, value: null };
534
+ }
535
+
536
+ return { errors: null, value: ret };
537
+ };
538
+
539
+
540
+ internals.error = function (report, rule) {
541
+
542
+ if (rule.message) {
543
+ report._setTemplate(rule.message);
544
+ }
545
+
546
+ return report;
547
+ };
548
+
549
+
550
+ internals.finalize = function (value, errors, helpers) {
551
+
552
+ errors = errors || [];
553
+ const { schema, state, prefs } = helpers;
554
+
555
+ // Failover value
556
+
557
+ if (errors.length) {
558
+ const failover = internals.default('failover', undefined, errors, helpers);
559
+ if (failover !== undefined) {
560
+ state.mainstay.tracer.value(state, 'failover', value, failover);
561
+ value = failover;
562
+ errors = [];
563
+ }
564
+ }
565
+
566
+ // Error override
567
+
568
+ if (errors.length &&
569
+ schema._flags.error) {
570
+
571
+ if (typeof schema._flags.error === 'function') {
572
+ errors = schema._flags.error(errors);
573
+ if (!Array.isArray(errors)) {
574
+ errors = [errors];
575
+ }
576
+
577
+ for (const error of errors) {
578
+ Assert(error instanceof Error || error instanceof Errors.Report, 'error() must return an Error object');
579
+ }
580
+ }
581
+ else {
582
+ errors = [schema._flags.error];
583
+ }
584
+ }
585
+
586
+ // Default
587
+
588
+ if (value === undefined) {
589
+ const defaulted = internals.default('default', value, errors, helpers);
590
+ state.mainstay.tracer.value(state, 'default', value, defaulted);
591
+ value = defaulted;
592
+ }
593
+
594
+ // Cast
595
+
596
+ if (schema._flags.cast &&
597
+ value !== undefined) {
598
+
599
+ const caster = schema._definition.cast[schema._flags.cast];
600
+ if (caster.from(value)) {
601
+ const casted = caster.to(value, helpers);
602
+ state.mainstay.tracer.value(state, 'cast', value, casted, schema._flags.cast);
603
+ value = casted;
604
+ }
605
+ }
606
+
607
+ // Externals
608
+
609
+ if (schema.$_terms.externals &&
610
+ prefs.externals &&
611
+ prefs._externals !== false) { // Disabled for matching
612
+
613
+ for (const { method } of schema.$_terms.externals) {
614
+ state.mainstay.externals.push({ method, schema, state, label: Errors.label(schema._flags, state, prefs) });
615
+ }
616
+ }
617
+
618
+ // Result
619
+
620
+ const result = { value, errors: errors.length ? errors : null };
621
+
622
+ if (schema._flags.result) {
623
+ result.value = schema._flags.result === 'strip' ? undefined : /* raw */ helpers.original;
624
+ state.mainstay.tracer.value(state, schema._flags.result, value, result.value);
625
+ state.shadow(value, schema._flags.result);
626
+ }
627
+
628
+ // Cache
629
+
630
+ if (schema._cache &&
631
+ prefs.cache !== false &&
632
+ !schema._refs.length) {
633
+
634
+ schema._cache.set(helpers.original, result);
635
+ }
636
+
637
+ // Artifacts
638
+
639
+ if (value !== undefined &&
640
+ !result.errors &&
641
+ schema._flags.artifact !== undefined) {
642
+
643
+ state.mainstay.artifacts = state.mainstay.artifacts || new Map();
644
+ if (!state.mainstay.artifacts.has(schema._flags.artifact)) {
645
+ state.mainstay.artifacts.set(schema._flags.artifact, []);
646
+ }
647
+
648
+ state.mainstay.artifacts.get(schema._flags.artifact).push(state.path);
649
+ }
650
+
651
+ return result;
652
+ };
653
+
654
+
655
+ internals.prefs = function (schema, prefs) {
656
+
657
+ const isDefaultOptions = prefs === Common.defaults;
658
+ if (isDefaultOptions &&
659
+ schema._preferences[Common.symbols.prefs]) {
660
+
661
+ return schema._preferences[Common.symbols.prefs];
662
+ }
663
+
664
+ prefs = Common.preferences(prefs, schema._preferences);
665
+ if (isDefaultOptions) {
666
+ schema._preferences[Common.symbols.prefs] = prefs;
667
+ }
668
+
669
+ return prefs;
670
+ };
671
+
672
+
673
+ internals.default = function (flag, value, errors, helpers) {
674
+
675
+ const { schema, state, prefs } = helpers;
676
+ const source = schema._flags[flag];
677
+ if (prefs.noDefaults ||
678
+ source === undefined) {
679
+
680
+ return value;
681
+ }
682
+
683
+ state.mainstay.tracer.log(schema, state, 'rule', flag, 'full');
684
+
685
+ if (!source) {
686
+ return source;
687
+ }
688
+
689
+ if (typeof source === 'function') {
690
+ const args = source.length ? [Clone(state.ancestors[0]), helpers] : [];
691
+
692
+ try {
693
+ return source(...args);
694
+ }
695
+ catch (err) {
696
+ errors.push(schema.$_createError(`any.${flag}`, null, { error: err }, state, prefs));
697
+ return;
698
+ }
699
+ }
700
+
701
+ if (typeof source !== 'object') {
702
+ return source;
703
+ }
704
+
705
+ if (source[Common.symbols.literal]) {
706
+ return source.literal;
707
+ }
708
+
709
+ if (Common.isResolvable(source)) {
710
+ return source.resolve(value, state, prefs);
711
+ }
712
+
713
+ return Clone(source);
714
+ };
715
+
716
+
717
+ internals.trim = function (value, schema) {
718
+
719
+ if (typeof value !== 'string') {
720
+ return value;
721
+ }
722
+
723
+ const trim = schema.$_getRule('trim');
724
+ if (!trim ||
725
+ !trim.args.enabled) {
726
+
727
+ return value;
728
+ }
729
+
730
+ return value.trim();
731
+ };
732
+
733
+
734
+ internals.ignore = {
735
+ active: false,
736
+ debug: Ignore,
737
+ entry: Ignore,
738
+ filter: Ignore,
739
+ log: Ignore,
740
+ resolve: Ignore,
741
+ value: Ignore
742
+ };
743
+
744
+
745
+ internals.errorsArray = function () {
746
+
747
+ const errors = [];
748
+ errors[Common.symbols.errors] = true;
749
+ return errors;
750
+ };