@jakubmazanec/args 1.0.2 → 1.0.3-next.439463c

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.
@@ -5,7 +5,7 @@ import { createCustomError } from '@jakubmazanec/error';
5
5
  INVALID_COMMAND: 'Command "{0}" has invalid format. Only words consisting of letters, numbers or dashes separated by single space are allowed.',
6
6
  MISSING_PARAMETER: 'Parameter "{0}" is required but its value is undefined.',
7
7
  INVALID_PARAMETER_DEFAULT_AND_CHOICES: 'Parameter "{0}" has invalid default value "{1}". Default value must be one of the `choices` elements.',
8
- INVALID_PARAMETER_ORDER: 'Optional parameter "{0}" found before required parameter "{1}". Required parameter must be first.',
8
+ INVALID_PARAMETER_ORDER: 'Parameter "{0}" has invalid order. All parameters before must have a default value or be required too',
9
9
  MISSING_OPTION: 'Option "{0}" is required but its value is undefined.',
10
10
  INVALID_OPTION_DEFAULT_AND_CHOICES: 'Option "{0}" has invalid default value "{1}". Default value must be one of the `choices` elements.',
11
11
  INVALID_OPTION_NAME: 'Option "{0}" has invalid name. Only names consisting of letters, numbers or dashes are allowed.',
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/ValidationError.ts"],"sourcesContent":["import {createCustomError} from '@jakubmazanec/error';\n\n/**\n * A subclass of `Error` that indicates a validation issue.\n */\nexport const ValidationError = createCustomError('ValidationError', {\n INVALID_COMMAND:\n 'Command \"{0}\" has invalid format. Only words consisting of letters, numbers or dashes separated by single space are allowed.',\n MISSING_PARAMETER: 'Parameter \"{0}\" is required but its value is undefined.',\n INVALID_PARAMETER_DEFAULT_AND_CHOICES:\n 'Parameter \"{0}\" has invalid default value \"{1}\". Default value must be one of the `choices` elements.',\n INVALID_PARAMETER_ORDER:\n 'Optional parameter \"{0}\" found before required parameter \"{1}\". Required parameter must be first.',\n MISSING_OPTION: 'Option \"{0}\" is required but its value is undefined.',\n INVALID_OPTION_DEFAULT_AND_CHOICES:\n 'Option \"{0}\" has invalid default value \"{1}\". Default value must be one of the `choices` elements.',\n INVALID_OPTION_NAME:\n 'Option \"{0}\" has invalid name. Only names consisting of letters, numbers or dashes are allowed.',\n REPEATED_OPTION_NAME:\n 'Option \"{0}\" has invalid short name \"{1}\". Short option names must be unique.',\n INVALID_OPTION_ARITY: 'Option \"{0}\" has invalid number of values. Found \"{1}\", require \"{2}\".',\n});\n"],"names":["createCustomError","ValidationError","INVALID_COMMAND","MISSING_PARAMETER","INVALID_PARAMETER_DEFAULT_AND_CHOICES","INVALID_PARAMETER_ORDER","MISSING_OPTION","INVALID_OPTION_DEFAULT_AND_CHOICES","INVALID_OPTION_NAME","REPEATED_OPTION_NAME","INVALID_OPTION_ARITY"],"mappings":"AAAA,SAAQA,iBAAiB,QAAO,sBAAsB;AAEtD;;CAEC,GACD,OAAO,MAAMC,kBAAkBD,kBAAkB,mBAAmB;IAClEE,iBACE;IACFC,mBAAmB;IACnBC,uCACE;IACFC,yBACE;IACFC,gBAAgB;IAChBC,oCACE;IACFC,qBACE;IACFC,sBACE;IACFC,sBAAsB;AACxB,GAAG"}
1
+ {"version":3,"sources":["../src/ValidationError.ts"],"sourcesContent":["import {createCustomError} from '@jakubmazanec/error';\n\n/**\n * A subclass of `Error` that indicates a validation issue.\n */\nexport const ValidationError = createCustomError('ValidationError', {\n INVALID_COMMAND:\n 'Command \"{0}\" has invalid format. Only words consisting of letters, numbers or dashes separated by single space are allowed.',\n MISSING_PARAMETER: 'Parameter \"{0}\" is required but its value is undefined.',\n INVALID_PARAMETER_DEFAULT_AND_CHOICES:\n 'Parameter \"{0}\" has invalid default value \"{1}\". Default value must be one of the `choices` elements.',\n INVALID_PARAMETER_ORDER:\n 'Parameter \"{0}\" has invalid order. All parameters before must have a default value or be required too',\n MISSING_OPTION: 'Option \"{0}\" is required but its value is undefined.',\n INVALID_OPTION_DEFAULT_AND_CHOICES:\n 'Option \"{0}\" has invalid default value \"{1}\". Default value must be one of the `choices` elements.',\n INVALID_OPTION_NAME:\n 'Option \"{0}\" has invalid name. Only names consisting of letters, numbers or dashes are allowed.',\n REPEATED_OPTION_NAME:\n 'Option \"{0}\" has invalid short name \"{1}\". Short option names must be unique.',\n INVALID_OPTION_ARITY: 'Option \"{0}\" has invalid number of values. Found \"{1}\", require \"{2}\".',\n});\n"],"names":["createCustomError","ValidationError","INVALID_COMMAND","MISSING_PARAMETER","INVALID_PARAMETER_DEFAULT_AND_CHOICES","INVALID_PARAMETER_ORDER","MISSING_OPTION","INVALID_OPTION_DEFAULT_AND_CHOICES","INVALID_OPTION_NAME","REPEATED_OPTION_NAME","INVALID_OPTION_ARITY"],"mappings":"AAAA,SAAQA,iBAAiB,QAAO,sBAAsB;AAEtD;;CAEC,GACD,OAAO,MAAMC,kBAAkBD,kBAAkB,mBAAmB;IAClEE,iBACE;IACFC,mBAAmB;IACnBC,uCACE;IACFC,yBACE;IACFC,gBAAgB;IAChBC,oCACE;IACFC,qBACE;IACFC,sBACE;IACFC,sBAAsB;AACxB,GAAG"}
@@ -29,10 +29,9 @@ import { isLongOption } from './isLongOption.js';
29
29
  }));
