Betterment / betterlint

MIT License
41 stars 15 forks source link

Boolean query param cop #35

Closed ivangreene closed 6 months ago

ivangreene commented 7 months ago

This is a mistake I've made at least twice now. Basically, query params like ?should_do_something=false will result in the value of string 'false' when fetched from a params object.

# Sending '?should_do_something=false' results in string 'false' here!
should_do_something = params.fetch(:should_do_something, false)

The only way that they can be falsey is if they are omitted from the request and default to false. This is also an issue where we are expecting exactly true and not just any truthy value. The solution is to cast them explicitly

# Always an actual boolean value
should_do_something = ActiveModel::Type::Boolean.new.cast(params.fetch(:should_do_something, false))