@@ -121,6 +121,16 @@ abstract class AbstractValidators implements ValidatorFactoryInterface
121
121
*/
122
122
protected $ allowedFieldSets = null ;
123
123
124
+ /**
125
+ * Whether existing resource attributes should be validated on for an update.
126
+ *
127
+ * If this is set to false, the validator instance will not be provided with the
128
+ * resource's existing attribute values when validating an update (PATCH) request.
129
+ *
130
+ * @var bool
131
+ */
132
+ protected $ validateExisting = true ;
133
+
124
134
/**
125
135
* @var Factory
126
136
*/
@@ -303,7 +313,7 @@ protected function createData(array $document): array
303
313
* > attributes as null values.
304
314
*
305
315
* So that the validator has access to the current values of attributes, we
306
- * merge to provided new attributes over the top of the existing attribute
316
+ * merge attributes provided by the client over the top of the existing attribute
307
317
* values.
308
318
*
309
319
* @param mixed $record
@@ -316,14 +326,38 @@ protected function updateData($record, array $document): array
316
326
{
317
327
$ resource = $ document ['data ' ];
318
328
319
- $ resource ['attributes ' ] = $ this ->extractAttributes (
320
- $ record ,
321
- isset ($ resource ['attributes ' ]) ? $ resource ['attributes ' ] : []
322
- )->all ();
329
+ if ($ this ->mustValidateExisting ($ record , $ document )) {
330
+ $ resource ['attributes ' ] = $ this ->extractAttributes (
331
+ $ record ,
332
+ $ resource ['attributes ' ] ?? []
333
+ )->all ();
334
+
335
+ $ resource ['relationships ' ] = $ this ->extractRelationships (
336
+ $ record ,
337
+ $ resource ['relationships ' ] ?? []
338
+ )->all ();
339
+ }
323
340
324
341
return $ resource ;
325
342
}
326
343
344
+ /**
345
+ * Should existing resource values be provided to the validator for an update request?
346
+ *
347
+ * Child classes can overload this method if they need to programmatically work out
348
+ * if existing values must be provided to the validator instance for an update request.
349
+ *
350
+ * @param mixed $record
351
+ * the record being updated
352
+ * @param array $document
353
+ * the JSON API document provided by the client.
354
+ * @return bool
355
+ */
356
+ protected function mustValidateExisting ($ record , array $ document ): bool
357
+ {
358
+ return false !== $ this ->validateExisting ;
359
+ }
360
+
327
361
/**
328
362
* Extract attributes for a resource update.
329
363
*
@@ -333,20 +367,47 @@ protected function updateData($record, array $document): array
333
367
*/
334
368
protected function extractAttributes ($ record , array $ new ): Collection
335
369
{
336
- return $ this ->existingValues ($ record )->merge ($ new );
370
+ return $ this ->existingAttributes ($ record )->merge ($ new );
337
371
}
338
372
339
373
/**
340
- * Get any existing values for the provided record.
374
+ * Get any existing attributes for the provided record.
341
375
*
342
376
* @param $record
343
377
* @return Collection
344
378
*/
345
- protected function existingValues ($ record ): Collection
379
+ protected function existingAttributes ($ record ): Collection
346
380
{
347
381
return collect ($ this ->container ->getSchema ($ record )->getAttributes ($ record ));
348
382
}
349
383
384
+ /**
385
+ * Extract relationships for a resource update.
386
+ *
387
+ * @param $record
388
+ * @param array $new
389
+ * @return Collection
390
+ */
391
+ protected function extractRelationships ($ record , array $ new ): Collection
392
+ {
393
+ return $ this ->existingRelationships ($ record )->merge ($ new );
394
+ }
395
+
396
+ /**
397
+ * Get any existing relationships for the provided record.
398
+ *
399
+ * As there is no reliable way for us to work this out (as we do not
400
+ * know the relationship keys), child classes should overload this method
401
+ * to add existing relationship data.
402
+ *
403
+ * @param $record
404
+ * @return Collection
405
+ */
406
+ protected function existingRelationships ($ record ): Collection
407
+ {
408
+ return collect ();
409
+ }
410
+
350
411
/**
351
412
* Get validation data for modifying a relationship.
352
413
*
0 commit comments