30
30
  }
31
31
  // if parameter is required, all parameters before must be required too
32
- if (i >= 1 && parameterConfig.required === true && parametersConfig[i - 1]?.required !== true) {
32
+ if (i >= 1 && parameterConfig.required === true && !parametersConfig.slice(0, i).every((previousParameterConfig)=>previousParameterConfig.required === true || typeof previousParameterConfig.defaultValue !== 'undefined')) {
33
33
  this.addError(new ValidationError('INVALID_PARAMETER_ORDER', {
34
34
  messageParameters: [
35
- parametersConfig[i - 1]?.label ?? i - 1,
36
35
  parameterConfig.label ?? i
37
36
  ]
38
37
  }));
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/internals/Checker.ts"],"sourcesContent":["import {type CommandsConfig} from '../CommandsConfig.js';\nimport {type OptionsConfig} from '../OptionsConfig.js';\nimport {type ParametersConfig} from '../ParametersConfig.js';\nimport {ValidationError} from '../ValidationError.js';\nimport {isCommand} from './isCommand.js';\nimport {isLongOption} from './isLongOption.js';\n\n/**\n * `Checker` is used for handling various checks before, during, and after parsing of the arguments.\n */\nexport class Checker {\n errors: Error[] = [];\n\n checkCommandsConfig(commandsConfig: CommandsConfig | []) {\n for (let commandConfig of commandsConfig) {\n if (!isCommand(commandConfig)) {\n this.addError(new ValidationError('INVALID_COMMAND', {messageParameters: [commandConfig]}));\n }\n }\n }\n\n checkParametersConfig(parametersConfig: ParametersConfig | []) {\n for (let [i, parameterConfig] of parametersConfig.entries()) {\n // types of choices must be correct\n if (\n parameterConfig.type !== 'boolean' &&\n parameterConfig.choices && // default values and chocies do not contradict each other\n typeof parameterConfig.defaultValue !== 'undefined' &&\n !parameterConfig.choices.includes(parameterConfig.defaultValue as never)\n ) {\n this.addError(\n new ValidationError('INVALID_PARAMETER_DEFAULT_AND_CHOICES', {\n messageParameters: [parameterConfig.label ?? i, parameterConfig.defaultValue],\n }),\n );\n }\n\n // if parameter is required, all parameters before must be required too\n if (\n i >= 1 &&\n parameterConfig.required === true &&\n parametersConfig[i - 1]?.required !== true\n ) {\n this.addError(\n new ValidationError('INVALID_PARAMETER_ORDER', {\n messageParameters: [\n parametersConfig[i - 1]?.label ?? i - 1,\n parameterConfig.label ?? i,\n ],\n }),\n );\n }\n }\n }\n\n checkOptionsConfig(optionsConfig: OptionsConfig) {\n let usedShortOptionNames: Set<string> = new Set([]);\n\n for (let [optionName, optionConfig] of Object.entries(optionsConfig)) {\n // option names must be in correct format\n if (!isLongOption(`--${optionName}`)) {\n this.addError(\n new ValidationError('INVALID_OPTION_NAME', {\n messageParameters: [optionName],\n }),\n );\n }\n\n // option short names must be unique\n if (optionConfig.shortName && usedShortOptionNames.has(optionConfig.shortName)) {\n this.addError(\n new ValidationError('REPEATED_OPTION_NAME', {\n messageParameters: [optionName, optionConfig.shortName],\n }),\n );\n } else if (optionConfig.shortName) {\n usedShortOptionNames.add(optionConfig.shortName);\n }\n\n // default values and chocies do not contradict each other\n if (\n (optionConfig.type === 'string' ||\n (optionConfig.type === 'number' && !optionConfig.count)) &&\n optionConfig.choices &&\n typeof optionConfig.defaultValue !== 'undefined' &&\n !optionConfig.choices.includes(optionConfig.defaultValue as never)\n ) {\n this.addError(\n new ValidationError('INVALID_OPTION_DEFAULT_AND_CHOICES', {\n messageParameters: [optionName, optionConfig.defaultValue],\n }),\n );\n }\n }\n }\n\n checkParsedParameters(parametersConfig: ParametersConfig | [], parsedParameters: unknown[]) {\n for (let [i, parameterConfig] of parametersConfig.entries()) {\n let parsedParameter = parsedParameters[i];\n\n // required parameters must have value\n if (parameterConfig.required === true && typeof parsedParameter === 'undefined') {\n this.addError(\n new ValidationError('MISSING_PARAMETER', {\n messageParameters: [parameterConfig.label ?? i],\n }),\n );\n }\n }\n }\n\n checkParsedOptions(optionsConfig: OptionsConfig, parsedOptions: Record<string, unknown>) {\n for (let [optionName, optionConfig] of Object.entries(optionsConfig)) {\n let parsedOption = parsedOptions[optionName];\n\n // check required\n if (optionConfig.required && typeof parsedOption === 'undefined') {\n this.addError(\n new ValidationError('MISSING_OPTION', {\n messageParameters: [optionName],\n }),\n );\n }\n\n // check arity\n if (\n typeof parsedOption !== 'undefined' &&\n ((optionConfig.type === 'string' && optionConfig.multiple) ||\n (optionConfig.type === 'number' && optionConfig.multiple)) &&\n typeof optionConfig.arity === 'number' &&\n Array.isArray(parsedOption) &&\n (parsedOption as unknown[]).length > 0 &&\n (parsedOption as unknown[]).length !== optionConfig.arity\n ) {\n this.addError(\n new ValidationError('INVALID_OPTION_ARITY', {\n messageParameters: [optionName, (parsedOption as unknown[]).length, optionConfig.arity],\n }),\n );\n }\n }\n }\n\n addError(error: Error) {\n this.errors.push(error);\n }\n}\n"],"names":["ValidationError","isCommand","isLongOption","Checker","errors","checkCommandsConfig","commandsConfig","commandConfig","addError","messageParameters","checkParametersConfig","parametersConfig","i","parameterConfig","entries","type","choices","defaultValue","includes","label","required","checkOptionsConfig","optionsConfig","usedShortOptionNames","Set","optionName","optionConfig","Object","shortName","has","add","count","checkParsedParameters","parsedParameters","parsedParameter","checkParsedOptions","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ACvCS,mBAAmB;wBAACI,gBAAgBM,KAAK,IAAIP;qBAAE;gBACjD;YAEJ;QACF;IACF;IAEAuB,mBAAmBb,aAA4B,EAAEc,aAAsC,EAAE;QACvF,KAAK,IAAI,CAACX,YAAYC,aAAa,IAAIC,OAAOb,OAAO,CAACQ,eAAgB;YACpE,IAAIe,eAAeD,aAAa,CAACX,WAAW;YAE5C,iBAAiB;YACjB,IAAIC,aAAaN,QAAQ,IAAI,OAAOiB,iBAAiB,aAAa;gBAChE,IAAI,CAAC7B,QAAQ,CACX,IAAIR,gBAAgB,kBAAkB;oBACpCS,mBAAmB;wBAACgB;qBAAW;gBACjC;YAEJ;YAEA,cAAc;YACd,IACE,OAAOY,iBAAiB,eACvB,CAAA,AAACX,aAAaX,IAAI,KAAK,YAAYW,aAAaY,QAAQ,IACtDZ,aAAaX,IAAI,KAAK,YAAYW,aAAaY,QAAQ,KAC1D,OAAOZ,aAAaa,KAAK,KAAK,YAC9BC,MAAMC,OAAO,CAACJ,iBACd,AAACA,aAA2BK,MAAM,GAAG,KACrC,AAACL,aAA2BK,MAAM,KAAKhB,aAAaa,KAAK,EACzD;gBACA,IAAI,CAAC/B,QAAQ,CACX,IAAIR,gBAAgB,wBAAwB;oBAC1CS,mBAAmB;wBAACgB;wBAAaY,aAA2BK,MAAM;wBAAEhB,aAAaa,KAAK;qBAAC;gBACzF;YAEJ;QACF;IACF;IAEA/B,SAASmC,KAAY,EAAE;QACrB,IAAI,CAACvC,MAAM,CAACwC,IAAI,CAACD;IACnB;AACF"}
1
+ {"version":3,"sources":["../../src/internals/Checker.ts"],"sourcesContent":["import {type CommandsConfig} from '../CommandsConfig.js';\nimport {type OptionsConfig} from '../OptionsConfig.js';\nimport {type ParametersConfig} from '../ParametersConfig.js';\nimport {ValidationError} from '../ValidationError.js';\nimport {isCommand} from './isCommand.js';\nimport {isLongOption} from './isLongOption.js';\n\n/**\n * `Checker` is used for handling various checks before, during, and after parsing of the arguments.\n */\nexport class Checker {\n errors: Error[] = [];\n\n checkCommandsConfig(commandsConfig: CommandsConfig | []) {\n for (let commandConfig of commandsConfig) {\n if (!isCommand(commandConfig)) {\n this.addError(new ValidationError('INVALID_COMMAND', {messageParameters: [commandConfig]}));\n }\n }\n }\n\n checkParametersConfig(parametersConfig: ParametersConfig | []) {\n for (let [i, parameterConfig] of parametersConfig.entries()) {\n // types of choices must be correct\n if (\n parameterConfig.type !== 'boolean' &&\n parameterConfig.choices && // default values and chocies do not contradict each other\n typeof parameterConfig.defaultValue !== 'undefined' &&\n !parameterConfig.choices.includes(parameterConfig.defaultValue as never)\n ) {\n this.addError(\n new ValidationError('INVALID_PARAMETER_DEFAULT_AND_CHOICES', {\n messageParameters: [parameterConfig.label ?? i, parameterConfig.defaultValue],\n }),\n );\n }\n\n // if parameter is required, all parameters before must be required too\n if (\n i >= 1 &&\n parameterConfig.required === true &&\n !parametersConfig\n .slice(0, i)\n .every(\n (previousParameterConfig) =>\n previousParameterConfig.required === true ||\n typeof previousParameterConfig.defaultValue !== 'undefined',\n )\n ) {\n this.addError(\n new ValidationError('INVALID_PARAMETER_ORDER', {\n messageParameters: [parameterConfig.label ?? i],\n }),\n );\n }\n }\n }\n\n checkOptionsConfig(optionsConfig: OptionsConfig) {\n let usedShortOptionNames: Set<string> = new Set([]);\n\n for (let [optionName, optionConfig] of Object.entries(optionsConfig)) {\n // option names must be in correct format\n if (!isLongOption(`--${optionName}`)) {\n this.addError(\n new ValidationError('INVALID_OPTION_NAME', {\n messageParameters: [optionName],\n }),\n );\n }\n\n // option short names must be unique\n if (optionConfig.shortName && usedShortOptionNames.has(optionConfig.shortName)) {\n this.addError(\n new ValidationError('REPEATED_OPTION_NAME', {\n messageParameters: [optionName, optionConfig.shortName],\n }),\n );\n } else if (optionConfig.shortName) {\n usedShortOptionNames.add(optionConfig.shortName);\n }\n\n // default values and chocies do not contradict each other\n if (\n (optionConfig.type === 'string' ||\n (optionConfig.type === 'number' && !optionConfig.count)) &&\n optionConfig.choices &&\n typeof optionConfig.defaultValue !== 'undefined' &&\n !optionConfig.choices.includes(optionConfig.defaultValue as never)\n ) {\n this.addError(\n new ValidationError('INVALID_OPTION_DEFAULT_AND_CHOICES', {\n messageParameters: [optionName, optionConfig.defaultValue],\n }),\n );\n }\n }\n }\n\n checkParsedParameters(parametersConfig: ParametersConfig | [], parsedParameters: unknown[]) {\n for (let [i, parameterConfig] of parametersConfig.entries()) {\n let parsedParameter = parsedParameters[i];\n\n // required parameters must have value\n if (parameterConfig.required === true && typeof parsedParameter === 'undefined') {\n this.addError(\n new ValidationError('MISSING_PARAMETER', {\n messageParameters: [parameterConfig.label ?? i],\n }),\n );\n }\n }\n }\n\n checkParsedOptions(optionsConfig: OptionsConfig, parsedOptions: Record<string, unknown>) {\n for (let [optionName, optionConfig] of Object.entries(optionsConfig)) {\n let parsedOption = parsedOptions[optionName];\n\n // check required\n if (optionConfig.required && typeof parsedOption === 'undefined') {\n this.addError(\n new ValidationError('MISSING_OPTION', {\n messageParameters: [optionName],\n }),\n );\n }\n\n // check arity\n if (\n typeof parsedOption !== 'undefined' &&\n ((optionConfig.type === 'string' && optionConfig.multiple) ||\n (optionConfig.type === 'number' && optionConfig.multiple)) &&\n typeof optionConfig.arity === 'number' &&\n Array.isArray(parsedOption) &&\n (parsedOption as unknown[]).length > 0 &&\n (parsedOption as unknown[]).length !== optionConfig.arity\n ) {\n this.addError(\n new ValidationError('INVALID_OPTION_ARITY', {\n messageParameters: [optionName, (parsedOption as unknown[]).length, optionConfig.arity],\n }),\n );\n }\n }\n }\n\n addError(error: Error) {\n this.errors.push(error);\n }\n}\n"],"names":["ValidationError","isCommand","isLongOption","Checker","errors","checkCommandsConfig","commandsConfig","commandConfig","addError","messageParameters","checkParametersConfig","parametersConfig","i","parameterConfig","entries","type","choices","defaultValue","includes","label","required","slice","every","previousParameterConfig","checkOptionsConfig","optionsConfig","usedShortOptionNames","Set","optionName","optionConfig","Object","shortName","has","add","count","checkParsedParameters","parsedParameters","parsedParameter","checkParsedOptions","parsedOptions","parsedOption","multiple","arity","Array","isArray","length","error","push"],"mappings":"AAGA,SAAQA,eAAe,QAAO,wBAAwB;AACtD,SAAQC,SAAS,QAAO,iBAAiB;AACzC,SAAQC,YAAY,QAAO,oBAAoB;AAE/C;;CAEC,GACD,OAAO,MAAMC;IACXC,SAAkB,EAAE,CAAC;IAErBC,oBAAoBC,cAAmC,EAAE;QACvD,KAAK,IAAIC,iBAAiBD,eAAgB;YACxC,IAAI,CAACL,UAAUM,gBAAgB;gBAC7B,IAAI,CAACC,QAAQ,CAAC,IAAIR,gBAAgB,mBAAmB;oBAACS,mBAAmB;wBAACF;qBAAc;gBAAA;YAC1F;QACF;IACF;IAEAG,sBAAsBC,gBAAuC,EAAE;QAC7D,KAAK,IAAI,CAACC,GAAGC,gBAAgB,IAAIF,iBAAiBG,OAAO,GAAI;YAC3D,mCAAmC;YACnC,IACED,gBAAgBE,IAAI,KAAK,aACzBF,gBAAgBG,OAAO,IAAI,0DAA0D;YACrF,OAAOH,gBAAgBI,YAAY,KAAK,eACxC,CAACJ,gBAAgBG,OAAO,CAACE,QAAQ,CAACL,gBAAgBI,YAAY,GAC9D;gBACA,IAAI,CAACT,QAAQ,CACX,IAAIR,gBAAgB,yCAAyC;oBAC3DS,mBAAmB;wBAACI,gBAAgBM,KAAK,IAAIP;wBAAGC,gBAAgBI,YAAY;qBAAC;gBAC/E;YAEJ;YAEA,uEAAuE;YACvE,IACEL,KAAK,KACLC,gBAAgBO,QAAQ,KAAK,QAC7B,CAACT,iBACEU,KAAK,CAAC,GAAGT,GACTU,KAAK,CACJ,CAACC,0BACCA,wBAAwBH,QAAQ,KAAK,QACrC,OAAOG,wBAAwBN,YAAY,KAAK,cAEtD;gBACA,IAAI,CAACT,QAAQ,CACX,IAAIR,gBAAgB,2BAA2B;oBAC7CS,mBAAmB;wBAACI,gBAAgBM,KAAK,IAAIP;qBAAE;gBACjD;YAEJ;QACF;IACF;IAEAY,mBAAmBC,aAA4B,EAAE;QAC/C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X,IAAIR,gBAAgB,qBAAqB;oBACvCS,mBAAmB;wBAACI,gBAAgBM,KAAK,IAAIP;qBAAE;gBACjD;YAEJ;QACF;IACF;IAEA0B,mBAAmBb,aAA4B,EAAEc,aAAsC,EAAE;QACvF,KAAK,IAAI,CAACX,YAAYC,aAAa,IAAIC,OAAOhB,OAAO,CAACW,eAAgB;YACpE,IAAIe,eAAeD,aAAa,CAACX,WAAW;YAE5C,iBAAiB;YACjB,IAAIC,aAAaT,QAAQ,IAAI,OAAOoB,iBAAiB,aAAa;gBAChE,IAAI,CAAChC,QAAQ,CACX,IAAIR,gBAAgB,kBAAkB;oBACpCS,mBAAmB;wBAACmB;qBAAW;gBACjC;YAEJ;YAEA,cAAc;YACd,IACE,OAAOY,iBAAiB,eACvB,CAAA,AAACX,aAAad,IAAI,KAAK,YAAYc,aAAaY,QAAQ,IACtDZ,aAAad,IAAI,KAAK,YAAYc,aAAaY,QAAQ,KAC1D,OAAOZ,aAAaa,KAAK,KAAK,YAC9BC,MAAMC,OAAO,CAACJ,iBACd,AAACA,aAA2BK,MAAM,GAAG,KACrC,AAACL,aAA2BK,MAAM,KAAKhB,aAAaa,KAAK,EACzD;gBACA,IAAI,CAAClC,QAAQ,CACX,IAAIR,gBAAgB,wBAAwB;oBAC1CS,mBAAmB;wBAACmB;wBAAaY,aAA2BK,MAAM;wBAAEhB,aAAaa,KAAK;qBAAC;gBACzF;YAEJ;QACF;IACF;IAEAlC,SAASsC,KAAY,EAAE;QACrB,IAAI,CAAC1C,MAAM,CAAC2C,IAAI,CAACD;IACnB;AACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jakubmazanec/args",
3
- "version": "1.0.2",
3
+ "version": "1.0.3-next.439463c",
4
4
  "description": "TypeScript-first library for parsing command line arguments.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -10,7 +10,7 @@ export const ValidationError = createCustomError('ValidationError', {
10
10
  INVALID_PARAMETER_DEFAULT_AND_CHOICES:
11
11
  'Parameter "{0}" has invalid default value "{1}". Default value must be one of the `choices` elements.',
12
12
  INVALID_PARAMETER_ORDER:
13
- 'Optional parameter "{0}" found before required parameter "{1}". Required parameter must be first.',
13
+ 'Parameter "{0}" has invalid order. All parameters before must have a default value or be required too',
14
14
  MISSING_OPTION: 'Option "{0}" is required but its value is undefined.',
15
15
  INVALID_OPTION_DEFAULT_AND_CHOICES:
16
16
  'Option "{0}" has invalid default value "{1}". Default value must be one of the `choices` elements.',
@@ -39,14 +39,17 @@ export class Checker {
39
39
  if (
40
40
  i >= 1 &&
41
41
  parameterConfig.required === true &&
42
- parametersConfig[i - 1]?.required !== true
42
+ !parametersConfig
43
+ .slice(0, i)
44
+ .every(
45
+ (previousParameterConfig) =>
46
+ previousParameterConfig.required === true ||
47
+ typeof previousParameterConfig.defaultValue !== 'undefined',
48
+ )
43
49
  ) {
44
50
  this.addError(
45
51
  new ValidationError('INVALID_PARAMETER_ORDER', {
46
- messageParameters: [
47
- parametersConfig[i - 1]?.label ?? i - 1,
48
- parameterConfig.label ?? i,
49
- ],
52
+ messageParameters: [parameterConfig.label ?? i],
50
53
  }),
51
54
  );
52
55
  }