Xanomorph

Share this post

How to use is_int correctly in Xano

www.xanomorph.com

How to use is_int correctly in Xano

It doesn't do what the equivalent in Python does.

Walter Haydock
Apr 21, 2023
Share
Share this post

How to use is_int correctly in Xano

www.xanomorph.com
white and red arrow sign

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

Thanks for reading Xanomorph! Subscribe for free to receive new posts.

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!

Thanks for reading Xanomorph! Subscribe for free to receive new posts.

Share
Share this post

How to use is_int correctly in Xano

www.xanomorph.com
Comments
Top
New
Community

No posts

Ready for more?

© 2023 Walter Haydock
Privacy ∙ Terms ∙ Collection notice
Start WritingGet the app
Substack is the home for great writing