Skip to content

Commit 648bc33

Browse files
committed
docs: Update docs
1 parent ca7fd92 commit 648bc33

File tree

5 files changed

+54
-5
lines changed

5 files changed

+54
-5
lines changed

docs/basic-usage.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ Bag will cast all values to their defined type _automatically_ for all scalar ty
7979
8080
### Modifying a Value Object
8181

82-
Value Objects are immutable, so you cannot change their properties directly. Instead, you can create a new instance with the updated values using the `Bag->with()` method:
82+
Value Objects are immutable, so you cannot change their properties directly. Instead, you can create a new instance with the updated values using the `Bag->with()` or `Bag->append()` methods:
8383

8484
```php
8585
$value = MyValue::from([
@@ -93,3 +93,6 @@ dump($newValue->toArray()); // ['name' => 'Davey Shafik', 'age' => 41]
9393
```
9494

9595
You can pass either named arguments, or an array or Collection of key-value pairs to the `Bag->with()` method.
96+
97+
> [!TIP]
98+
> The `Bag->append()` method works the same way as `Bag->with()`, but it will not validate the new value object. You can manually validate the object using `Bag->valid()`.

docs/laravel-controller-injection.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Laravel Controller Injection
22

3-
Bag can automatically inject _validated_ Bag objects into your controllers using Laravel's automatic dependency injection. This can take
3+
Bag can automatically inject Bag objects into your controllers using Laravel's automatic dependency injection. This can take
44
the place of using Laravel's form request validation _and_ accessing the input data.
55

66
```php
@@ -13,5 +13,30 @@ class MyController extends Controller {
1313
}
1414
```
1515

16+
## Automatic Validation
17+
1618
When you type hint a `Bag` object in your controller method, Bag will automatically validate the request data and inject the `Bag` object into your controller method.
1719

20+
## Manual Validation
21+
22+
If you want to inject the `Bag` object without validation, you can add the `WithoutValidation` attribute to the property:
23+
24+
```php
25+
use App\Values\MyValue;
26+
use Bag\Attributes\WithoutValidation;
27+
28+
class MyController extends Controller {
29+
public function store(
30+
#[WithoutValidation] MyValue $value
31+
) {
32+
$value = $value->append(extra: 'data')->valid();
33+
// $value is now a validated MyValue object
34+
}
35+
}
36+
```
37+
38+
> [!TIP]
39+
> The `Bag->valid()` method will throw a `ValidationException` if the Bag object is not valid by default, you can pass in `false` to return null instead.
40+
41+
> [!CAUTION]
42+
> The input values must still fulfill the requirements of the Bag constructor, all required properties must be present otherwise an exception will be thrown.

docs/mapping.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ class MyValue extends Bag {
125125

126126
## When Mapping Applies
127127

128-
Input mapping is applied when calling `Bag::from()`. You can use either the original property name _or_ the mapped name when creating a Bag.
128+
Input mapping is applied when calling `Bag::from()` or `Bag::withoutValidation()`. You can use either the original property name _or_ the mapped name when creating a Bag.
129129

130130
> [!TIP]
131131
> [Validation](validation) is applied to the original property name, not the mapped name.

docs/validation.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ readonly class MyValue extends Bag
2929

3030
In this example we added a `#[Required]` attribute to the `name` property, and defined validation rules in the `rules()` method.
3131

32-
You can validate a Bag object using the `Bag::validate()` method:
32+
You can validate a Bag object before creating it using the `Bag::validate()` method:
3333

3434
```php
3535
$value = MyValue::validate([
@@ -38,6 +38,27 @@ $value = MyValue::validate([
3838
]);
3939
```
4040

41+
## Creating a Bag Without Validation
42+
43+
If you want to create a Bag without automatically validating it, you can use the `Bag::withoutValidation()` method:
44+
45+
```php
46+
$value = MyValue::withoutValidation([
47+
'name' => 'Davey Shafik',
48+
'age' => 40,
49+
]);
50+
```
51+
52+
To futher append properties to a Bag without validation, you can use the `Bag::append()` method:
53+
54+
```php
55+
$value = MyValue::withoutValidation([
56+
'name' => 'Davey Shafik',
57+
])->append([
58+
'age' => 40,
59+
]);
60+
```
61+
4162
## Built-in Validation Attributes
4263

4364
Bag provides a number of built-in validation attributes, based on various Laravel validation rules:

docs/why.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ has [custom collection classes](https://spatie.be/docs/laravel-data/v4/as-a-data
5050

5151
## Hidden Properties
5252

53-
Bag supports [hiding properties](hidden) when transforming to an array or JSON.
53+
Bag supports [hiding properties](hidden) when transforming to an array and/or JSON.
5454

5555
## Artisan `make:bag` Command
5656

0 commit comments

Comments
 (0)