Skip to content

Commit

Permalink
Merge pull request #77 from programmatordev/1.x
Browse files Browse the repository at this point in the history
v1.3.0
  • Loading branch information
andrepimpao authored Aug 14, 2024
2 parents 74fdbd7 + 8c50eec commit 6d3760a
Show file tree
Hide file tree
Showing 102 changed files with 1,058 additions and 397 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ $validator = Validator::type('int')->greaterThanOrEqual(18);

// and validate with these:
$validator->validate(16); // returns bool: false
$validator->assert(16, 'age'); // throws exception: The age value should be greater than or equal to 18, 16 given.
$validator->assert(16, 'age'); // throws exception: The age value should be greater than or equal to 18.
```

## Documentation
Expand Down
2 changes: 1 addition & 1 deletion docs/01-get-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ $validator = Validator::type('int')->greaterThanOrEqual(18);

// and validate with these:
$validator->validate(16); // returns bool: false
$validator->assert(16, 'age'); // throws exception: The age value should be greater than or equal to 18, 16 given.
$validator->assert(16, 'age'); // throws exception: The age value should be greater than or equal to 18.
```
5 changes: 5 additions & 0 deletions docs/03-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@

- [Blank](03-rules_blank.md)
- [Count](03-rules_count.md)
- [IsFalse](03-rules_is-false.md)
- [IsNull](03-rules_is-null.md)
- [IsTrue](03-rules_is-true.md)
- [NotBlank](03-rules_not-blank.md)
- [NotNull](03-rules_not-null.md)
- [Type](03-rules_type.md)

## String Rules
Expand Down Expand Up @@ -52,4 +56,5 @@

## Other Rules

- [AtLeastOneOf](03-rules_at-least-one-of.md)
- [Optional](03-rules_optional.md)
65 changes: 65 additions & 0 deletions docs/03-rules_at-least-one-of.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# AtLeastOneOf

Checks that the value satisfies at least one of the given constraints.

```php
/** Validator[] $constraints */
Choice(
array $constraints,
?string $message = null
);
```

## Basic Usage

```php
Validator::atLeastOneOf([
Validator::isFalse(),
Validator::type('int')->greaterThanOrEqual(18)
])->validate(false); // true

Validator::atLeastOneOf([
Validator::isFalse(),
Validator::type('int')->greaterThanOrEqual(18)
])->validate(20); // true

Validator::atLeastOneOf([
Validator::isFalse(),
Validator::type('int')->greaterThanOrEqual(18)
])->validate(true); // false

Validator::atLeastOneOf([
Validator::isFalse(),
Validator::type('int')->greaterThanOrEqual(18)
])->validate(16); // false
```

> [!NOTE]
> An `UnexpectedValueException` will be thrown when a value in the `constraints` array is not an instance of `Validator`.
## Options

### `constraints`

type: `array<int, Validator>` `required`

Collection of constraints to be validated against the input value.
If at least one given constraint is valid, the validation is considered successful.

### `message`

type: `?string` default: `The {{ name }} value should satisfy at least one of the following constraints: {{ messages }}`

Message that will be shown if all given constraints are not valid.

The following parameters are available:

| Parameter | Description |
|------------------|-------------------------------------------------|
| `{{ value }}` | The current invalid value |
| `{{ name }}` | Name of the invalid value |
| `{{ messages }}` | List of error messages based on the constraints |

## Changelog

- `1.3.0` Created
2 changes: 1 addition & 1 deletion docs/03-rules_blank.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Validator::blank(normalizer: fn($value) => trim($value))->validate(' '); // true

### `message`

type: `?string` default: `The {{ name }} value should be blank, {{ value }} given.`
type: `?string` default: `The {{ name }} value should be blank.`

Message that will be shown if the value is not blank.

Expand Down
11 changes: 4 additions & 7 deletions docs/03-rules_choice.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ Validator::choice(['red', 'green', 'blue'], multiple: true, min: 2, max: 3)->val
> [!NOTE]
> An `UnexpectedValueException` will be thrown when `multiple` is `true` and the input value is not an `array`.
> [!NOTE]
> An `UnexpectedValueException` will be thrown when the `min` value is greater than or equal to the `max` value.
## Options

