@flowscripter/dynamic-cli-framework 7.0.4 → 7.0.5

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.
@@ -1 +1 @@
1
- {"version":3,"file":"argumentValueValidation.d.ts","sourceRoot":"","sources":["../../../../src/runtime/values/argumentValueValidation.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAIV,wBAAwB,EACxB,eAAe,EACf,kBAAkB,EACnB,MAAM,yCAAyC,CAAC;AAEjD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,yCAAyC,CAAC;AAC1E,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,yCAAyC,CAAC;AACtE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,yCAAyC,CAAC;AAE7E,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,yCAAyC,CAAC;AAI/E,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,yCAAyC,CAAC;AA6b7E;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CACjC,MAAM,EAAE,MAAM,GAAG,aAAa,EAC9B,KAAK,EAAE,kBAAkB,GAAG,eAAe,GAAG,KAAK,CAAC,eAAe,GAAG,SAAS,CAAC,EAChF,gBAAgB,EAAE,KAAK,CAAC,eAAe,CAAC,GACvC,kBAAkB,GAAG,eAAe,GAAG,KAAK,CAAC,eAAe,CAAC,CAQ/D;AAED;;;;;;GAMG;AACH,wBAAgB,uBAAuB,CACrC,UAAU,EAAE,UAAU,EACtB,KAAK,EAAE,kBAAkB,EACzB,gBAAgB,EAAE,KAAK,CAAC,eAAe,CAAC,GACvC,kBAAkB,CAQpB;AAED;;;;;;;GAOG;AACH,wBAAgB,kCAAkC,CAChD,aAAa,EAAE,aAAa,EAC5B,KAAK,EAAE,wBAAwB,EAC/B,gBAAgB,EAAE,KAAK,CAAC,eAAe,CAAC,GACvC,wBAAwB,CAmD1B;AAED,wBAAgB,wBAAwB,CACtC,eAAe,EAAE,eAAe,EAChC,WAAW,EAAE,OAAO,GACnB,MAAM,CA4DR"}
1
+ {"version":3,"file":"argumentValueValidation.d.ts","sourceRoot":"","sources":["../../../../src/runtime/values/argumentValueValidation.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAIV,wBAAwB,EACxB,eAAe,EACf,kBAAkB,EACnB,MAAM,yCAAyC,CAAC;AAEjD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,yCAAyC,CAAC;AAC1E,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,yCAAyC,CAAC;AACtE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,yCAAyC,CAAC;AAE7E,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,yCAAyC,CAAC;AAI/E,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,yCAAyC,CAAC;AAgb7E;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CACjC,MAAM,EAAE,MAAM,GAAG,aAAa,EAC9B,KAAK,EAAE,kBAAkB,GAAG,eAAe,GAAG,KAAK,CAAC,eAAe,GAAG,SAAS,CAAC,EAChF,gBAAgB,EAAE,KAAK,CAAC,eAAe,CAAC,GACvC,kBAAkB,GAAG,eAAe,GAAG,KAAK,CAAC,eAAe,CAAC,CAQ/D;AAED;;;;;;GAMG;AACH,wBAAgB,uBAAuB,CACrC,UAAU,EAAE,UAAU,EACtB,KAAK,EAAE,kBAAkB,EACzB,gBAAgB,EAAE,KAAK,CAAC,eAAe,CAAC,GACvC,kBAAkB,CAQpB;AAED;;;;;;;GAOG;AACH,wBAAgB,kCAAkC,CAChD,aAAa,EAAE,aAAa,EAC5B,KAAK,EAAE,wBAAwB,EAC/B,gBAAgB,EAAE,KAAK,CAAC,eAAe,CAAC,GACvC,wBAAwB,CA+C1B;AAED,wBAAgB,wBAAwB,CACtC,eAAe,EAAE,eAAe,EAChC,WAAW,EAAE,OAAO,GACnB,MAAM,CA+DR"}
@@ -1,6 +1,36 @@
1
1
  import { ValueTypeName, ComplexValueTypeName } from "@flowscripter/dynamic-cli-framework-api";
2
2
  import { isComplexOption } from "../argument/ArgumentTypeGuards.js";
3
3
  import { InvalidArgumentReason } from "@flowscripter/dynamic-cli-framework-api";
4
+ /**
5
+ * Returns a copy of the provided {@link InvalidArgument} with the provided prefix prepended to its name.
6
+ */
7
+ function withPrefix(invalidArgument, prefix) {
8
+ return { ...invalidArgument, name: `${prefix}${invalidArgument.name ?? ""}` };
9
+ }
10
+ /**
11
+ * Invokes the custom validate function (if any) of the provided {@link Argument} and returns an
12
+ * {@link InvalidArgument} if the custom validation failed.
13
+ *
14
+ * @param argument the {@link Argument} providing the custom validate function.
15
+ * @param name the name to use for the {@link InvalidArgument}.
16
+ * @param value the validated and type-converted value.
17
+ */
18
+ function doCustomValidation(argument, name, value) {
19
+ if (argument.validate === undefined) {
20
+ return undefined;
21
+ }
22
+ const message = argument.validate(value);
23
+ if (message === undefined) {
24
+ return undefined;
25
+ }
26
+ return {
27
+ argument,
28
+ name,
29
+ value: value,
30
+ reason: InvalidArgumentReason.CUSTOM_VALIDATION,
31
+ message,
32
+ };
33
+ }
4
34
  /**
5
35
  * Validates the provided primitive value against the provided {@link Argument}.
6
36
  *
@@ -162,27 +192,10 @@ function validateArrayValue(subCommandArgument, arrayValue) {
162
192
  }
163
193
  if (validationResult.invalidArgument) {
164
194
  // fast fail
165
- if (validationResult.invalidArgument.value !== undefined) {
166
- return {
167
- validValue: convertedArrayValue,
168
- invalidArgument: {
169
- argument: validationResult.invalidArgument.argument,
170
- name: `[${i}]${validationResult.invalidArgument.name ? validationResult.invalidArgument.name : ""}`,
171
- value: validationResult.invalidArgument.value,
172
- reason: validationResult.invalidArgument.reason,
173
- },
174
- };
175
- }
176
- else {
177
- return {
178
- validValue: convertedArrayValue,
179
- invalidArgument: {
180
- argument: validationResult.invalidArgument.argument,
181
- name: `[${i}]${validationResult.invalidArgument.name ? validationResult.invalidArgument.name : ""}`,
182
- reason: validationResult.invalidArgument.reason,
183
- },
184
- };
185
- }
195
+ return {
196
+ validValue: convertedArrayValue,
197
+ invalidArgument: withPrefix(validationResult.invalidArgument, `[${i}]`),
198
+ };
186
199
  }
187
200
  }
188
201
  return {
@@ -193,8 +206,14 @@ function validateObjectValue(argument, objectValue) {
193
206
  const convertedObjectValue = {};
194
207
  for (let i = 0; i < argument.properties.length; i++) {
195
208
  const propertyArg = argument.properties[i];
196
- const propertyValue = objectValue[propertyArg.name];
209
+ let propertyValue = objectValue[propertyArg.name];
210
+ if (propertyValue === undefined && propertyArg.defaultValue !== undefined) {
211
+ propertyValue = propertyArg.defaultValue;
212
+ }
197
213
  if (propertyValue === undefined) {
214
+ if (propertyArg.isOptional) {
215
+ continue;
216
+ }
198
217
  return {
199
218
  validValue: convertedObjectValue,
200
219
  invalidArgument: {
@@ -252,27 +271,18 @@ function validateObjectValue(argument, objectValue) {
252
271
  }
253
272
  // fast fail
254
273
  if (validationResult.invalidArgument !== undefined) {
255
- if (validationResult.invalidArgument.value !== undefined) {
256
- return {
257
- validValue: convertedObjectValue,
258
- invalidArgument: {
259
- argument: validationResult.invalidArgument.argument,
260
- name: `.${propertyArg.name}${validationResult.invalidArgument.name ? validationResult.invalidArgument.name : ""}`,
261
- value: validationResult.invalidArgument.value,
262
- reason: validationResult.invalidArgument.reason,
263
- },
264
- };
265
- }
266
- else {
267
- return {
268
- validValue: convertedObjectValue,
269
- invalidArgument: {
270
- argument: validationResult.invalidArgument.argument,
271
- name: `.${propertyArg.name}${validationResult.invalidArgument.name ? validationResult.invalidArgument.name : ""}`,
272
- reason: validationResult.invalidArgument.reason,
273
- },
274
- };
275
- }
274
+ return {
275
+ validValue: convertedObjectValue,
276
+ invalidArgument: withPrefix(validationResult.invalidArgument, `.${propertyArg.name}`),
277
+ };
278
+ }
279
+ // custom validation runs only once the property (and any nested properties) passed validation
280
+ const customInvalidArgument = doCustomValidation(propertyArg, `.${propertyArg.name}`, validationResult.validValue);
281
+ if (customInvalidArgument !== undefined) {
282
+ return {
283
+ validValue: convertedObjectValue,
284
+ invalidArgument: customInvalidArgument,
285
+ };
276
286
  }
277
287
  }
278
288
  return {
@@ -320,38 +330,13 @@ function doSubCommandArgumentValidation(argument, value, isArray, isOptional, in
320
330
  validationResult = validatePrimitiveValue(argument, value);
321
331
  }
322
332
  if (validationResult.invalidArgument !== undefined) {
323
- if (validationResult.invalidArgument.value !== undefined) {
324
- validationResult.invalidArgument = {
325
- argument: validationResult.invalidArgument.argument,
326
- name: `${argument.name}${validationResult.invalidArgument.name ? validationResult.invalidArgument.name : ""}`,
327
- value: validationResult.invalidArgument.value,
328
- reason: validationResult.invalidArgument.reason,
329
- };
330
- }
331
- else {
332
- validationResult.invalidArgument = {
333
- argument: validationResult.invalidArgument.argument,
334
- name: `${argument.name}${validationResult.invalidArgument.name ? validationResult.invalidArgument.name : ""}`,
335
- reason: validationResult.invalidArgument.reason,
336
- };
337
- }
338
- }
339
- if (validationResult.invalidArgument) {
340
- invalidArguments.push(validationResult.invalidArgument);
333
+ invalidArguments.push(withPrefix(validationResult.invalidArgument, argument.name));
341
334
  return undefined;
342
335
  }
343
- if (argument.validate && validationResult.validValue !== undefined) {
344
- const customError = argument.validate(validationResult.validValue);
345
- if (customError !== undefined) {
346
- invalidArguments.push({
347
- argument,
348
- name: argument.name,
349
- value: validationResult.validValue,
350
- reason: InvalidArgumentReason.CUSTOM_VALIDATION,
351
- message: customError,
352
- });
353
- return undefined;
354
- }
336
+ const customInvalidArgument = doCustomValidation(argument, argument.name, validationResult.validValue);
337
+ if (customInvalidArgument !== undefined) {
338
+ invalidArguments.push(customInvalidArgument);
339
+ return undefined;
355
340
  }
356
341
  return validationResult.validValue;
357
342
  }
@@ -417,18 +402,10 @@ export function validateGlobalCommandArgumentValue(globalCommand, value, invalid
417
402
  }
418
403
  return undefined;
419
404
  }
420
- if (globalCommandArgument.validate && validationResult.validValue !== undefined) {
421
- const customError = globalCommandArgument.validate(validationResult.validValue);
422
- if (customError !== undefined) {
423
- invalidArguments.push({
424
- argument: globalCommandArgument,
425
- name: globalCommand.name,
426
- value: validationResult.validValue,
427
- reason: InvalidArgumentReason.CUSTOM_VALIDATION,
428
- message: customError,
429
- });
430
- return undefined;
431
- }
405
+ const customInvalidArgument = doCustomValidation(globalCommandArgument, globalCommand.name, validationResult.validValue);
406
+ if (customInvalidArgument !== undefined) {
407
+ invalidArguments.push(customInvalidArgument);
408
+ return undefined;
432
409
  }
433
410
  return validationResult.validValue;
434
411
  }
@@ -449,7 +426,10 @@ export function getInvalidArgumentString(invalidArgument, skipArgName) {
449
426
  }
450
427
  let valueString = "";
451
428
  if (invalidArgument.value !== undefined) {
452
- valueString = `'${invalidArgument.value}'`;
429
+ valueString =
430
+ typeof invalidArgument.value === "object"
431
+ ? `'${JSON.stringify(invalidArgument.value)}'`
432
+ : `'${invalidArgument.value}'`;
453
433
  }
454
434
  let argString = "";
455
435
  if (nameString !== "") {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flowscripter/dynamic-cli-framework",
3
- "version": "7.0.4",
3
+ "version": "7.0.5",
4
4
  "description": "A framework for developing CLI applications which supports dynamic discovery and installation of new commands",
5
5
  "keywords": [
6
6
  "bun",
@@ -33,6 +33,42 @@ interface ObjectValueValidationResult extends ValidationResult {
33
33
  validValue?: Values;
34
34
  }
35
35
 
36
+ /**
37
+ * Returns a copy of the provided {@link InvalidArgument} with the provided prefix prepended to its name.
38
+ */
39
+ function withPrefix(invalidArgument: InvalidArgument, prefix: string): InvalidArgument {
40
+ return { ...invalidArgument, name: `${prefix}${invalidArgument.name ?? ""}` };
41
+ }
42
+
43
+ /**
44
+ * Invokes the custom validate function (if any) of the provided {@link Argument} and returns an
45
+ * {@link InvalidArgument} if the custom validation failed.
46
+ *
47
+ * @param argument the {@link Argument} providing the custom validate function.
48
+ * @param name the name to use for the {@link InvalidArgument}.
49
+ * @param value the validated and type-converted value.
50
+ */
51
+ function doCustomValidation(
52
+ argument: Argument | ComplexOption,
53
+ name: string,
54
+ value: SingleValueType | Values | Array<SingleValueType | Values>,
55
+ ): InvalidArgument | undefined {
56
+ if (argument.validate === undefined) {
57
+ return undefined;
58
+ }
59
+ const message = argument.validate(value as ValueType | Values | Array<Values>);
60
+ if (message === undefined) {
61
+ return undefined;
62
+ }
63
+ return {
64
+ argument,
65
+ name,
66
+ value: value as PopulatedValueType | PopulatedValues,
67
+ reason: InvalidArgumentReason.CUSTOM_VALIDATION,
68
+ message,
69
+ };
70
+ }
71
+
36
72
  /**
37
73
  * Validates the provided primitive value against the provided {@link Argument}.
38
74
  *
@@ -217,30 +253,10 @@ function validateArrayValue(
217
253
  }
218
254
  if (validationResult.invalidArgument) {
219
255
  // fast fail
220
- if (validationResult.invalidArgument.value !== undefined) {
221
- return {
222
- validValue: convertedArrayValue,
223
- invalidArgument: {
224
- argument: validationResult.invalidArgument.argument,
225
- name: `[${i}]${
226
- validationResult.invalidArgument.name ? validationResult.invalidArgument.name : ""
227
- }`,
228
- value: validationResult.invalidArgument.value,
229
- reason: validationResult.invalidArgument.reason,
230
- },
231
- };
232
- } else {
233
- return {
234
- validValue: convertedArrayValue,
235
- invalidArgument: {
236
- argument: validationResult.invalidArgument.argument,
237
- name: `[${i}]${
238
- validationResult.invalidArgument.name ? validationResult.invalidArgument.name : ""
239
- }`,
240
- reason: validationResult.invalidArgument.reason,
241
- },
242
- };
243
- }
256
+ return {
257
+ validValue: convertedArrayValue,
258
+ invalidArgument: withPrefix(validationResult.invalidArgument, `[${i}]`),
259
+ };
244
260
  }
245
261
  }
246
262
  return {
@@ -256,9 +272,15 @@ function validateObjectValue(
256
272
 
257
273
  for (let i = 0; i < argument.properties.length; i++) {
258
274
  const propertyArg = argument.properties[i]!;
259
- const propertyValue = objectValue[propertyArg.name];
275
+ let propertyValue = objectValue[propertyArg.name];
260
276
 
277
+ if (propertyValue === undefined && propertyArg.defaultValue !== undefined) {
278
+ propertyValue = propertyArg.defaultValue as PopulatedValueType | PopulatedValues;
279
+ }
261
280
  if (propertyValue === undefined) {
281
+ if (propertyArg.isOptional) {
282
+ continue;
283
+ }
262
284
  return {
263
285
  validValue: convertedObjectValue,
264
286
  invalidArgument: {
@@ -329,30 +351,22 @@ function validateObjectValue(
329
351
  }
330
352
  // fast fail
331
353
  if (validationResult.invalidArgument !== undefined) {
332
- if (validationResult.invalidArgument.value !== undefined) {
333
- return {
334
- validValue: convertedObjectValue,
335
- invalidArgument: {
336
- argument: validationResult.invalidArgument.argument,
337
- name: `.${propertyArg.name}${
338
- validationResult.invalidArgument.name ? validationResult.invalidArgument.name : ""
339
- }`,
340
- value: validationResult.invalidArgument.value,
341
- reason: validationResult.invalidArgument.reason,
342
- },
343
- };
344
- } else {
345
- return {
346
- validValue: convertedObjectValue,
347
- invalidArgument: {
348
- argument: validationResult.invalidArgument.argument,
349
- name: `.${propertyArg.name}${
350
- validationResult.invalidArgument.name ? validationResult.invalidArgument.name : ""
351
- }`,
352
- reason: validationResult.invalidArgument.reason,
353
- },
354
- };
355
- }
354
+ return {
355
+ validValue: convertedObjectValue,
356
+ invalidArgument: withPrefix(validationResult.invalidArgument, `.${propertyArg.name}`),
357
+ };
358
+ }
359
+ // custom validation runs only once the property (and any nested properties) passed validation
360
+ const customInvalidArgument = doCustomValidation(
361
+ propertyArg,
362
+ `.${propertyArg.name}`,
363
+ validationResult.validValue!,
364
+ );
365
+ if (customInvalidArgument !== undefined) {
366
+ return {
367
+ validValue: convertedObjectValue,
368
+ invalidArgument: customInvalidArgument,
369
+ };
356
370
  }
357
371
  }
358
372
  return {
@@ -407,44 +421,17 @@ function doSubCommandArgumentValidation(
407
421
  }
408
422
 
409
423
  if (validationResult.invalidArgument !== undefined) {
410
- if (validationResult.invalidArgument.value !== undefined) {
411
- validationResult.invalidArgument = {
412
- argument: validationResult.invalidArgument.argument,
413
- name: `${argument.name}${
414
- validationResult.invalidArgument.name ? validationResult.invalidArgument.name : ""
415
- }`,
416
- value: validationResult.invalidArgument.value,
417
- reason: validationResult.invalidArgument.reason,
418
- };
419
- } else {
420
- validationResult.invalidArgument = {
421
- argument: validationResult.invalidArgument.argument,
422
- name: `${argument.name}${
423
- validationResult.invalidArgument.name ? validationResult.invalidArgument.name : ""
424
- }`,
425
- reason: validationResult.invalidArgument.reason,
426
- };
427
- }
428
- }
429
-
430
- if (validationResult.invalidArgument) {
431
- invalidArguments.push(validationResult.invalidArgument);
424
+ invalidArguments.push(withPrefix(validationResult.invalidArgument, argument.name));
432
425
  return undefined;
433
426
  }
434
- if (argument.validate && validationResult.validValue !== undefined) {
435
- const customError = argument.validate(
436
- validationResult.validValue as ValueType | Values | Array<Values>,
437
- );
438
- if (customError !== undefined) {
439
- invalidArguments.push({
440
- argument,
441
- name: argument.name,
442
- value: validationResult.validValue as PopulatedValueType,
443
- reason: InvalidArgumentReason.CUSTOM_VALIDATION,
444
- message: customError,
445
- });
446
- return undefined;
447
- }
427
+ const customInvalidArgument = doCustomValidation(
428
+ argument,
429
+ argument.name,
430
+ validationResult.validValue!,
431
+ );
432
+ if (customInvalidArgument !== undefined) {
433
+ invalidArguments.push(customInvalidArgument);
434
+ return undefined;
448
435
  }
449
436
  return validationResult.validValue as PopulatedValueType | PopulatedValues | undefined;
450
437
  }
@@ -540,18 +527,14 @@ export function validateGlobalCommandArgumentValue(
540
527
  return undefined;
541
528
  }
542
529
 
543
- if (globalCommandArgument.validate && validationResult.validValue !== undefined) {
544
- const customError = globalCommandArgument.validate(validationResult.validValue as ValueType);
545
- if (customError !== undefined) {
546
- invalidArguments.push({
547
- argument: globalCommandArgument,
548
- name: globalCommand.name,
549
- value: validationResult.validValue,
550
- reason: InvalidArgumentReason.CUSTOM_VALIDATION,
551
- message: customError,
552
- });
553
- return undefined;
554
- }
530
+ const customInvalidArgument = doCustomValidation(
531
+ globalCommandArgument,
532
+ globalCommand.name,
533
+ validationResult.validValue!,
534
+ );
535
+ if (customInvalidArgument !== undefined) {
536
+ invalidArguments.push(customInvalidArgument);
537
+ return undefined;
555
538
  }
556
539
  return validationResult.validValue as PopulatedSingleValueType;
557
540
  }
@@ -577,7 +560,10 @@ export function getInvalidArgumentString(
577
560
  }
578
561
  let valueString = "";
579
562
  if (invalidArgument.value !== undefined) {
580
- valueString = `'${invalidArgument.value}'`;
563
+ valueString =
564
+ typeof invalidArgument.value === "object"
565
+ ? `'${JSON.stringify(invalidArgument.value)}'`
566
+ : `'${invalidArgument.value}'`;
581
567
  }
582
568
  let argString = "";
583
569
  if (nameString !== "") {