Skip to content

Commit

Permalink
Introduce various validation rule attributes
Browse files Browse the repository at this point in the history
Added new validation rule attributes including ascii, decimal, gte
(greater than or equal), lte (less than or equal), mime type, missing,
and present. These attributes enhance the robustness and specificity
of data validation across the application.
  • Loading branch information
faustbrian committed Dec 11, 2023
1 parent 0d6c883 commit df78be5
Show file tree
Hide file tree
Showing 16 changed files with 473 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/Attributes/Validation/Ascii.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Spatie\LaravelData\Attributes\Validation;

use Attribute;

#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class Ascii extends StringValidationAttribute
{
public static function keyword(): string
{
return 'ascii';
}

public function parameters(): array
{
return [];
}
}
24 changes: 24 additions & 0 deletions src/Attributes/Validation/Decimal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Spatie\LaravelData\Attributes\Validation;

use Attribute;
use Spatie\LaravelData\Support\Validation\References\RouteParameterReference;

#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class Decimal extends StringValidationAttribute
{
public function __construct(protected int|float|RouteParameterReference $min, protected null|int|float|RouteParameterReference $max)
{
}

public static function keyword(): string
{
return 'decimal';
}

public function parameters(): array
{
return array_filter([$this->min, $this->max]);
}
}
29 changes: 29 additions & 0 deletions src/Attributes/Validation/GreaterThanOrEqual.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Spatie\LaravelData\Attributes\Validation;

use Attribute;
use Spatie\LaravelData\Support\Validation\References\FieldReference;

#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class GreaterThanOrEqual extends StringValidationAttribute
{
protected FieldReference $field;

public function __construct(
string|FieldReference $field,
) {
$this->field = $this->parseFieldReference($field);
}


public static function keyword(): string
{
return 'gte';
}

public function parameters(): array
{
return [$this->field];
}
}
29 changes: 29 additions & 0 deletions src/Attributes/Validation/LessThanOrEqual.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Spatie\LaravelData\Attributes\Validation;

use Attribute;
use Spatie\LaravelData\Support\Validation\References\FieldReference;

#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class LessThanOrEqual extends StringValidationAttribute
{
protected FieldReference $field;

public function __construct(
string|FieldReference $field,
) {
$this->field = $this->parseFieldReference($field);
}


public static function keyword(): string
{
return 'lte';
}

public function parameters(): array
{
return [$this->field];
}
}
25 changes: 25 additions & 0 deletions src/Attributes/Validation/MimeTypeByFileExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Spatie\LaravelData\Attributes\Validation;

use Attribute;
use Illuminate\Support\Arr;

#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class MimeTypeByFileExtension extends StringValidationAttribute
{
public function __construct(
private readonly array|string $types,
) {
}

public static function keyword(): string
{
return 'mimes';
}

public function parameters(): array
{
return Arr::wrap($this->types);
}
}
19 changes: 19 additions & 0 deletions src/Attributes/Validation/Missing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Spatie\LaravelData\Attributes\Validation;

use Attribute;

#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class Missing extends StringValidationAttribute
{
public static function keyword(): string
{
return 'missing';
}

public function parameters(): array
{
return [];
}
}
39 changes: 39 additions & 0 deletions src/Attributes/Validation/MissingIf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Spatie\LaravelData\Attributes\Validation;

use Attribute;
use BackedEnum;
use Illuminate\Support\Arr;
use Spatie\LaravelData\Support\Validation\References\FieldReference;
use Spatie\LaravelData\Support\Validation\References\RouteParameterReference;
use Spatie\LaravelData\Support\Validation\RequiringRule;

#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class MissingIf extends StringValidationAttribute implements RequiringRule
{
protected FieldReference $field;

protected string|array $values;

public function __construct(
string|FieldReference $field,
array|string|BackedEnum|RouteParameterReference ...$values
) {
$this->field = $this->parseFieldReference($field);
$this->values = Arr::flatten($values);
}

public static function keyword(): string
{
return 'missing_if';
}

public function parameters(): array
{
return [
$this->field,
$this->values,
];
}
}
40 changes: 40 additions & 0 deletions src/Attributes/Validation/MissingUnless.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Spatie\LaravelData\Attributes\Validation;

