sindresorhus / ow

Function argument validation for humans
https://sindresorhus.com/ow/
MIT License
3.8k stars 105 forks source link

`Ow` instances are mutable #195

Open mmkal opened 3 years ago

mmkal commented 3 years ago

This can lead to some pretty strange behaviour:

const ow = require('ow')

const array = ow.array

ow([1], array) // ok
ow([''], array.ofType(ow.string)) // ok
ow([1], array) // ArgumentError: (array) Expected `item` to be of type `string` but received type `number`

Many users won't hit this because ow.array and all the other props return a new value every time they're accessed. This should probably be documented at a minimum, but I wondered if there'd be any interest in a refactor to make the Predicates and their validators immutable, and replace addValidator with withValidator or similar which would return a new instance.

sindresorhus commented 3 years ago

I'm totally open to making it immutable.