pglite icon indicating copy to clipboard operation
pglite copied to clipboard

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

Open claygeo opened this issue 8 months ago • 0 comments

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.

claygeo avatar Mar 30 '26 15:03 claygeo