I wanted to have a switch/case statement that accepts an object in Javascript.
The function looks like this.
const validate = (values) => { values is an object, can be accessed like so (values.bar, values.foo)
const errors = {}
switch(values) {
case !values.bar || values.bar === '':
errors.bar = 'Enter bar'
case values.bar.length < 10:
errors.bar = 'Bar is too short'
case !values.foo || values.foo === '':
errors.foo = 'Enter foo'
...
default:
return errors
}
}
This doesn't work and I have used an if/else statement instead, but I feel like a switch/case would be perfect for this kind of example. Thoughts?
Change
switch(values) {
To
switch(true) {
switch
checks with strict equality.
And use, if necessary some break
if you do not want to fall through the switch cases (kudos to blex).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With