generator icon indicating copy to clipboard operation
generator copied to clipboard

[BUG] validateTemplateConfig crashes with TypeError when template defines parameters: {}

Open devxdh opened this issue 4 weeks ago • 1 comments

Describe the bug.

When a template defines an empty parameters object (parameters: {} in .ageneratorrc or package.json), passing any unexpected or misspelled template parameter (such as --param foo=bar or templateParams: { foo: 'bar' }) causes validateTemplateConfig to crash with an unhandled TypeError:

TypeError: Cannot read properties of undefined (reading '1')
    at getParamSuggestion (apps/generator/lib/templates/config/validator.js:95:86)
    at apps/generator/lib/templates/config/validator.js:113:28
    at Array.forEach (<anonymous>)
    at isProvidedParameterSupported (apps/generator/lib/templates/config/validator.js:112:15)
    at module.exports.validateTemplateConfig (apps/generator/lib/templates/config/validator.js:49:3)

Instead of showing the clean error message (This template doesn't have any params.), the process crashes with a runtime exception.

What causes this:

In apps/generator/lib/templates/config/validator.js#isProvidedParameterSupported:

  1. If configParams is {} (an empty object), !configParams evaluates to false because {} is truthy in JavaScript.
  2. The check on line 108 is bypassed, and line 113 calls getParamSuggestion(wp, configParams) to look for a typo suggestion.
  3. Inside getParamSuggestion:
    function getParamSuggestion(wrongParam, configParams) {
      const sortInt = (a, b) => a[0] - b[0];
      return Object.keys(configParams).map(param => [levenshtein(wrongParam, param), param]).sort(sortInt)[0][1];
    }
    
  4. Since configParams is empty, Object.keys(configParams) returns [].
  5. Accessing [0] returns undefined.
  6. Accessing undefined[1] throws TypeError: Cannot read properties of undefined (reading '1').

(Sibling bug in the same file): In validateConditionalGeneration (line 192), typeof validation !== 'object' allows validation: null (since typeof null === 'object'), passing null to ajv.compile(null) and crashing with TypeError: Cannot read properties of null (reading '$id').

Expected behavior

  1. When a template does not define any parameters (configParams is missing or {}), the generator should throw: Error: This template doesn't have any params.
  2. When validation in conditionalGeneration is null, it should throw: Error: Invalid 'validation' object for <fileName>: null

Screenshots

No Screeshots

How to Reproduce

Option 1: 1-line command in terminal from repo root:

node -e "const { validateTemplateConfig } = require('./apps/generator/lib/templates/config/validator'); validateTemplateConfig({ parameters: {} }, { foo: 'bar' });"

Option 2: Using the Generator class in a script:

const Generator = require('./apps/generator/lib/generator');
const path = require('path');
const fs = require('fs');
const os = require('os');

const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gen-'));
const tmplDir = fs.mkdtempSync(path.join(os.tmpdir(), 'tmpl-'));

fs.mkdirSync(path.join(tmplDir, 'template'), { recursive: true });
fs.writeFileSync(path.join(tmplDir, 'package.json'), JSON.stringify({ name: 'my-template', version: '1.0.0' }));
fs.writeFileSync(path.join(tmplDir, '.ageneratorrc'), 'apiVersion: v3\nparameters: {}\n');

(async () => {
  try {
    const gen = new Generator(tmplDir, tmpDir, {
      forceWrite: true,
      templateParams: { unexpectedParam: 'hello' }
    });
    await gen.generateFromFile(path.resolve('apps/generator/test/docs/dummy.yml'));
  } catch (err) {
    console.error('Crashed with:', err.message);
  } finally {
    fs.rmSync(tmpDir, { recursive: true, force: true });
    fs.rmSync(tmplDir, { recursive: true, force: true });
  }
})();

Observed Output:

TypeError: Cannot read properties of undefined (reading '1')
    at getParamSuggestion (apps/generator/lib/templates/config/validator.js:95:86)
    at apps/generator/lib/templates/config/validator.js:113:28
    at Array.forEach (<anonymous>)
    at isProvidedParameterSupported (apps/generator/lib/templates/config/validator.js:112:15)
    at module.exports.validateTemplateConfig (apps/generator/lib/templates/config/validator.js:49:3)

🖥️ Device Information [optional]

  • Operating System (OS): Fedora Linux Workstation 44
  • Browser: Chrome
  • Browser Version: 154

👀 Have you checked for similar open issues?

  • [x] I checked and didn't find similar issue

🏢 Have you read the Contributing Guidelines?

Are you willing to work on this issue ?

Yes I am willing to submit a PR!

devxdh avatar Nov 06 '26 16:11 devxdh

The regression tests fit in apps/generator/test/templateConfigValidator.test.js. It already covers missing parameter configuration, typo suggestions, and non-object conditional validation, but those cases use omitted parameters and a string validation, rather than {} and null. [1] [2]

For your PR, I’d add these cases:

  • parameters: {} with { foo: 'bar' } throws the exact no-params message.
  • parameters: {} with no supplied parameters still returns true. Preserve the existing early return when there are no unsupported parameters. [3]
  • conditionalGeneration: { 'file.js': { subject: 'info.title', validation: null } } throws Invalid 'validation' object for file.js: null, rather than reaching AJV. [4]

You can run that suite from apps/generator with:

npm run test:unit -- --runTestsByPath test/templateConfigValidator.test.js

The package’s test:unit script invokes Jest. I haven’t run the tests here. [5]

To reply, just mention @dosu.


Docs are dead. Just use Dosu.

Leave Feedback Ask Dosu about generator Share Dosu with your team

dosubot[bot] avatar Nov 06 '26 16:11 dosubot[bot]