### `constraints`
Expand Down Expand Up @@ -78,7 +75,7 @@ For example, if `max` is 2, the input array must have at most 2 values.

### `message`

type: `?string` default: `The {{ name }} value is not a valid choice, {{ value }} given. Accepted values are: {{ constraints }}.`
type: `?string` default: `The {{ name }} value is not a valid choice. Accepted values are: {{ constraints }}.`

Message that will be shown if input value is not a valid choice.

Expand All @@ -92,7 +89,7 @@ The following parameters are available:

### `multipleMessage`

type: `?string` default: `The {{ name }} value has one or more invalid choices, {{ value }} given. Accepted values are: {{ constraints }}.`
type: `?string` default: `The {{ name }} value has one or more invalid choices. Accepted values are: {{ constraints }}.`

Message that will be shown when `multiple` is `true` and at least one of the input array values is not a valid choice.

Expand All @@ -106,7 +103,7 @@ The following parameters are available:

### `minMessage`

type: `?string` default: `The {{ name }} value must have at least {{ min }} choices, {{ numElements }} choices given.`
type: `?string` default: `The {{ name }} value must have at least {{ min }} choice(s).`

Message that will be shown when `multiple` is `true` and input array has fewer values than the defined in `min`.

Expand All @@ -123,7 +120,7 @@ The following parameters are available:

### `maxMessage`

type: `?string` default: `The {{ name }} value must have at most {{ max }} choices, {{ numElements }} choices given.`
type: `?string` default: `The {{ name }} value must have at most {{ max }} choice(s).`

Message that will be shown when `multiple` is `true` and input array has more values than the defined in `max`.

Expand Down
9 changes: 3 additions & 6 deletions docs/03-rules_count.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ Validator::count(min: 3, max: 3)->validate(['a', 'b', 'c']); // true
> [!NOTE]
> An `UnexpectedValueException` will be thrown when either `min` or `max` options are not given.
> [!NOTE]
> An `UnexpectedValueException` will be thrown when the `min` value is greater than the `max` value.
> [!NOTE]
> An `UnexpectedValueException` will be thrown when the input value is not an `array` or an object implementing `\Countable`.
Expand All @@ -54,7 +51,7 @@ It defines the maximum number of elements required.

### `minMessage`

type: `?string` default: `The {{ name }} value should contain {{ min }} elements or more, {{ numElements }} elements given.`
type: `?string` default: `The {{ name }} value should contain {{ min }} elements or more.`

Message that will be shown when the input value has fewer elements than the defined in `min`.

Expand All @@ -70,7 +67,7 @@ The following parameters are available:

### `maxMessage`

type: `?string` default: `The {{ name }} value should contain {{ max }} elements or less, {{ numElements }} elements given.`
type: `?string` default: `The {{ name }} value should contain {{ max }} elements or less.`

Message that will be shown when the input value has more elements than the defined in `max`.

Expand All @@ -86,7 +83,7 @@ The following parameters are available:

### `exactMessage`

type: `?string` default: `The {{ name }} value should contain exactly {{ min }} elements, {{ numElements }} elements given.`
type: `?string` default: `The {{ name }} value should contain exactly {{ min }} elements.`

Message that will be shown when `min` and `max` options have the same value and the input value has a different number of elements.

Expand Down
2 changes: 1 addition & 1 deletion docs/03-rules_country.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Available options:

### `message`

type: `?string` default: `The {{ name }} value is not a valid country, {{ value }} given.`
type: `?string` default: `The {{ name }} value is not a valid country.`

Message that will be shown if the input value is not a valid country code.

Expand Down
4 changes: 2 additions & 2 deletions docs/03-rules_each-key.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ Validator that will validate each key of an `array` or object implementing `\Tra

### `message`

type: `?string` default: `Invalid key: {{ message }}`
type: `?string` default: `Invalid key {{ key }}: {{ message }}`

Message that will be shown if at least one input value key is invalid according to the given `validator`.

```php
// Throws: Invalid key: The color key value should be of type "string", 1 given.
// throws: Invalid key 1: The value should be of type "string".
Validator::eachKey(
Validator::type('string')
)->assert(['red' => '#f00', 1 => '#0f0'], 'color');
Expand Down
2 changes: 1 addition & 1 deletion docs/03-rules_each-value.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type: `?string` default: `At key "{{ key }}": {{ message }}`
Message that will be shown if at least one input value element is invalid according to the given `validator`.

