You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+116-6Lines changed: 116 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,14 +7,124 @@ This library builds on the _outstanding_ JsonSchema validator [opis/json-schema]
7
7
8
8
The entire intent of this library is to make JsonSchema feel like a first class citizen in a Laravel project.
9
9
10
-
It adds a new config file, `config/json-schema.php` to configure your root directory for self-hosted schema files.
10
+
- Adds a new config file, `config/json-schema.php`, to configure your root directory for self-hosted schema files.
11
+
- Adds the `SchemaValidator` facade that can be used to instantiate the validator with appropriate loaders.
12
+
- Adds new PhpUnit assertions in the `Carsdotcom\JsonSchemaValidation\Traits\JsonSchemaAssertions` trait, such as validating that a mixed item validates for a specific schema.
13
+
- Most interestingly, it lets you use JsonSchema to validate incoming Requests bodies, and/or validate your own outgoing response bodies, all using JsonSchema schemas that you can then export into OpenAPI documentation.
11
14
12
-
It adds a Facade, `SchemaValidator` that can be used to instantiate the validator with appropriate loaders.
15
+
## Laravel Version Compatibility
13
16
14
-
It adds new PhpUnit assertions in `JsonSchemaAssertions` like validating that a mixed item validates for a specific schema.
17
+
This package supports Laravel `v9` and `v10`
15
18
16
-
Most interestingly, it lets you use JsonSchema to validate incoming Requests bodies, and/or validate your own outgoing response bodies, all using JsonSchema schemas that you can then export into OpenAPI documentation.
19
+
## Installation
17
20
18
-
## Coming Soon
21
+
```
22
+
composer require carsdotcom/laravel-json-schema
23
+
```
19
24
20
-
More documentation will be coming soon, including some more projects that build on this, including Guzzle outgoing- and incoming-body validation, and a new kind of Laravel Model that persists to JSON instead of to a relational database.
25
+
## Using Laravel JSON Schema
26
+
27
+
### Setup
28
+
29
+
#### Config File
30
+
Copy the `json-schema.php` file from the `vendor/carsdotcom/laravel-json-schema/config` folder to your application's `config` folder.
31
+
32
+
#### Schema Storage
33
+
1. Create a `Schemas` folder under your application root folder, such as `app/Schemas`.
34
+
2. Create a new storage disk under the `disks` key within your application's `config/filesystem.php` file:
35
+
36
+
```
37
+
'disks' => [
38
+
'schemas' => [
39
+
'driver' => 'local',
40
+
'root' => app_path('app/Schemas'), // must match the 'config.json-schema.local_base_prefix' value
41
+
]
42
+
]
43
+
```
44
+
3. Add your schema files to the `app/Schemas` folder. You may create subfolders to keep things organized.
45
+
46
+
#### Generate Enum Schemas
47
+
48
+
_This is an optional step, but can be super helpful._
49
+
50
+
Note: Enums must be created either as a built-in PHP `enum` object or a `MyCLabs\Enum\Enum` class.
51
+
52
+
1. Add `use Carsdotcom\JsonSchemaValidation\Traits\GeneratesSchemaTrait;` to the declarations in the Enum.
53
+
2. Add a `SCHEMA` constant to the enum. It's value will be the relative path to your schema file, such as: `const SCHEMA = '/Acme/Enums/item_type.json';`
54
+
3. Run the `schema:generate` Artisan command.
55
+
56
+
## Validating JSON Data Against a Schema
57
+
58
+
For this example, we'll be using these objects:
59
+
60
+
### Hosted JSON Schema File
61
+
62
+
This is assumed to be stored in your `app/Schemas` folder as `Product.json`.
0 commit comments