Laravel Data 4.x package - validation not working as expected

22 views Asked by At

I am using the popular Spatie plugin Laravel Data 4, however for some reason reason the validation isn't working quite as expected.

I am using Laravel 11.x and Laravel Data 4.x (latest versions as of 19th March 2024)

Can anyone explain why the 'awayTeamGoals' validation is set to a max of 30, however the validation is failing as i've entered a much bigger integer??

<?php

namespace App\Data;

use App\Enums\Outcomes;
use App\Enums\Platforms;
use Spatie\LaravelData\Attributes\MapName;
use Spatie\LaravelData\Data;
use Spatie\LaravelData\Mappers\SnakeCaseMapper;

#[MapName(SnakeCaseMapper::class)]
class ResultData extends Data
{
    public function __construct(
        public int $matchId,
        public int $homeTeamId,
        public int $awayTeamId,
        public int $homeTeamGoals,
        public int $awayTeamGoals,
        public int $homeTeamPlayerCount,
        public int $awayTeamPlayerCount,
        public Outcomes $outcome,
        public Platforms $platform,
        public ?string $media,
        public ?string $properties,
        public \DateTime $matchDate
    ) {
    }

    public static function rules(): array
    {
        return [
            'awayTeamGoals' => ['required', 'integer', 'min:0', 'max:30'],
        ];
    }
}

  // function where the Data is being used
        $result = new ResultData(
            1,
            231,
            545,
            0,
            11111,         // this should fail validation 'awayTeamGoals'
            2,
            4,
            Outcomes::AWAYWIN,
            Platforms::CURRENT_GEN,
            null,
            '{"clubIds": ["' . $clubId . '"]}',
            \DateTime::createFromFormat('Y-m-d H:i:s', '2022-01-01 00:00:00')
        );

dd($result->all());

0

There are 0 answers