Commit 932822f4 authored by Sean McGivern's avatar Sean McGivern Committed by Stan Hu

Merge branch '23890-api-should-accepts-boolean' into 'master'

API: Fix booleans not recognized as such when using the `to_boolean` helper

Fixes #22831
Fixes #23890

See merge request !7149
parent ef037519
......@@ -6,6 +6,7 @@ module API
SUDO_PARAM = :sudo
def to_boolean(value)
return value if [true, false].include?(value)
return true if value =~ /^(true|t|yes|y|1|on)$/i
return false if value =~ /^(false|f|no|n|0|off)$/i
......@@ -266,18 +266,25 @@ describe API::Helpers, api: true do
describe '.to_boolean' do
it 'accepts booleans' do
expect(to_boolean(true)).to be(true)
expect(to_boolean(false)).to be(false)
it 'converts a valid string to a boolean' do
expect(to_boolean('true')).to be_truthy
expect(to_boolean('YeS')).to be_truthy
expect(to_boolean('t')).to be_truthy
expect(to_boolean('1')).to be_truthy
expect(to_boolean('ON')).to be_truthy
expect(to_boolean('FaLse')).to be_falsy
expect(to_boolean('F')).to be_falsy
expect(to_boolean('NO')).to be_falsy
expect(to_boolean('n')).to be_falsy
expect(to_boolean('0')).to be_falsy
expect(to_boolean('oFF')).to be_falsy
expect(to_boolean(true)).to be(true)
expect(to_boolean('true')).to be(true)
expect(to_boolean('YeS')).to be(true)
expect(to_boolean('t')).to be(true)
expect(to_boolean('1')).to be(true)
expect(to_boolean('ON')).to be(true)
expect(to_boolean('FaLse')).to be(false)
expect(to_boolean('F')).to be(false)
expect(to_boolean('NO')).to be(false)
expect(to_boolean('n')).to be(false)
expect(to_boolean('0')).to be(false)
expect(to_boolean('oFF')).to be(false)
it 'converts an invalid string to nil' do
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment