-
Notifications
You must be signed in to change notification settings - Fork 109
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
Documentation about validation of relationships object using getResourceObject #2
Comments
Thanks for your message. I need to sit down and write some proper documentation at some point but unfortunately the project that I've created this package for is burning all of my time at the moment. Better documentation will come at some point! What you're currently doing above works, but there's handy helper methods that make it easier to write. What you've written above can be rewritten as: $validator = $this->getResourceObjectValidator(...)
->hasOne('relationshipName', 'relationshipType'); You can also use Both Both these helper methods take an optional third argument of options (in an array) that can be set on either the $validator = $this->getResourceObjectValidator(...)
->hasOne('relationshipName', 'relationshipType', [
'required' => true,
'allowEmpty' => false,
'callback' => function (ResourceIdentifierInterface $identifier) {
return Person::where('id', $identifier->getId())->exists();
}
]);
If provided, the callback is invoked by the validator to check that the actual id provided by the client is valid. This callback is only called if all other validation on the relationship passes - i.e. you can trust that For a has-many validator, the callback is provided with an instance of Let me know if you have any questions! I'm going to leave this issue open as a reminder that I need to add better documentation for validation. |
Thanks a bunch, that really cleared up everything I was missing on validation. |
howdy @lindyhopchris I've been checking out the relationship management with creating/updating a resource and I've stumbled upon this issue...I have a few questions I hope you can help me with:
Thank you so much in advance. |
No problem...
|
@lindyhopchris excellent!! tyvm sir 😃 |
Hi @lindyhopchris ! I have the same question, how can I validate relationships object's data? |
Hi @mandryka The relationship validation checks that the relationship object is correct according to the JSON API spec, and it also checks via the store that the related resource exists. Additionally it checks If you need to validate the actual relationship identifier that was sent, pass a Alternatively you can pass an instance of If you're still stuck or can't figure out how to do what you're trying to achieve, then create a new issue with an explanation of what you're trying to validate and I'll be able to help. |
@lindyhopchris thanks a lot! |
Might be able to close this one, since it seems like everything has been rewritten 😄 |
Yeah, good point. |
Hey there,
Forgive me if I read incorrectly, but I don't think there's any documentation about validating relationships as part of a new resource creation, such as the request body shown in the example under http://jsonapi.org/format/#document-resource-objects
The current solution I'm using is to manually construct the relationships validator and set it on the main resource validator. eg.
Although I'm not 100% sure that this is the recommended method.
If you would like me to submit a change to README to that effect under content validation, I'm happy to do that too.
The text was updated successfully, but these errors were encountered: