Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix errors when creating a predicate with a request body containing a field of type number. #768

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

srmppn
Copy link

@srmppn srmppn commented May 17, 2024

Description

I encountered an error when creating a predicate that matches the request body with type number using contains, startsWith, or endsWith. For example, when creating a predicate for the API /transfer-money with an amount that starts with 200:

predicates = {
    startsWith: { amount: 200 }
}

Sending a request with an amount of 200.01 results in the following error:

{
    "errors": [
        {
            "message": "actual.indexOf is not a function",
            "name": "TypeError",
            "stack": "TypeError: actual.indexOf is not a function\n    at ..."
        }
    ]
}

Screenshot 2567-05-17 at 19 37 17

Modifications

I have created three new methods (contains, startsWith, endsWith) that check whether the actual type is a string. If it is not, the value is converted to a string before calling the .indexOf method.

However, I'm considering instead of allow using contains, startsWith, endsWith with type number it might be better to throw an exception and provide more understandable error messages, such as:

Type not supported for this parameter {name}. Consider using equals or matches instead. 

Or maybe we could add features such as lessThan or greaterThan to support specific types of numbers. What do you think?

Details

Mountebank version 2.9.1

@srmppn srmppn changed the title Fix errors when matching with type number Fix errors when creating predicate with type number May 17, 2024
@srmppn srmppn changed the title Fix errors when creating predicate with type number Fix errors when creating a predicate with a request body containing a field of type number. May 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant