Codeception / module-rest

REST module for Codeception
MIT License
53 stars 27 forks source link

Get which property failed with seeResponseIsValidOnJsonSchemaString #104

Open francislavoie opened 2 months ago

francislavoie commented 2 months ago

When we use $I->seeResponseIsValidOnJsonSchemaString, and the assertion fails, we get an error like this:

 Step  See response is valid on json schema string "{"type":"object","properties":{"error":{"type":"string"},"client":{"type":["string","number","null"]},"state":{"type":"null"},"message":{"type":["string","null"]},"int..."
 Fail  String value found, but a null is required
Failed asserting that false is true.

This is very confusing and hard to debug because it doesn't tell us exactly which property failed the assertion. When we have a giant schema, figuring that out by hand is a real pain.

It would be fantastic if the error message could be improved to mention which property (or path to the property if deeply nested) failed the assertion.

It looks like https://github.com/jsonrainbow/json-schema is being used under the hood, right now the error message is just taking the "message" from the validator:

https://github.com/Codeception/module-rest/blob/f24db25ca268d379dcd870945ba88845c4cb558f/src/Codeception/Module/REST.php#L961-L966

According to the validator package's README, getErrors() also returns the property that failed. It gives an example like this:

    foreach ($validator->getErrors() as $error) {
        printf("[%s] %s\n", $error['property'], $error['message']);
    }

Could we update the error message for this assertion, please?