```php
// Throws: At key 2: The color value should not be blank, "" given.
// throws: At key 2: The color value should not be blank.
Validator::eachValue(
Validator::notBlank()
)->assert(['red', 'green', ''], 'color');
Expand Down
2 changes: 1 addition & 1 deletion docs/03-rules_email.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Validator::email(normalizer: fn($value) => trim($value))->validate('test@example

### `message`

type: `?string` default: `The {{ name }} value is not a valid email address, {{ value }} given.`
type: `?string` default: `The {{ name }} value is not a valid email address.`

Message that will be shown if the input value is not a valid email address.

Expand Down
2 changes: 1 addition & 1 deletion docs/03-rules_greater-than-or-equal.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Can be a `string`, `int`, `float` or `DateTimeInterface` object.

### `message`

type: `?string` default: `The {{ name }} value should be greater than or equal to {{ constraint }}, {{ value }} given.`
type: `?string` default: `The {{ name }} value should be greater than or equal to {{ constraint }}.`

Message that will be shown if the value is not greater than or equal to the constraint value.

Expand Down
2 changes: 1 addition & 1 deletion docs/03-rules_greater-than.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Can be a `string`, `int`, `float` or `DateTimeInterface` object.

### `message`

type: `?string` default: `The {{ name }} value should be greater than {{ constraint }}, {{ value }} given.`
type: `?string` default: `The {{ name }} value should be greater than {{ constraint }}.`

Message that will be shown if the value is not greater than the constraint value.

Expand Down
37 changes: 37 additions & 0 deletions docs/03-rules_is-false.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# IsFalse

Validates that a value is `false`.

Check the [IsTrue](03-rules_is-true.md) rule for a `true` validation.

```php
IsFalse(
?string $message = null
);
```

## Basic Usage

```php
// anything else will be false
Validator::isFalse()->validate(false); // true
```

## Options

### `message`

type: `?string` default: `The {{ name }} value should be false.`

Message that will be shown if the value is false.

The following parameters are available:

| Parameter | Description |
|---------------|---------------------------|
| `{{ value }}` | The current invalid value |
| `{{ name }}` | Name of the invalid value |

## Changelog

- `1.3.0` Created
37 changes: 37 additions & 0 deletions docs/03-rules_is-null.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# IsNull

Validates that a value is `null`.

Check the [NotNull](03-rules_not-null.md) rule for the opposite validation.

```php
IsNull(
?string $message = null
);
```

## Basic Usage

```php
// anything else will be false
Validator::isNull()->validate(null); // true
```

## Options

### `message`

type: `?string` default: `The {{ name }} value should be null.`

Message that will be shown if the value is null.

The following parameters are available:

| Parameter | Description |
|---------------|---------------------------|
| `{{ value }}` | The current invalid value |
| `{{ name }}` | Name of the invalid value |

## Changelog

- `1.3.0` Created
37 changes: 37 additions & 0 deletions docs/03-rules_is-true.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# IsTrue

Validates that a value is `true`.

Check the [IsFalse](03-rules_is-false.md) rule for a `false` validation.

```php
IsTrue(
?string $message = null
);
```

## Basic Usage

```php
// anything else will be false
Validator::isTrue()->validate(true); // true
```

## Options

### `message`

type: `?string` default: `The {{ name }} value should be true.`

Message that will be shown if the value is true.

The following parameters are available:

| Parameter | Description |
|---------------|---------------------------|
| `{{ value }}` | The current invalid value |
| `{{ name }}` | Name of the invalid value |

## Changelog

- `1.3.0` Created
2 changes: 1 addition & 1 deletion docs/03-rules_language.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Available options:

### `message`

type: `?string` default: `The {{ name }} value is not a valid language, {{ value }} given.`
type: `?string` default: `The {{ name }} value is not a valid language.`

Message that will be shown if the input value is not a valid language code.

Expand Down
Loading

0 comments on commit 6d3760a

Please sign in to comment.