prismatic-io / spectral

Prismatic's typescript library for custom components
https://prismatic.io/docs/spectral/custom-component-library
35 stars 2 forks source link

Fix the contains function to handle stringified numbers #196

Closed taylorreece closed 5 months ago

taylorreece commented 5 months ago

The contains function is designed to determine if an element is contained within an array (or object, or string... but this PR relates to arrays).

For strings, it works as expected - "foo" is contained in ["foo", "bar"]. For numbers, it works as expected - 2 is contained in [1,2,3]. For stringified numbers, it does not work as expected. It currently returns false if you check if "2" is contained in ["1", "2", "3"].

That's because the element you check is passed through the parseValue function, which checks if the value is a string and, if so, the value is JSON.parse'd. JSON.parse("2") becomes 2, and 2 !== "2".

This PR loosens restrictions on contains, so that the value "2" would be passed to contains, and both the values "2" and 2 would be searched for in the array.