-
Notifications
You must be signed in to change notification settings - Fork 357
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
centralize errors #364
Merged
bighappyface
merged 4 commits into
jsonrainbow:6.0.0-dev
from
shmax:centralize-error-codes
Mar 7, 2017
Merged
centralize errors #364
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
namespace JsonSchema; | ||
|
||
class ConstraintError extends \MabeEnum\Enum | ||
{ | ||
const ADDITIONAL_ITEMS = 'additionalItems'; | ||
const ADDITIONAL_PROPERTIES = 'additionalProp'; | ||
const ALL_OF = 'allOf'; | ||
const ANY_OF = 'anyOf'; | ||
const DEPENDENCIES = 'dependencies'; | ||
const DISALLOW = 'disallow'; | ||
const DIVISIBLE_BY = 'divisibleBy'; | ||
const ENUM = 'enum'; | ||
const EXCLUSIVE_MINIMUM = 'exclusiveMinimum'; | ||
const EXCLUSIVE_MAXIMUM = 'exclusiveMaximum'; | ||
const FORMAT_COLOR = 'colorFormat'; | ||
const FORMAT_DATE = 'dateFormat'; | ||
const FORMAT_DATE_TIME = 'dateTimeFormat'; | ||
const FORMAT_DATE_UTC = 'dateUtcFormat'; | ||
const FORMAT_EMAIL = 'emailFormat'; | ||
const FORMAT_HOSTNAME = 'styleHostName'; | ||
const FORMAT_IP = 'ipFormat'; | ||
const FORMAT_PHONE = 'phoneFormat'; | ||
const FORMAT_REGEX= 'regexFormat'; | ||
const FORMAT_STYLE = 'styleFormat'; | ||
const FORMAT_TIME = 'timeFormat'; | ||
const FORMAT_URL = 'urlFormat'; | ||
const LENGTH_MAX = 'maxLength'; | ||
const LENGTH_MIN = 'minLength'; | ||
const MAXIMUM = 'maximum'; | ||
const MIN_ITEMS = 'minItems'; | ||
const MINIMUM = 'minimum'; | ||
const MISSING_MAXIMUM = 'missingMaximum'; | ||
const MISSING_MINIMUM = 'missingMinimum'; | ||
const MAX_ITEMS = 'maxItems'; | ||
const MULTIPLE_OF = 'multipleOf'; | ||
const NOT = 'not'; | ||
const ONE_OF = 'oneOf'; | ||
const REQUIRED = 'required'; | ||
const REQUIRED_D3 = 'selfRequired'; | ||
const REQUIRES = 'requires'; | ||
const PATTERN = 'pattern'; | ||
const PREGEX_INVALID = 'pregrex'; | ||
const PROPERTIES_MIN = 'minProperties'; | ||
const PROPERTIES_MAX = 'maxProperties'; | ||
const TYPE = 'type'; | ||
const UNIQUE_ITEMS = 'uniqueItems'; | ||
|
||
public function getMessage() | ||
{ | ||
$name = $this->getValue(); | ||
static $messages = array( | ||
self::ADDITIONAL_ITEMS => 'The item %s[%s] is not defined and the definition does not allow additional items', | ||
self::ADDITIONAL_PROPERTIES => 'The property %s is not defined and the definition does not allow additional properties', | ||
self::ALL_OF => 'Failed to match all schemas', | ||
self::ANY_OF => 'Failed to match at least one schema', | ||
self::DEPENDENCIES => '%s depends on %s, which is missing', | ||
self::DISALLOW => 'Disallowed value was matched', | ||
self::DIVISIBLE_BY => 'Is not divisible by %d', | ||
self::ENUM => 'Does not have a value in the enumeration %s', | ||
self::EXCLUSIVE_MINIMUM => 'Must have a minimum value greater than %d', | ||
self::EXCLUSIVE_MAXIMUM => 'Must have a maximum value less than %d', | ||
self::FORMAT_COLOR => 'Invalid color', | ||
self::FORMAT_DATE => 'Invalid date %s, expected format YYYY-MM-DD', | ||
self::FORMAT_DATE_TIME => 'Invalid date-time %s, expected format YYYY-MM-DDThh:mm:ssZ or YYYY-MM-DDThh:mm:ss+hh:mm', | ||
self::FORMAT_DATE_UTC => 'Invalid time %s, expected integer of milliseconds since Epoch', | ||
self::FORMAT_EMAIL => 'Invalid email', | ||
self::FORMAT_HOSTNAME => 'Invalid hostname', | ||
self::FORMAT_IP => 'Invalid IP address', | ||
self::FORMAT_PHONE => 'Invalid phone number', | ||
self::FORMAT_REGEX=> 'Invalid regex format %s', | ||
self::FORMAT_STYLE => 'Invalid style', | ||
self::FORMAT_TIME => 'Invalid time %s, expected format hh:mm:ss', | ||
self::FORMAT_URL => 'Invalid URL format', | ||
self::LENGTH_MAX => 'Must be at most %d characters long', | ||
self::LENGTH_MIN => 'Must be at least %d characters long', | ||
self::MAX_ITEMS => 'There must be a maximum of %d items in the array', | ||
self::MAXIMUM => 'Must have a maximum value less than or equal to %d', | ||
self::MIN_ITEMS => 'There must be a minimum of %d items in the array', | ||
self::MINIMUM => 'Must have a minimum value greater than or equal to %d', | ||
self::MISSING_MAXIMUM => 'Use of exclusiveMaximum requires presence of maximum', | ||
self::MISSING_MINIMUM => 'Use of exclusiveMinimum requires presence of minimum', | ||
self::MULTIPLE_OF => 'Must be a multiple of %d', | ||
self::NOT => 'Matched a schema which it should not', | ||
self::ONE_OF => 'Failed to match exactly one schema', | ||
self::REQUIRED => 'The property %s is required', | ||
self::REQUIRED_D3 => 'Is missing and it is required', | ||
self::REQUIRES => 'The presence of the property %s requires that %s also be present', | ||
self::PATTERN => 'Does not match the regex pattern %s', | ||
self::PREGEX_INVALID => 'The pattern %s is invalid', | ||
self::PROPERTIES_MIN => 'Must contain a minimum of %d properties', | ||
self::PROPERTIES_MAX => 'Must contain no more than %d properties', | ||
self::TYPE => '%s value found, but %s is required', | ||
self::UNIQUE_ITEMS => 'There are no duplicates allowed in the array' | ||
); | ||
|
||
if (!isset($messages[$name])) { | ||
throw new InvalidArgumentException('Missing error message for ' . $name); | ||
} | ||
|
||
return $messages[$name]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
|
||
namespace JsonSchema\Constraints; | ||
|
||
use JsonSchema\ConstraintError; | ||
use JsonSchema\Entity\JsonPointer; | ||
use JsonSchema\Exception\ValidationException; | ||
|
||
|
@@ -36,23 +37,30 @@ public function __construct(Factory $factory = null) | |
$this->factory = $factory ?: new Factory(); | ||
} | ||
|
||
public function addError(JsonPointer $path = null, $message, $constraint = '', array $more = null) | ||
public function addError(ConstraintError $constraint, JsonPointer $path = null, array $more = array()) | ||
{ | ||
$message = $constraint ? $constraint->getMessage() : ''; | ||
$name = $constraint ? $constraint->getValue() : ''; | ||
$error = array( | ||
'property' => $this->convertJsonPointerIntoPropertyPath($path ?: new JsonPointer('')), | ||
'pointer' => ltrim(strval($path ?: new JsonPointer('')), '#'), | ||
'message' => $message, | ||
'constraint' => $constraint, | ||
'message' => ucfirst(vsprintf($message, array_map(function ($val) { | ||
if (is_scalar($val)) { | ||
return $val; | ||
} | ||
|
||
return json_encode($val); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like the idea of json-encoding complex types. Good idea 👍. |
||
}, array_values($more)))), | ||
'constraint' => array( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment as above - if you feed it to the constructor, then this becomes unnecessary. |
||
'name' => $name, | ||
'params' => $more | ||
) | ||
); | ||
|
||
if ($this->factory->getConfig(Constraint::CHECK_MODE_EXCEPTIONS)) { | ||
throw new ValidationException(sprintf('Error validating %s: %s', $error['pointer'], $error['message'])); | ||
} | ||
|
||
if (is_array($more) && count($more) > 0) { | ||
$error += $more; | ||
} | ||
|
||
$this->errors[] = $error; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May we please also have a
setMessage
method that can add / overwrite messages, and store the messages on the class rather than insidegetMessage
? This would be useful for local translations and people's custom errors that never make it as far as upstream.Would be good if this could accept either a single error message, or an array of them to allow setting multiple errors at once. e.g.:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think @erayd and I already came to an understanding on this, but just for those following along at home
ErrorConstraint
is not a localization interface. It's just a central repository for the natural language strings that we already had floating around in the code. Its only relevance to localization is as a reference.