Skip to content

Show values in case of error (for some checks) #122

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/JsonSchema/Constraints/Constraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,12 @@ public function setUriRetriever(UriRetriever $uriRetriever)
/**
* {@inheritDoc}
*/
public function addError($path, $message)
public function addError($path, $message, $value = null)
{
$this->errors[] = array(
'property' => $path,
'message' => $message
'message' => $message,
'value' => print_r($value, true)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@doits Given that the property is provided in an error message, would using a tool like jq be better for debugging the JSON file data?

);
}

Expand Down
4 changes: 2 additions & 2 deletions src/JsonSchema/Constraints/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ public function check($element, $schema = null, $path = null, $i = null)
}
}

$this->addError($path, "does not have a value in the enumeration " . print_r($schema->enum, true));
$this->addError($path, "does not have a value in the enumeration " . print_r($schema->enum, true), $element);
}
}
}
16 changes: 8 additions & 8 deletions src/JsonSchema/Constraints/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,40 +26,40 @@ public function check($element, $schema = null, $path = null, $i = null)
if (isset($schema->exclusiveMinimum)) {
if (isset($schema->minimum)) {
if ($schema->exclusiveMinimum && $element === $schema->minimum) {
$this->addError($path, "must have a minimum value greater than boundary value of " . $schema->minimum);
$this->addError($path, "must have a minimum value greater than boundary value of " . $schema->minimum, $element);
} else if ($element < $schema->minimum) {
$this->addError($path, "must have a minimum value of " . $schema->minimum);
$this->addError($path, "must have a minimum value of " . $schema->minimum, $element);
}
} else {
$this->addError($path, "use of exclusiveMinimum requires presence of minimum");
}
} else if (isset($schema->minimum) && $element < $schema->minimum) {
$this->addError($path, "must have a minimum value of " . $schema->minimum);
$this->addError($path, "must have a minimum value of " . $schema->minimum, $element);
}

// Verify maximum
if (isset($schema->exclusiveMaximum)) {
if (isset($schema->maximum)) {
if ($schema->exclusiveMaximum && $element === $schema->maximum) {
$this->addError($path, "must have a maximum value less than boundary value of " . $schema->maximum);
$this->addError($path, "must have a maximum value less than boundary value of " . $schema->maximum, $element);
} else if ($element > $schema->maximum) {
$this->addError($path, "must have a maximum value of " . $schema->maximum);
$this->addError($path, "must have a maximum value of " . $schema->maximum, $element);
}
} else {
$this->addError($path, "use of exclusiveMaximum requires presence of maximum");
}
} else if (isset($schema->maximum) && $element > $schema->maximum) {
$this->addError($path, "must have a maximum value of " . $schema->maximum);
$this->addError($path, "must have a maximum value of " . $schema->maximum, $element);
}

// Verify divisibleBy - Draft v3
if (isset($schema->divisibleBy) && $this->fmod($element, $schema->divisibleBy) != 0) {
$this->addError($path, "is not divisible by " . $schema->divisibleBy);
$this->addError($path, "is not divisible by " . $schema->divisibleBy, $element);
}

// Verify multipleOf - Draft v4
if (isset($schema->multipleOf) && $this->fmod($element, $schema->multipleOf) != 0) {
$this->addError($path, "must be a multiple of " . $schema->multipleOf);
$this->addError($path, "must be a multiple of " . $schema->multipleOf, $element);
}

$this->checkFormat($element, $schema, $path, $i);
Expand Down
6 changes: 3 additions & 3 deletions src/JsonSchema/Constraints/String.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ public function check($element, $schema = null, $path = null, $i = null)
{
// Verify maxLength
if (isset($schema->maxLength) && $this->strlen($element) > $schema->maxLength) {
$this->addError($path, "must be at most " . $schema->maxLength . " characters long");
$this->addError($path, "must be at most " . $schema->maxLength . " characters long", $element);
}

//verify minLength
if (isset($schema->minLength) && $this->strlen($element) < $schema->minLength) {
$this->addError($path, "must be at least " . $schema->minLength . " characters long");
$this->addError($path, "must be at least " . $schema->minLength . " characters long", $element);
}

// Verify a regex pattern
if (isset($schema->pattern) && !preg_match('#' . str_replace('#', '\\#', $schema->pattern) . '#', $element)) {
$this->addError($path, "does not match the regex pattern " . $schema->pattern);
$this->addError($path, "does not match the regex pattern " . $schema->pattern, $element);
}

$this->checkFormat($element, $schema, $path, $i);
Expand Down