use Attribute;
use BackedEnum;
use Illuminate\Support\Arr;
use Spatie\LaravelData\Support\Validation\References\FieldReference;
use Spatie\LaravelData\Support\Validation\References\RouteParameterReference;
use Spatie\LaravelData\Support\Validation\RequiringRule;

#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class MissingUnless extends StringValidationAttribute implements RequiringRule
{
protected FieldReference $field;

protected string|array $values;

public function __construct(
string|FieldReference $field,
null|array|string|BackedEnum|RouteParameterReference ...$values
) {
$this->field = $this->parseFieldReference($field);
$this->values = Arr::flatten($values);
}


public static function keyword(): string
{
return 'missing_unless';
}

public function parameters(): array
{
return [
$this->field,
$this->values,
];
}
}
33 changes: 33 additions & 0 deletions src/Attributes/Validation/MissingWith.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Spatie\LaravelData\Attributes\Validation;

use Attribute;
use Illuminate\Support\Arr;
use Spatie\LaravelData\Support\Validation\References\FieldReference;
use Spatie\LaravelData\Support\Validation\RequiringRule;

#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class MissingWith extends StringValidationAttribute implements RequiringRule
{
protected array $fields;

public function __construct(array|string|FieldReference ...$fields)
{
foreach (Arr::flatten($fields) as $field) {
$this->fields[] = $field instanceof FieldReference ? $field : new FieldReference($field);
}
}

public static function keyword(): string
{
return 'missing_with';
}

public function parameters(): array
{
return [
$this->fields,
];
}
}
33 changes: 33 additions & 0 deletions src/Attributes/Validation/MissingWithAll.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Spatie\LaravelData\Attributes\Validation;

use Attribute;
use Illuminate\Support\Arr;
use Spatie\LaravelData\Support\Validation\References\FieldReference;
use Spatie\LaravelData\Support\Validation\RequiringRule;

#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class MissingWithAll extends StringValidationAttribute implements RequiringRule
{
protected array $fields;

public function __construct(array|string|FieldReference ...$fields)
{
foreach (Arr::flatten($fields) as $field) {
$this->fields[] = $field instanceof FieldReference ? $field : new FieldReference($field);
}
}

public static function keyword(): string
{
return 'missing_with_all';
}

public function parameters(): array
{
return [
$this->fields,
];
}
}
19 changes: 19 additions & 0 deletions src/Attributes/Validation/Negative.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Spatie\LaravelData\Attributes\Validation;

use Attribute;

#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class Negative extends StringValidationAttribute
{
public static function keyword(): string
{
return 'lt';
}

public function parameters(): array
{
return [0];
}
}
19 changes: 19 additions & 0 deletions src/Attributes/Validation/Positive.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Spatie\LaravelData\Attributes\Validation;

use Attribute;

#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class Positive extends StringValidationAttribute
{
public static function keyword(): string
{
return 'gt';
}

public function parameters(): array
{
return [0];
}
}
39 changes: 39 additions & 0 deletions src/Attributes/Validation/PresentIf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Spatie\LaravelData\Attributes\Validation;

use Attribute;
use BackedEnum;
use Illuminate\Support\Arr;
use Spatie\LaravelData\Support\Validation\References\FieldReference;
use Spatie\LaravelData\Support\Validation\References\RouteParameterReference;
use Spatie\LaravelData\Support\Validation\RequiringRule;

#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class PresentIf extends StringValidationAttribute implements RequiringRule
{
protected FieldReference $field;

protected string|array $values;

public function __construct(
string|FieldReference $field,
array|string|BackedEnum|RouteParameterReference ...$values
) {
$this->field = $this->parseFieldReference($field);
$this->values = Arr::flatten($values);
}

public static function keyword(): string
{
return 'present_if';
}

public function parameters(): array
{
return [
$this->field,
$this->values,
];
}
}
Loading

0 comments on commit df78be5

Please sign in to comment.