Check input of keyword argument - how does it work?

When writing your own keyword, is there a way to limit input to an attribute to certain set/group?
Is there a way how to check these before doing anything else with those values after?

Keyword1 with 1 argument :

  • Let´s say, in that argument I wanna allow only integers 1 to 4, anything greater than that should not be accepted.

Keyword 2 with 1 argument:

  • Let´s say, I want a number, not longer than 6 digits (1 to 5 digits is fine)

Hey!

I dont recon Robot Framework necessarily has anything like that specifically. :thinking:

But I dont see why you couldn’t utilize a customLibrary or write it into a keyword.

Something like this maybe?

from robot.api.deco import keyword, library

ROBOT_AUTO_KEYWORDS = False

@library
class CustomLibrary:
    @keyword
    def keyword1(self, arg:int): 
        upper_bound = 4
        lower_bound = 1
        if upper_bound < arg < lower_bound:
            raise ValueError("Argument value is invalid! Not in range of acceptable values")

        print("Allowed Argument")
        #or
        #Do Something

    @keyword
    def keyword2(self, arg): 
        num = 6
        if len(arg) > num:
            raise ValueError("Argument length exceeds the maximum allowed amount!")
        
        print("Allowed Argument")
        #or
        #Do Something
1 Like

Just a shot in to the blue. Maybe through Pydantic validators Validators - Pydantic

Thought you still would have to validate the parameter inside the keyword.

Not sure if that works nicely. If it does, it might be worthwhile enhancing robot by adding the option to add an argument validator to keywords. Then parameters would be validated automatically.

1 Like

Actually, I think the @validate_call annotation could achieve what you want. Example here: Pydantic: Simplifying Data Validation in Python – Real Python

Hello,

I usually do it the way @Endo showed. But for keyword implemented in python there is an alternative using the Argument conversion feature.

pekkaklarck mentioned it in the video presentation of RF7(at 27:40 to 33:30), but I have not tried it yet.

Apparently, type hint in python implemented keywords are not just type hints for documentation, but actually generate a failure (in some case only?).
Since RF7.0 “literals” are supported, but on earlier versions it was already possible using Enum or IntEnum. The new Literal feature has less boilerplate, as you don’t have to define an enum class beforehand.

I have not played with it yet, but this is what I understand from the video. I will be sure to try it someday though! Thanks for the reminder!

1 Like

Thank you, @FrancisG and @Noordsestern, I have to have a better look at those as this is the first time I heard about them, I will try the Argument conversion first.