Skip to content

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
@jarl-dk jarl-dk force-pushed the master branch 2 times, most recently from f7b3d51 to 1690e62 Compare July 31, 2025 05:28
@jarl-dk
Copy link
Member

jarl-dk commented Sep 30, 2025

The project has been stale for a while. Sorry for that.
I agree with your considerations regarding an exception in stead when using these matchers on numbers. Could you update the branch for such an implementation in stead? Or even better have matchers like lessThan, etc.

@srmppn
Copy link
Author

srmppn commented Sep 30, 2025

@jarl-dk Sure, I will do it 👍. Would you like me to create issue ticket for visibility and tracking in the future

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.

2 participants