Thanks to visit codestin.com
Credit goes to github.com

Skip to content

More helpful union error messages #275

@johngeorgewright

Description

@johngeorgewright

I recently received a confusing error message from union validation. Take the following as an example... a simple union of 2 objects:

import * as t from 'runtypes'

const S = t.String.withConstraint((x) => x === 'bar' || 'Should be bar')

const R = t.Record({ s: S }).Or(t.Record({ t: S }))

try {
  R.check({ s: 'rab' })
} catch (error) {
  console.error(error)
}

This produces:

ValidationError: Expected { s: string; } | { t: string; }, but was object
    at ...
    <stack traces>
    at ... {
  code: 'TYPE_INCORRECT'
}

The message Expected { s: string; } | { t: string; }, but was object isn't all that helpful. Can this message be improved somehow?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions