Skip to content

fix(types): widen boolean serializer to accept number and string coercions#952

Open
claygeo wants to merge 1 commit intoelectric-sql:mainfrom
claygeo:fix/boolean-serializer-coercion
Open

fix(types): widen boolean serializer to accept number and string coercions#952
claygeo wants to merge 1 commit intoelectric-sql:mainfrom
claygeo:fix/boolean-serializer-coercion

Conversation

@claygeo
Copy link
Copy Markdown

@claygeo claygeo commented Mar 30, 2026

Problem

The boolean type serializer currently throws for any non-boolean input:

serialize: (x: boolean) => {
  if (typeof x !== 'boolean') {
    throw new Error('Invalid input for boolean type')
  }
  return x ? 't' : 'f'
}

JavaScript often represents booleans as 1/0 or "true"/"false" (from form inputs, JSON APIs, etc.). Passing these to an UPDATE statement causes an unexpected runtime error.

Fixes #791

Fix

Widen the serializer to accept boolean | number | string, matching PostgreSQL's own boolean casting rules:

serialize: (x: boolean | number | string) => {
  if (typeof x === 'string') {
    return ['true', 't', '1', 'yes', 'on'].includes(x.toLowerCase())
      ? 't'
      : 'f'
  }
  return x ? 't' : 'f'
}
  • Strings: case-insensitive match against PostgreSQL's accepted true literals (true, t, 1, yes, on). Everything else serializes to 'f' (matching false, f, 0, no, off).
  • Numbers/booleans: standard JS truthiness (1 → 't', 0 → 'f').

Tests

Added 10 serializer tests covering all input types and boundaries.

…cions

Previously the boolean serializer threw for any non-boolean input,
causing UPDATE statements with JS truthy values (1, 0, "true", "false")
to error unexpectedly. PostgreSQL accepts these coercions natively.

Widen the serialize signature to boolean | number | string:
- Strings: matches PostgreSQL's accepted true literals
  ('true', 't', '1', 'yes', 'on') case-insensitively
- Numbers / booleans: coerced via truthiness (1 -> 't', 0 -> 'f')

Fixes: electric-sql#791
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.

QueryFailedError: Invalid input for boolean type

1 participant