I'm trying to write a mock API server in Karate and I want to make sure that the incoming request matches a given schema (or Karate fuzzy expression as given in this example)
Here's what I'm trying, but it's resulting in the following error:
Error
11:42:13.342 [armeria-common-worker-nio-2-9] WARN com.intuit.karate - scenario match evaluation failed at line 14: pathMatches('/manufacturers') && methodIs('post') && match request != schema - com.intuit.karate.KarateException: >>>> js failed:
01: pathMatches('/manufacturers') && methodIs('post') && match request != schema
<<<<
org.graalvm.polyglot.PolyglotException: SyntaxError: Unnamed:1:59 Expected ; but found request
pathMatches('/manufacturers') && methodIs('post') && match request != schema
Feature file
Feature: Manufacturer Mock API Server
Background:
* configure cors = true
* def schema =
"""
{
id: "#number",
name: "#string",
status: "#string"
}
"""
* url 'http://localhost:8080/manufacturers'
Scenario: pathMatches('/manufacturers') && methodIs('post') && match request != schema
* def responseStatus 400
Scenario: pathMatches('/manufacturers') && methodIs('post') && match request == schema
* def response = request
Question
Is there any way to validate the schema of an incoming request body?
Edits
Based on Peter's answer below, I tried this, but this is ALWAYS resulting in a 200 OK response:
Scenario: pathMatches('/manufacturers') && methodIs('post')
* def schemaCheck = karate.match('request == schema')
* eval
"""
if (schemaCheck.pass) {
responseStatus = 200;
response = request;
} else {
responseStatus = 400;
response = schemaCheck;
}
"""