Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 29 additions & 18 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,52 @@ env:
jobs:
lint_test:
name: Lint & Test
runs-on: nixos
runs-on: blacksmith-4vcpu-ubuntu-2404
strategy:
matrix:
reqs:
- symfony: "^5"
shell: "php81"
php: "8.1"
- symfony: "^5"
shell: "php82"
php: "8.2"
- symfony: "^5"
shell: "php83"
php: "8.3"
- symfony: "^6"
shell: "php81"
php: "8.1"
- symfony: "^6"
shell: "php82"
php: "8.2"
- symfony: "^6"
shell: "php83"
php: "8.3"
- symfony: "^7"
shell: "php82"
php: "8.2"
- symfony: "^7"
shell: "php83"
php: "8.3"
- symfony: "^6"
shell: "php84"
php: "8.4"
- symfony: "^7"
shell: "php84"
defaults:
run:
shell: nix develop .#${{ matrix.reqs.shell }} --command bash {0}
php: "8.4"
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.reqs.php }}
tools: composer
coverage: none
env:
runner: "blacksmith"

- name: Get composer cache directory
id: composer-cache
run: echo "dir=$(composer config cache-files-dir)" >> "$GITHUB_OUTPUT"

- name: Cache Composer dependencies
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: /tmp/composer-cache
key: ${{ runner.os }}-${{ hashFiles('**/composer.lock') }}
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-php${{ matrix.reqs.php }}-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-php${{ matrix.reqs.php }}-

- name: Install dependencies
run: >-
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"phpcs": "phpcs -s src tests"
},
"require": {
"justinrainbow/json-schema": "^5.2",
"justinrainbow/json-schema": "^5.2 || ^6",
"symfony/http-foundation": "^4 || ^5 || ^6 || ^7",
"wunderwerkio/jsonapi-error": "^1.0"
},
Expand Down
10 changes: 6 additions & 4 deletions src/HttpApiValidationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ trait HttpApiValidationTrait {
* The data structure to validate.
* @param array<string, mixed>|object $schema
* The JSON schema to validate against.
* @param int|null $checkMode
* @param int<0, 1023>|null $checkMode
* The check mode to use, see \JsonSchema\Validator::CHECK_MODE_*.
*
* @return \Wunderwerk\HttpApiUtils\Validation\ValidationResult
Expand All @@ -46,7 +46,7 @@ protected function validateDataStructure(object &$data, array|object $schema, ?i
* The JSON string to validate.
* @param array<string, mixed>|object $schema
* The JSON schema to validate against.
* @param int|null $checkMode
* @param int<0, 1023>|null $checkMode
* The check mode to use, see \JsonSchema\Validator::CHECK_MODE_*.
*
* @return \Wunderwerk\HttpApiUtils\Validation\ValidationResult
Expand All @@ -65,14 +65,16 @@ protected function validateJsonString(string $payload, array|object $schema, ?in
* The JSON object to validate.
* @param array<string, mixed>|object $schema
* The JSON schema to validate against.
* @param int|null $checkMode
* @param int<0, 1023>|null $checkMode
* The check mode to use, see \JsonSchema\Validator::CHECK_MODE_*.
*
* @return \Wunderwerk\HttpApiUtils\Validation\ValidationResult
* The validation result.
*/
protected function validateArray(mixed $payload, array|object $schema, ?int $checkMode = NULL) {
$data = $this->getValidator()->arrayToObjectRecursive($payload);
$data = is_array($payload)
? $this->getValidator()->arrayToObjectRecursive($payload)
: (object) [];

return $this->validateDataStructure($data, $schema, $checkMode);
}
Expand Down
11 changes: 8 additions & 3 deletions src/Validation/ValidationResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ValidationResult {
/**
* Construct new validation result.
*
* @param array{property: string, pointer: string, message: string, constraint: string, pattern?: string}[] $errors
* @param array{property: string, pointer: string, message: string, constraint: string|array{name: string, params: array<mixed>}, pattern?: string}[] $errors
* Array of validation errors.
*/
public function __construct(
Expand All @@ -39,7 +39,7 @@ public function isValid(): bool {
/**
* Get the validation errors.
*
* @return array{property: string, pointer: string, message: string, constraint: string, pattern?: string}[]
* @return array{property: string, pointer: string, message: string, constraint: string|array{name: string, params: array<mixed>}, pattern?: string}[]
* The validation errors.
*/
public function getErrors(): array {
Expand All @@ -65,6 +65,11 @@ public function getResponse(
$errors = [];

foreach ($this->getErrors() as $error) {
$constraint = $error['constraint'];
if (is_array($constraint)) {
$constraint = $constraint['name'];
}

$errors[] = new JsonApiError(
status: 422,
code: 'validation_error',
Expand All @@ -74,7 +79,7 @@ public function getResponse(
'pointer' => $error['pointer'],
],
meta: [
'constraint' => $error['constraint'],
'constraint' => $constraint,
],
);
}
Expand Down