I first learned how to code in Python, which is a relatively forgiving and easy language to start out with. And because of that, most of my coding frame of reference centers around it.
In Python, if you run the following, it will return True
isinstance(5, int)
If you run the below, it will return False
isinstance(5.5, int)
Now, you can attempt to force any value into the data type of int
. For example:
int(5.5)
will return the int
of 5
.
If you attempt the below, however, you will get a ValueError
:
int("cat")
That’s because you can’t turn a cat into an integer (just like you can’t feed one into an ATM).
But Xano works differently.
The is_int
filter checks if the data type is int
, not whether value is a valid int
.
And you can force a text string into int
.
So the below will return True
(confusingly, in my mind):
So how do you test if something is actually a valid int
?
You need to do a weird workaround where you:
Force the value into an int (using
to_int
)Multiply the value by 1
The result will either be 0 or a valid int
. You can then run your conditional logic on whether or not the output is 0 or not 0.
A major problem with this approach is that zero is a valid integer! And if you plug in zero to your function, it will return…zero. But if your logic is set up to act differently on whether or not the output is zero, you might get an unanticipated result.
Unfortunately I don’t have a good fix for this, but this should hopefully be a corner case for most people. Otherwise, I hope this is helpful.
If you know a better way, please tell me in the comments!