test(testFunctions): display stringified arguments in error message by pano9000 · Pull Request #2442 · validatorjs/validator.js

Hello,

the error message that the testFunctions currently throws, when a test case fails is currently not correctly displaying options objects, even though the intention clearly is there.

This pull request properly implements this now, by stringifying the arguments, making sure they can get correctly printed as string.

Before
Error: validator.isDate("2024/05/01", [Object object]) passed but should have failed
After:
Error: validator.isDate("2024/05/01", {"delimiters":["/"," "],"format":"YYYY/MM/DD","strictMode":false}) passed but should have failed

This is achieved by a tiny helper function that maps through the args and JSON.stringifys them.

All of these are available since node v0.10, so we still are compatible with that (ancient :)) version:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map#browser_compatibility
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

I would've like to replace the "format" function with template literals as well, but these were not available in node 0.10.0 (which is the minimum engine mentioned in the package.json) yet, but only came with node 4.

Checklist

  • PR contains only changes related; no stray files, etc.
  • README updated (where applicable)
  • Tests written (where applicable)
  • References provided in PR (where applicable)