|
3 | 3 | namespace Wikibase\Repo\RestApi\Infrastructure; |
4 | 4 |
|
5 | 5 | use Swaggest\JsonDiff\Exception; |
| 6 | +use Swaggest\JsonDiff\InvalidFieldTypeException; |
6 | 7 | use Swaggest\JsonDiff\JsonPatch; |
7 | 8 | use Swaggest\JsonDiff\MissingFieldException; |
8 | 9 | use Swaggest\JsonDiff\UnknownOperationException; |
|
15 | 16 | class JsonDiffJsonPatchValidator implements JsonPatchValidator { |
16 | 17 |
|
17 | 18 | public function validate( array $patch ): ?ValidationError { |
18 | | - // TODO: remove foreach checks when upstream PR merged |
19 | | - // https://github.com/swaggest/json-diff/pull/60 |
20 | | - foreach ( $patch as $operation ) { |
21 | | - if ( !is_array( $operation ) ) { |
22 | | - return new ValidationError( self::CODE_INVALID ); |
23 | | - } |
24 | | - if ( array_key_exists( 'op', $operation ) && !is_string( $operation['op'] ) ) { |
25 | | - return new ValidationError( |
26 | | - self::CODE_INVALID_FIELD_TYPE, |
27 | | - [ self::ERROR_CONTEXT_OPERATION => $operation, self::ERROR_CONTEXT_FIELD => 'op' ] |
28 | | - ); |
29 | | - } |
30 | | - if ( array_key_exists( 'path', $operation ) && !is_string( $operation['path'] ) ) { |
31 | | - return new ValidationError( |
32 | | - self::CODE_INVALID_FIELD_TYPE, |
33 | | - [ self::ERROR_CONTEXT_OPERATION => $operation, self::ERROR_CONTEXT_FIELD => 'path' ] |
34 | | - ); |
35 | | - } |
36 | | - if ( array_key_exists( 'from', $operation ) && !is_string( $operation['from'] ) ) { |
37 | | - return new ValidationError( |
38 | | - self::CODE_INVALID_FIELD_TYPE, |
39 | | - [ self::ERROR_CONTEXT_OPERATION => $operation, self::ERROR_CONTEXT_FIELD => 'from' ] |
40 | | - ); |
41 | | - } |
42 | | - } |
43 | | - |
44 | 19 | try { |
45 | 20 | JsonPatch::import( $patch ); |
46 | 21 | } catch ( MissingFieldException $e ) { |
47 | 22 | return new ValidationError( |
48 | 23 | self::CODE_MISSING_FIELD, |
49 | 24 | [ self::ERROR_CONTEXT_OPERATION => (array)$e->getOperation(), self::ERROR_CONTEXT_FIELD => $e->getMissingField() ] |
50 | 25 | ); |
| 26 | + } catch ( InvalidFieldTypeException $e ) { |
| 27 | + return new ValidationError( |
| 28 | + self::CODE_INVALID_FIELD_TYPE, |
| 29 | + [ self::ERROR_CONTEXT_OPERATION => (array)$e->getOperation(), self::ERROR_CONTEXT_FIELD => $e->getField() ] |
| 30 | + ); |
51 | 31 | } catch ( UnknownOperationException $e ) { |
52 | 32 | return new ValidationError( |
53 | 33 | self::CODE_INVALID_OPERATION, |
|
0 commit comments