Skip to content

DF-1509: Convert the form definition error classes to JavaScript with JSDoc - #509

Open
alexluckett wants to merge 1 commit into
chore/df-1509-typesfrom
chore/df-1509-errors
Open

alexluckett wants to merge 1 commit into
chore/df-1509-typesfrom
chore/df-1509-errors

Conversation

@alexluckett

@alexluckett alexluckett commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Proposed change

Part of the TypeScript to JavaScript migration (v6), this PR is stacked on the public types PR and converts engine/errors.ts, the form definition error classes, to JavaScript with JSDoc types. It is kept separate because this is the first file where the JavaScript form of an inherited field changes behaviour at runtime.

  • In TypeScript, declare cause: ValidationError narrows the type of Error.cause without emitting any code. JavaScript has no declare, and a class field without a value would reset cause to undefined after the Error constructor has set it. The field therefore takes the value the constructor has already set, with the same comment that is used in the components:
    /** @type {Joi.ValidationError} */
    // @ts-expect-error TS2729: the base constructor has already set it
    cause = this.cause
    The published type is still cause: ValidationError.
  • Behaviour change: Error stores cause as a non-enumerable property, whereas a class field is enumerable, so cause now shows up when an error's properties are listed. JSON.stringify(error) will include the Joi validation error, which describes the form definition rather than any data entered by users. This repo's logger (@elastic/ecs-pino-format) writes only the error type, message and stack, and pino's standard serializer skips an error-like cause, so our logs are unaffected.
  • The Joi type is imported in the default form, @import Joi from 'joi', and written as Joi.ValidationError. With the braces form, tsc 5.9 leaves the import out of errors.d.ts because joi also exports ValidationError as a value, and the published file would then refer to a type it never imports. Consumers would see that as an error, or as any with skipLibCheck. A comment in the file explains this, and TypeScript 7 keeps the braces form.
  • conditionName, controllerName and componentType remain readonly in the published types (@readonly), and public has been dropped.

The only difference from main in the compiled code is the cause = this.cause line. The full test suite gives the same results as on main (2,583 passed, 13 skipped).

Jira ticket: DF-1509

Type of change

  • Bug fix
  • New feature
  • Breaking change
  • Misc. (documentation, build updates, etc)

Checklist

  • You have executed this code locally and it performs as expected.
  • You have added tests to verify your code works.
  • You have added code comments and JSDoc, where appropriate.
  • There is no commented-out code.
  • You have added developer docs in README.md and docs/* (where appropriate, e.g. new features).
  • The tests are passing (npm run test).
  • The linting checks are passing (npm run lint).
  • The code has been formatted (npm run format).

@alexluckett
alexluckett added this pull request to stack #513 November 24, 2026 21:27
SchemaValidationError narrows Error.cause to the Joi ValidationError.
JavaScript has no type-only declare field, so a typed class field keeps
the value the Error constructor set. As a class field, cause is now
enumerable: JSON.stringify(error) includes the validation error, which
describes the form definition. The repo logger writes only the error
type, message and stack.
@sonarqubecloud

Copy link
Copy Markdown

@jbarnsley10 jbarnsley10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants