Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom error message #239

Closed
csimplestring opened this issue Mar 15, 2016 · 3 comments
Closed

Custom error message #239

csimplestring opened this issue Mar 15, 2016 · 3 comments

Comments

@csimplestring
Copy link

Hi, it is great to support customized error message in the 1.6.0 release. But after reading the source code for a while, I notice a minor issue during adding error:

$this->addError($path, 'The item ' . $i . '[' . $k . '] is not defined and the definition does not allow additional items', 'additionalItems', array('additionalItems' => $schema->additionalItems,));

The problem is that the 'not-allowed' field '$i' and '$k' is in the error message, external users have to parse it from error message. Once the error message is changed, external users have to change parsing accordingly.

Therefore to fully support customized error message, the error message should not contain any error field path, e.g., '$i', '$k', which shall be moved to '$path' instead.

@jonathan-s
Copy link
Contributor

I haven't checked how you implement custom error messages, but the approach described in this issue comment seems like a rather nice one: geraintluff/tv4#115 (comment)

@abreksa4
Copy link

+1

@DannyvdSluijs
Copy link
Collaborator

The error message you mentioned above is still in the code base but has shifted a bit. For reference you can find them here:
https://github.com/justinrainbow/json-schema/blob/master/src/JsonSchema/ConstraintError.php#L59
https://github.com/justinrainbow/json-schema/blob/master/src/JsonSchema/Constraints/CollectionConstraint.php#L101-L110

@csimplestring Since your issue report the repo has progressed and since #364 the property became available. Using below example code you can see the property is now part of the errors.
In an attempt to cleanup this repo we are trying to filter the issues and see which ones might be closed. Is it safe to assume this is a rather old issue, which sadly was left unanswered, but now can be closed? Feel free to close it yourself with some comments if helpful.

Schema:

{
  "type": "object",
  "additionalProperties": false
}

Code:

<?php

use JsonSchema\Constraints\Constraint;
use JsonSchema\Uri\UriRetriever;
use JsonSchema\Validator;

require_once '../../vendor/autoload.php';

$retriever = new UriRetriever();
$schema = $retriever->retrieve('internal://' . realpath('./schema.json'));
$obj = json_decode('{"foo": "bar"}', false);
$validator = new Validator();
$result = $validator->validate($obj, $schema, Constraint::CHECK_MODE_APPLY_DEFAULTS);

var_dump($validator->getErrors()[0]["constraint"]["params"]["property"]);

Output:

string(3) "foo"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants