davila7/claude-code-templates · error · Error

validate() must be implemented by subclass

Error message

validate() must be implemented by subclass

What it means

Thrown by BaseValidator.validate() in cli-tool/src/validation/BaseValidator.js. BaseValidator is an abstract base class; its validate() is a placeholder that always throws, and every concrete validator (per component type) must override it. Hitting this error means a validator subclass was instantiated without implementing validate(), or the base class was used directly.

Source

Thrown at cli-tool/src/validation/BaseValidator.js:148

    const column = index - lineStart + 1;

    return {
      line: lineNumber,
      column: column,
      lineText: lineText.trim(),
      position: `${lineNumber}:${column}`
    };
  }

  /**
   * Abstract method - must be implemented by subclasses
   * @param {object} component - Component to validate
   * @param {object} options - Validation options
   * @returns {Promise<object>} Validation results
   * @throws {Error} If not implemented
   */
  async validate(component, options = {}) {
    throw new Error('validate() must be implemented by subclass');
  }
}

module.exports = BaseValidator;

View on GitHub (pinned to a0851ed10c)

Solutions

  1. Implement an async validate(component, options = {}) method in the subclass returning the standard result object
  2. If you meant to run a real validation, instantiate the concrete validator for the component type instead of BaseValidator
  3. Add a static assertion or JSDoc @abstract marker so the subclass is checked at definition time

Example fix

// before
class MyValidator extends BaseValidator {}
await new MyValidator().validate(component);

// after
class MyValidator extends BaseValidator {
  async validate(component, options = {}) {
    // ...perform checks...
    return { valid: true, errors: [], warnings: [] };
  }
}
await new MyValidator().validate(component);
Defensive patterns

Strategy: type-guard

Validate before calling

if (validator instanceof BaseValidator && validator.constructor === BaseValidator) {
  throw new Error('Do not instantiate BaseValidator directly');
}

Type guard

function implementsValidate(v) {
  return typeof v === 'object' && v !== null
    && typeof v.validate === 'function'
    && v.validate !== BaseValidator.prototype.validate; // not the stub
}

Try / catch

try {
  result = await validator.validate(component, options);
} catch (e) {
  if (/must be implemented by subclass/.test(e.message)) {
    throw new Error(`${validator.constructor.name} does not implement validate() — implementation bug`);
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling new BaseValidator().validate(...) or creating a subclass that forgets to define an async validate(component, options) method. Also reachable if a subclass defines validate as a non-method property or with a different name, so the inherited stub runs.

Common situations: Adding a new component-type validator and forgetting the validate() override; refactoring renaming validate to validateComponent; JavaScript silently allowing instantiation of the abstract class.

Related errors


AI-assisted analysis of davila7/claude-code-templates@a0851ed10c (2026-08-28). Data as JSON: /api/errors/33af6063bf82d67a. Report an issue: GitHub.