Skip to content

Commit 043bc7c

Browse files
committed
Merge pull request #142 from onlinesid/custom-error-msg
Optional extra arguments for custom error messages
2 parents a4bee9f + acdaf80 commit 043bc7c

14 files changed

+90
-64
lines changed

src/JsonSchema/Constraints/CollectionConstraint.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ public function check($value, $schema = null, $path = null, $i = null)
2424
{
2525
// Verify minItems
2626
if (isset($schema->minItems) && count($value) < $schema->minItems) {
27-
$this->addError($path, "There must be a minimum of " . $schema->minItems . " items in the array");
27+
$this->addError($path, "There must be a minimum of " . $schema->minItems . " items in the array", 'minItems', array('minItems' => $schema->minItems,));
2828
}
2929

3030
// Verify maxItems
3131
if (isset($schema->maxItems) && count($value) > $schema->maxItems) {
32-
$this->addError($path, "There must be a maximum of " . $schema->maxItems . " items in the array");
32+
$this->addError($path, "There must be a maximum of " . $schema->maxItems . " items in the array", 'maxItems', array('maxItems' => $schema->maxItems,));
3333
}
3434

3535
// Verify uniqueItems
@@ -39,7 +39,7 @@ public function check($value, $schema = null, $path = null, $i = null)
3939
$unique = array_map(function($e) { return var_export($e, true); }, $value);
4040
}
4141
if (count(array_unique($unique)) != count($value)) {
42-
$this->addError($path, "There are no duplicates allowed in the array");
42+
$this->addError($path, "There are no duplicates allowed in the array", 'uniqueItems');
4343
}
4444
}
4545

