ianstormtaylor / superstruct

A simple and composable way to validate data in JavaScript (and TypeScript).
https://docs.superstructjs.org
MIT License
6.96k stars 223 forks source link

Superstruct cannot create error message if a Symbol fails validation #1134

Closed Mrtenz closed 1 year ago

Mrtenz commented 1 year ago

When a Symbol fails validation, Superstruct is unable to create an error message for it. This results in Superstruct throwing an error message, even when using is for validation.

This can be reproduced using:

is(Symbol('foo'), string());

The expected result is false, but instead this throws the following error:

TypeError: Cannot convert a Symbol value to a string
    at value (./node_modules/superstruct/src/utils.ts:38:65)
    at print (./node_modules/superstruct/src/structs/types.ts:423:43)
    at Struct.validator (./node_modules/superstruct/src/struct.ts:46:24)

Looks like Superstruct is trying to create a stringified version of it in this function, which is not possible for Symbols:

https://github.com/ianstormtaylor/superstruct/blob/fe7e79779573c6619265013919f913e7c6c2138c/src/utils.ts#L37-L39