How to call validate.js and use it in feature file (to verify the response)?

234 Views Asked by At

How to call validate.js and use it in feature file (to verify a specific part of the response)?

I am trying to use https://github.com/validatorjs/validator.js which is a library with some awesome validators out-of-the-box.

While reading the Karate documentation there is a way to read / call and read .js files, so I taught there has to be a way to do this. https://intuit.github.io/karate/#schema-validation

I gotten this far but: ReferenceError: "isNumeric" is not defined in at line number 1

var validator = require('validator');
* def isNumeric = validator.isNumeric ;

In a scenario:
    And match each response/list/costs/numberX == '#? isNumeric(_)'

I feel I am really close...

1

There are 1 best solutions below

2
On

Unfortunately as of now Karate supports only ES5 (via Nashorn) and also does not support JS "module" concepts such as the import or require keywords.

Personally I think this is a good thing, the more JS you use, the more unmaintainable your scripts become. And there is no good way to debug. Note that Karate has syntax to do "functional style" loops and transformations.

Also I have found that in most of the cases where you think JS is needed, Karate's built-in schema validation is sufficient or a better choice.

That said, we hope that when we switch to Graal (proposed, and a must for Java 13+) we will be able to use ES6+ and I am personally looking forward to the arrow notation for functions.