opis / json-schema

JSON Schema validator for PHP
https://opis.io/json-schema
Apache License 2.0
566 stars 57 forks source link

Format null value on error messages #144

Open EVDW opened 1 week ago

EVDW commented 1 week ago

Version used: 2.3.0

Problem description: When using a custom error message with the argument {data:value} in the error message template, if the data value is null, the error message will contains the string "{data:value}" instead of just nothing.

How to reproduce: Here is a schema configuration with:

'$error' => [
    'type' => 'The data should be a valid resource identifier ({expected}), "{data:value}" ({data:type}) given.',
]

Let's say a null value is given to the schema, the rendered error message will return: The data should be a valid resource identifier (integer), "{data:value}" (null) given.

The expected return should be: The data should be a valid resource identifier (integer), "" (null) given.

Issue: The issue is in the ErrorFormatter where the key to replaced is searched with an isset function. But isset determine if a variable is declared and is different than null, which is the problem. The array_key_exists function should be better in order to get null values and to replace the key in the error template message.

sorinsarca commented 1 week ago

I will add a fix