Skip to content

Add property name in draft-3 required error #430

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 7 commits into from
Closed

Add property name in draft-3 required error #430

wants to merge 7 commits into from

Conversation

sunspikes
Copy link
Contributor

@sunspikes sunspikes commented Jun 2, 2017

In case of required error with draft-3, the error message is missing property names. (Similar to #91)

Creating a new PR as mentioned in #428 against 6.0.0-dev

@krishnaprasadmg
Copy link

I guess #429 need to be merged for the tests to succeed in HHVM

@@ -93,7 +93,7 @@ public function getMessage()
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::REQUIRED_D3 => 'The property %s is required',
Copy link
Contributor

Choose a reason for hiding this comment

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

This line is redundant - there's already an identical message template on line 95 (REQUIRED), so IMO it makes more sense to just use that.

@@ -144,7 +144,8 @@ protected function validateCommonProperties(&$value, $schema = null, JsonPointer
} elseif (isset($schema->required) && !is_array($schema->required)) {
// Draft 3 - Required attribute - e.g. "foo": {"type": "string", "required": true}
if ($schema->required && $value instanceof self) {
$this->addError(ConstraintError::REQUIRED_D3(), $path);
$propertyName = current($path->getPropertyPaths());
Copy link
Contributor

Choose a reason for hiding this comment

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

Please use end() rather than current() - current() makes assumptions about the state of the internal pointer on the returned array, which could result in hard-to-find bugs if the pointer code is changed at a later date.

@erayd
Copy link
Contributor

erayd commented Jun 2, 2017

I guess #429 need to be merged for the tests to succeed in HHVM.

Yep. I'm not seeing any show-stoppers though; there's nothing in your PR that will cause HHVM to fail.

Copy link
Contributor

@erayd erayd left a comment

Choose a reason for hiding this comment

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

Looks great - thanks for fixing this 👍

@@ -144,7 +144,8 @@ protected function validateCommonProperties(&$value, $schema = null, JsonPointer
} elseif (isset($schema->required) && !is_array($schema->required)) {
// Draft 3 - Required attribute - e.g. "foo": {"type": "string", "required": true}
if ($schema->required && $value instanceof self) {
$this->addError(ConstraintError::REQUIRED_D3(), $path);
$propertyName = end(array_values($path->getPropertyPaths()));
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are you using array_values here? Unless I'm missing something, calling array_values will do absolutely nothing in this situation.

Copy link
Contributor

@erayd erayd Jun 2, 2017

Choose a reason for hiding this comment

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

Are you using it as a shortcut to solve end() wanting a reference?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I did it to avoid an intermediate variable. But unfortunately it throws a warning saying 'only variables can be passed to end as argument'

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, end() and friends are a bit annoying that way, although I can understand why it's required - sometimes you need to modify the referenced element, which can't be done without a reference. Would be nice if there was a handy way to refer to the last element without requiring this kind of shenanigans, but sadly PHP doesn't provide that functionality.

You definitely get points for creativity - using array_values to resolve it is certainly thinking outside the box! The intent is much less obvious than just using the intermediate variable though. The extra line is annoying, but IMO worth it for making the code obvious to whoever else reads it.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's days like these when I wish PHP had proper pointers (as per C).

Choose a reason for hiding this comment

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

🙂 well said, thanks!

Copy link
Contributor

@erayd erayd left a comment

Choose a reason for hiding this comment

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

Looks good - thanks 👍

@sunspikes
Copy link
Contributor Author

@erayd thank you so much for your quick feedbacks!

@erayd
Copy link
Contributor

erayd commented Jun 2, 2017

No worries - thanks for your contributions!

@erayd
Copy link
Contributor

erayd commented Jun 2, 2017

@bighappyface Travis failure for HHVM can be ignored for this PR; it's caused by Travis removing HHVM support from their default stable platform. The code is fine, and all PHP versions pass OK.

The platform issue is addressed in #429.

This was referenced Jun 5, 2017
@erayd
Copy link
Contributor

erayd commented Jun 5, 2017

#429 has been merged, so this PR will now pass Travis after being rebased.

@sunspikes - could you please rebase this, as other PRs (in particular #429) have been merged since you opened it.

@sunspikes
Copy link
Contributor Author

@bighappyface Unfortunately i deleted my branch :(

Anyway i have created a new PR against the updated master #432

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

Successfully merging this pull request may close these issues.

4 participants