Comments (10)
@lindyhopchris thanks a lot!
I created AcceptRelatedEntity
class, which implements AcceptRelatedResourceInterface
and added validation for related entity to accept(...)
. Also I used ErrorCreatorTrait
for adding errors.
from laravel-json-api.
Might be able to close this one, since it seems like everything has been rewritten 😄
from laravel-json-api.
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 hasMany()
as a helper for adding a hasMany
relationship. These helper methods are chainable, so you you call many in a row.
Both hasOne()
and hasMany()
take the relationship name as the first argument and a relationship type for the second. If your relationship can accept multiple types (i.e. it's polymorphic) then pass in an array of types for the second argument.
Both these helper methods take an optional third argument of options (in an array) that can be set on either the HasOneValidator
or HasManyValidator
that is created. E.g. you could do the following:
$validator = $this->getResourceObjectValidator(...)
->hasOne('relationshipName', 'relationshipType', [
'required' => true,
'allowEmpty' => false,
'callback' => function (ResourceIdentifierInterface $identifier) {
return Person::where('id', $identifier->getId())->exists();
}
]);
required
is whether the client must have provided the relationship in the resource object that was sent. allowEmpty
is whether the relationship can be empty (null
for has-one, empty array for has-many).
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 $identifier
passed in to the callback has the correct resource type on it and there is an id value.
For a has-many validator, the callback is provided with an instance of ResourceIdentifierCollectionInterface
.
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.
from laravel-json-api.
Thanks a bunch, that really cleared up everything I was missing on validation.
from laravel-json-api.
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:
- Could you please give me an example of what would a real life
relationshipName
andrelationshipType
look like? I'm having trouble distinguishing them (especially therelationshipType
). - When not sending the optional third argument array to the methods
hasOne()
andhasMany()
, what are the default values for required, allowEmpty and callback? - Related to the previous question (third argument array), is it possible to only send a subset of its vaules? e.g. Send only the
required
and have the rest as default? What would each value default to?
Thank you so much in advance.
from laravel-json-api.
No problem...
$relationshipName
is the key of the relationship on the resource object that is being validated.$relationshipType
is the type of the resource on the inverse side of the relationship (I probably should have called the variable$resourceType
). E.g. if you are validating apost
resource object that has anauthor
relationship, where the author is aperson
resource object, then$relationshipName
would beauthor
and$relationshipType
would beperson
.allowEmpty
istrue
,required
isfalse
.callback
isnull
, i.e. no callback to execute in the validation. I use the callback to check that the provided id for the relationship actually exists (normally a database query).- Yes. All options arrays are exchanged in a way so that only values that are provided are set over the defaults. E.g.
https://github.com/cloudcreativity/json-api/blob/master/src/Validator/Relationships/HasOneValidator.php#L199
from laravel-json-api.
@lindyhopchris excellent!! tyvm sir 😃
from laravel-json-api.
Hi @lindyhopchris !
I have the same question, how can I validate relationships object's data?
I tried to use $relationships->hasOne('key', 'type', true, false) in relationshipRules method, but I think that this method just for checking exist of object.
How can I add some validation rules to this object?
from laravel-json-api.
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 allowEmpty
and required
depending on the settings.
If you need to validate the actual relationship identifier that was sent, pass a Closure
for the next argument as per here:
https://github.com/cloudcreativity/json-api/blob/master/src/Contracts/Validators/RelationshipsValidatorInterface.php#L55-L56
Alternatively you can pass an instance of AcceptRelatedResourceInterface
which would allow you to use dependency injection within that instance if you resolve it out of the container.
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.
from laravel-json-api.
Yeah, good point.
from laravel-json-api.
Related Issues (20)
- Pagination In Custom Method HOT 1
- Unable to create json api resource with attribute name "type" HOT 3
- Self-relation doesn't include model fields HOT 5
- Arguement 3 passed to controller must be an instance of model, string given
- Support for multiple operations in a single request HOT 2
- use Errors Handling in Laravel 8 HOT 1
- Why does json-api trimming spaces in my attributes when updating? HOT 3
- filter slug and underscore HOT 1
- Multiple primary keys - Mysql Error HOT 1
- Sparse fieldsets not working with included relationships HOT 3
- How to attach a middleware to a specific resource relationship? HOT 3
- How I can filter included resources by their fields HOT 8
- Migrate from cloudcreativity/laravel-json-api to laravel-json-api/laravel HOT 15
- How to stop this library from making asterix queries? HOT 1
- Make a request to one endpoint from another controller HOT 5
- Insert on duplicate key upd when inserting a record? HOT 6
- Handle HasMany relationship with extra field. HOT 2
- [5.x] Missing meta HOT 8
- bug: service provider boot throws "No application encryption key has been specified." HOT 1
- Error in tests HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laravel-json-api.