@@ -92,7 +92,7 @@ protected function validateItems($value, $schema = null, $path = null, $i = null
9292
$this->checkUndefined($v, $schema->additionalItems, $path, $k);
9393
} else {
9494
$this->addError(
95-
$path, 'The item ' . $i . '[' . $k . '] is not defined and the definition does not allow additional items');
95+
$path, 'The item ' . $i . '[' . $k . '] is not defined and the definition does not allow additional items', 'additionalItems', array('additionalItems' => $schema->additionalItems,));
9696
}
9797
} else {
9898
// Should be valid against an empty schema

src/JsonSchema/Constraints/Constraint.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,20 @@ public function setUriRetriever(UriRetriever $uriRetriever)
6161
/**
6262
* {@inheritDoc}
6363
*/
64-
public function addError($path, $message)
64+
public function addError($path, $message, $constraint='', array $more=null)
6565
{
66-
$this->errors[] = array(
66+
$error = array(
6767
'property' => $path,
68-
'message' => $message
68+
'message' => $message,
69+
'constraint' => $constraint,
6970
);
71+
72+
if (is_array($more) && count($more) > 0)
73+
{
74+
$error += $more;
75+
}
76+
77+
$this->errors[] = $error;
7078
}
7179

7280
/**

src/JsonSchema/Constraints/ConstraintInterface.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ public function addErrors(array $errors);
3333
/**
3434
* adds an error
3535
*
36-
* @param $path
37-
* @param $message
36+
* @param string $path
37+
* @param string $message
38+
* @param string $constraint the constraint/rule that is broken, e.g.: 'minLength'
39+
* @param array $more more array elements to add to the error
3840
*/
39-
public function addError($path, $message);
41+
public function addError($path, $message, $constraint='', array $more=null);
4042

4143
/**
4244
* checks if the validator has not raised errors

src/JsonSchema/Constraints/EnumConstraint.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ public function check($element, $schema = null, $path = null, $i = null)
4141
}
4242
}
4343

44-
$this->addError($path, "Does not have a value in the enumeration " . print_r($schema->enum, true));
44+
$this->addError($path, "Does not have a value in the enumeration " . print_r($schema->enum, true), 'enum', array('enum' => $schema->enum,));
4545
}
4646
}

src/JsonSchema/Constraints/FormatConstraint.php

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ public function check($element, $schema = null, $path = null, $i = null)
2929
switch ($schema->format) {
3030
case 'date':
3131
if (!$date = $this->validateDateTime($element, 'Y-m-d')) {
32-
$this->addError($path, sprintf('Invalid date %s, expected format YYYY-MM-DD', json_encode($element)));
32+
$this->addError($path, sprintf('Invalid date %s, expected format YYYY-MM-DD', json_encode($element)), 'format', array('format' => $schema->format,));
3333
}
3434
break;
3535

3636
case 'time':
3737
if (!$this->validateDateTime($element, 'H:i:s')) {
38-
$this->addError($path, sprintf('Invalid time %s, expected format hh:mm:ss', json_encode($element)));
38+
$this->addError($path, sprintf('Invalid time %s, expected format hh:mm:ss', json_encode($element)), 'format', array('format' => $schema->format,));
3939
}
4040
break;
4141

@@ -45,74 +45,79 @@ public function check($element, $schema = null, $path = null, $i = null)
4545
!$this->validateDateTime($element, 'Y-m-d\TH:i:sP') &&
4646
!$this->validateDateTime($element, 'Y-m-d\TH:i:sO')
4747
) {
48-
$this->addError($path, sprintf('Invalid date-time %s, expected format YYYY-MM-DDThh:mm:ssZ or YYYY-MM-DDThh:mm:ss+hh:mm', json_encode($element)));
48+
$this->addError($path, sprintf('Invalid date-time %s, expected format YYYY-MM-DDThh:mm:ssZ or YYYY-MM-DDThh:mm:ss+hh:mm', json_encode($element)), 'format', array('format' => $schema->format,));
4949
}
5050
break;
5151

5252
case 'utc-millisec':
5353
if (!$this->validateDateTime($element, 'U')) {
54-
$this->addError($path, sprintf('Invalid time %s, expected integer of milliseconds since Epoch', json_encode($element)));
54+
$this->addError($path, sprintf('Invalid time %s, expected integer of milliseconds since Epoch', json_encode($element)), 'format', array('format' => $schema->format,));
5555
}
5656
break;
5757

5858
case 'regex':
5959
if (!$this->validateRegex($element)) {
60-
$this->addError($path, 'Invalid regex format ' . $element);
60+
$this->addError($path, 'Invalid regex format ' . $element, 'format', array('format' => $schema->format,));
6161
}
6262
break;
6363

6464
case 'color':
6565
if (!$this->validateColor($element)) {
66-
$this->addError($path, "Invalid color");
66+
$this->addError($path, "Invalid color", 'format', array('format' => $schema->format,));
6767
}
6868
break;
6969

7070
case 'style':
7171
if (!$this->validateStyle($element)) {
72-
$this->addError($path, "Invalid style");
72+
$this->addError($path, "Invalid style", 'format', array('format' => $schema->format,));
7373
}
7474
break;
7575

7676
case 'phone':
7777
if (!$this->validatePhone($element)) {
78-
$this->addError($path, "Invalid phone number");
78+
$this->addError($path, "Invalid phone number", 'format', array('format' => $schema->format,));
7979
}
8080
break;
8181

8282
case 'uri':
8383
if (null === filter_var($element, FILTER_VALIDATE_URL, FILTER_NULL_ON_FAILURE)) {
84-
$this->addError($path, "Invalid URL format");
84+
$this->addError($path, "Invalid URL format", 'format', array('format' => $schema->format,));
8585
}
8686
break;
8787

8888
case 'email':
8989
if (null === filter_var($element, FILTER_VALIDATE_EMAIL, FILTER_NULL_ON_FAILURE)) {
90-
$this->addError($path, "Invalid email");
90+
$this->addError($path, "Invalid email", 'format', array('format' => $schema->format,));
9191
}
9292
break;
9393

9494
case 'ip-address':
9595
case 'ipv4':
9696
if (null === filter_var($element, FILTER_VALIDATE_IP, FILTER_NULL_ON_FAILURE | FILTER_FLAG_IPV4)) {
97-
$this->addError($path, "Invalid IP address");
97+
$this->addError($path, "Invalid IP address", 'format', array('format' => $schema->format,));
9898
}
9999
break;
100100

101101
case 'ipv6':
102102
if (null === filter_var($element, FILTER_VALIDATE_IP, FILTER_NULL_ON_FAILURE | FILTER_FLAG_IPV6)) {
103-
$this->addError($path, "Invalid IP address");
103+
$this->addError($path, "Invalid IP address", 'format', array('format' => $schema->format,));
104104
}
105105
break;
106106

107107
case 'host-name':
108108
case 'hostname':
109109
if (!$this->validateHostname($element)) {
110-
$this->addError($path, "Invalid hostname");
110+
$this->addError($path, "Invalid hostname", 'format', array('format' => $schema->format,));
111111
}
112112
break;
113113

114114
default:
115-
// Do nothing so that custom formats can be used.
115+
// Empty as it should be:
116+
// The value of this keyword is called a format attribute. It MUST be a string.
117+
// A format attribute can generally only validate a given set of instance types.
118+
// If the type of the instance to validate is not in this set, validation for
119+
// this format attribute and instance SHOULD succeed.
120+
// http://json-schema.org/latest/json-schema-validation.html#anchor105
116121
break;
117122
}
118123
}

src/JsonSchema/Constraints/NumberConstraint.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,40 +26,40 @@ public function check($element, $schema = null, $path = null, $i = null)
2626
if (isset($schema->exclusiveMinimum)) {
2727
if (isset($schema->minimum)) {
2828
if ($schema->exclusiveMinimum && $element === $schema->minimum) {
29-
$this->addError($path, "Must have a minimum value greater than boundary value of " . $schema->minimum);
29+
$this->addError($path, "Must have a minimum value greater than boundary value of " . $schema->minimum, 'exclusiveMinimum', array('minimum' => $schema->minimum,));
3030
} else if ($element < $schema->minimum) {
31-
$this->addError($path, "Must have a minimum value of " . $schema->minimum);
31+
$this->addError($path, "Must have a minimum value of " . $schema->minimum, 'minimum', array('minimum' => $schema->minimum,));
3232
}
3333
} else {
34-
$this->addError($path, "Use of exclusiveMinimum requires presence of minimum");
34+
$this->addError($path, "Use of exclusiveMinimum requires presence of minimum", 'missingMinimum');
3535
}
3636
} else if (isset($schema->minimum) && $element < $schema->minimum) {
37-
$this->addError($path, "Must have a minimum value of " . $schema->minimum);
37+
$this->addError($path, "Must have a minimum value of " . $schema->minimum, 'minimum', array('minimum' => $schema->minimum,));
3838
}
3939

4040
// Verify maximum
4141
if (isset($schema->exclusiveMaximum)) {
4242
if (isset($schema->maximum)) {
4343
if ($schema->exclusiveMaximum && $element === $schema->maximum) {
44-
$this->addError($path, "Must have a maximum value less than boundary value of " . $schema->maximum);
44+
$this->addError($path, "Must have a maximum value less than boundary value of " . $schema->maximum, 'exclusiveMaximum', array('maximum' => $schema->maximum,));
4545
} else if ($element > $schema->maximum) {
46-
$this->addError($path, "Must have a maximum value of " . $schema->maximum);
46+
$this->addError($path, "Must have a maximum value of " . $schema->maximum, 'maximum', array('maximum' => $schema->maximum,));
4747
}
4848
} else {
49-
$this->addError($path, "Use of exclusiveMaximum requires presence of maximum");
49+
$this->addError($path, "Use of exclusiveMaximum requires presence of maximum", 'missingMinimum');
5050
}
5151
} else if (isset($schema->maximum) && $element > $schema->maximum) {
52-
$this->addError($path, "Must have a maximum value of " . $schema->maximum);
52+
$this->addError($path, "Must have a maximum value of " . $schema->maximum, 'maximum', array('maximum' => $schema->maximum,));
5353
}
5454

5555
// Verify divisibleBy - Draft v3
5656
if (isset($schema->divisibleBy) && $this->fmod($element, $schema->divisibleBy) != 0) {
57-
$this->addError($path, "Is not divisible by " . $schema->divisibleBy);
57+
$this->addError($path, "Is not divisible by " . $schema->divisibleBy, 'divisibleBy', array('divisibleBy' => $schema->divisibleBy,));
5858
}
5959

6060
// Verify multipleOf - Draft v4
6161
if (isset($schema->multipleOf) && $this->fmod($element, $schema->multipleOf) != 0) {
62-
$this->addError($path, "Must be a multiple of " . $schema->multipleOf);
62+
$this->addError($path, "Must be a multiple of " . $schema->multipleOf, 'multipleOf', array('multipleOf' => $schema->multipleOf,));
6363
}
6464

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

src/JsonSchema/Constraints/ObjectConstraint.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function validatePatternProperties($element, $path, $patternProperties)
4646
foreach ($patternProperties as $pregex => $schema) {
4747
// Validate the pattern before using it to test for matches
4848
if (@preg_match('/'. $pregex . '/', '') === false) {
49-
$this->addError($path, 'The pattern "' . $pregex . '" is invalid');
49+
$this->addError($path, 'The pattern "' . $pregex . '" is invalid', 'pregex', array('pregex' => $pregex,));
5050
continue;
5151
}
5252
foreach ($element as $i => $value) {
@@ -77,7 +77,7 @@ public function validateElement($element, $matches, $objectDefinition = null, $p
7777

7878
// no additional properties allowed
7979
if (!in_array($i, $matches) && $additionalProp === false && $this->inlineSchemaProperty !== $i && !$definition) {
80-
$this->addError($path, "The property - " . $i . " - is not defined and the definition does not allow additional properties");
80+
$this->addError($path, "The property " . $i . " is not defined and the definition does not allow additional properties", 'additionalProp');
8181
}
8282

8383
// additional properties defined
@@ -92,7 +92,7 @@ public function validateElement($element, $matches, $objectDefinition = null, $p
9292
// property requires presence of another
9393
$require = $this->getProperty($definition, 'requires');
9494
if ($require && !$this->getProperty($element, $require)) {
95-
$this->addError($path, "The presence of the property " . $i . " requires that " . $require . " also be present");
95+
$this->addError($path, "The presence of the property " . $i . " requires that " . $require . " also be present", 'requires');
9696
}
9797

9898
if (!$definition) {

src/JsonSchema/Constraints/StringConstraint.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,23 @@ public function check($element, $schema = null, $path = null, $i = null)
2424
{
2525
// Verify maxLength
2626
if (isset($schema->maxLength) && $this->strlen($element) > $schema->maxLength) {
27-
$this->addError($path, "Must be at most " . $schema->maxLength . " characters long");
27+
$this->addError($path, "Must be at most " . $schema->maxLength . " characters long", 'maxLength', array(
28+
'maxLength' => $schema->maxLength,
29+
));
2830
}
2931

3032
//verify minLength
3133
if (isset($schema->minLength) && $this->strlen($element) < $schema->minLength) {
32-
$this->addError($path, "Must be at least " . $schema->minLength . " characters long");
34+
$this->addError($path, "Must be at least " . $schema->minLength . " characters long", 'minLength', array(
35+
'minLength' => $schema->minLength,
36+
));
3337
}
3438

3539
// Verify a regex pattern
3640
if (isset($schema->pattern) && !preg_match('#' . str_replace('#', '\\#', $schema->pattern) . '#', $element)) {
37-
$this->addError($path, "Does not match the regex pattern " . $schema->pattern);
41+
$this->addError($path, "Does not match the regex pattern " . $schema->pattern, 'pattern', array(
42+
'pattern' => $schema->pattern,
43+
));
3844
}
3945

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

src/JsonSchema/Constraints/TypeConstraint.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function check($value = null, $schema = null, $path = null, $i = null)
8282
implode(', ', array_filter(self::$wording)))
8383
);
8484
}
85-
$this->addError($path, gettype($value) . " value found, but " . self::$wording[$type] . " is required");
85+
$this->addError($path, ucwords(gettype($value)) . " value found, but " . self::$wording[$type] . " is required", 'type');
8686
}
8787
}
8888

0 commit comments

Comments
 (0)