Skip to content

Commit 7cd6e9c

Browse files
author
Riley Aven
committed
Parse form requests, add documentation
1 parent e159f86 commit 7cd6e9c

File tree

8 files changed

+105
-32
lines changed

8 files changed

+105
-32
lines changed

README.md

Lines changed: 55 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
# This is my package laravel-rules-to-schema
1+
# Laravel Rules to Schema
22

33
[![Latest Version on Packagist](https://img.shields.io/packagist/v/riley19280/laravel-rules-to-schema.svg?style=flat-square)](https://packagist.org/packages/riley19280/laravel-rules-to-schema)
44
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/riley19280/laravel-rules-to-schema/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/riley19280/laravel-rules-to-schema/actions?query=workflow%3Arun-tests+branch%3Amain)
55
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/riley19280/laravel-rules-to-schema/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/riley19280/laravel-rules-to-schema/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
66
[![Total Downloads](https://img.shields.io/packagist/dt/riley19280/laravel-rules-to-schema.svg?style=flat-square)](https://packagist.org/packages/riley19280/laravel-rules-to-schema)
77

8-
This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.
8+
Create a json schema for your Laravel rules
99

10-
## Support us
10+
## Prerequisites
1111

12-
[<img src="https://github-ads.s3.eu-central-1.amazonaws.com/laravel-rules-to-schema.jpg?t=1" width="419px" />](https://spatie.be/github-ad-click/laravel-rules-to-schema)
12+
It is recommended to use `FormRequest` classes in your application to use this package.
13+
This allows for easy extraction of rule objects to pass them into this package.
1314

14-
We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).
15-
16-
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).
15+
You can still use this package just by passing in an array of rules, but that is a but more cumbersome.
1716

1817
## Installation
1918

@@ -23,43 +22,72 @@ You can install the package via composer:
2322
composer require riley19280/laravel-rules-to-schema
2423
```
2524

26-
You can publish and run the migrations with:
27-
28-
```bash
29-
php artisan vendor:publish --tag="laravel-rules-to-schema-migrations"
30-
php artisan migrate
31-
```
32-
33-
You can publish the config file with:
25+
And publish the config file with:
3426

3527
```bash
3628
php artisan vendor:publish --tag="laravel-rules-to-schema-config"
3729
```
3830

39-
This is the contents of the published config file:
31+
## Usage
4032

4133
```php
42-
return [
43-
];
34+
use \Illuminate\Foundation\Http\FormRequest;
35+
36+
$schema = LaravelRulesToSchema::parse(FormRequest|array $rules): FluentSchema
37+
38+
$schema->compile(); // Returns an array representation of the json schema
4439
```
4540

46-
Optionally, you can publish the views using
41+
## Customization
4742

48-
```bash
49-
php artisan vendor:publish --tag="laravel-rules-to-schema-views"
43+
### JSON Schema Representation
44+
45+
This package relies on [riley19280/fluent-json-schema](https://github.com/riley19280/fluent-json-schema) to build
46+
and represent JSON schemas. For details on how to use and configure the objects returned from the rule parser,
47+
please see the [documentation](https://github.com/riley19280/fluent-json-schema) for that package.
48+
49+
50+
### Registering schemas for custom rules
51+
52+
JSON Schema definitions for custom rules can be registered in the config file by providing the rule name or class name.
53+
54+
The type can be any of the simple JSON schema types (array, boolean, integer, null, number, object, string).
55+
If a more complex type is required, you can provide the class name that implements `LaravelRulesToSchema\Contracts\HasJsonSchema`
56+
57+
```php
58+
// config/rules-to-schema.php
59+
60+
'custom_rule_schemas' => [
61+
// \CustomPackage\CustomRule::class => \Support\CustomRuleSchemaDefinition::class,
62+
// \CustomPackage\CustomRule::class => 'string',
63+
// \CustomPackage\CustomRule::class => ['null', 'string'],
64+
],
5065
```
5166

52-
## Usage
67+
### Extending the rule parser
68+
69+
Should you want to further customize or tweak how rules are parsed, additional parsers can be added in the config file.
70+
Each parser is run for each rule in the order it is defined in the config file.
71+
Custom parsers must implement the `LaravelRulesToSchema\Contracts\RuleParser` interface.
5372

5473
```php
55-
$laravelRulesToSchema = new LaravelRulesToSchema();
56-
echo $laravelRulesToSchema->echoPhrase('Hello, LaravelRulesToSchema!');
74+
// config/rules-to-schema.php
75+
76+
'parsers' => [
77+
...
78+
\CustomPackage\CustomParser::class,
79+
],
5780
```
5881

59-
## Testing
82+
### Tools for package developers
6083

61-
```bash
62-
composer test
84+
If your package contains a custom rule, you can provide the types as part of the package as well.
85+
86+
In a service provider you can use the following methods to programmatically register a rule or parser:
87+
88+
```php
89+
LaravelRulesToSchema::registerCustomRuleSchema(CustomRule::class, CustomRuleSchemaDefinition::class);
90+
LaravelRulesToSchema::registerParser(CustomParser::class);
6391
```
6492

6593
## Changelog

composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
{
22
"name": "riley19280/laravel-rules-to-schema",
3-
"description": "Parse Laravel validation rules into a Json Schema Object",
3+
"description": "Parse Laravel validation rules into a Json Schema",
44
"keywords": [
55
"Riley Aven",
66
"laravel",
7-
"laravel-rules-to-schema"
7+
"laravel-rules-to-schema",
8+
"json schema",
9+
"laravel rule",
10+
"parse rule"
811
],
912
"homepage": "https://github.com/riley19280/laravel-rules-to-schema",
1013
"license": "MIT",

config/rules-to-schema.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
return [
1414

1515
/*
16-
* The key to store validation rules under
16+
* The internal key to store validation rules under for parsers
1717
* This should be unique and not match any real property names
1818
* that will be submitted in requests.
1919
*/

src/Facades/LaravelRulesToSchema.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Illuminate\Support\Facades\Facade;
77

88
/**
9-
* @method static FluentSchema parse(array $rules) Parse Rules
9+
* @method static FluentSchema parse(string|array $rules) Parse Rules
1010
* @method static void registerParser(string $parser) Register a rule parser
1111
* @method static void registerCustomRuleSchema(string $rule, mixed $type) Register a schema for a custom rule
1212
*

src/LaravelRulesToSchema.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace LaravelRulesToSchema;
44

55
use FluentJsonSchema\FluentSchema;
6+
use Illuminate\Foundation\Http\FormRequest;
67

78
class LaravelRulesToSchema
89
{
@@ -12,8 +13,17 @@ class LaravelRulesToSchema
1213

1314
protected static array $additionalCustomSchemas = [];
1415

15-
public function parse(array $rules): FluentSchema
16+
public function parse(string|array $rules): FluentSchema
1617
{
18+
if (is_string($rules)) {
19+
if (! class_exists($rules)) {
20+
throw new \Exception("Class $rules does not implement ".FormRequest::class.' and can not be parsed.');
21+
}
22+
$instance = new $rules;
23+
24+
$rules = method_exists($instance, 'rules') ? app()->call([$instance, 'rules']) : [];
25+
}
26+
1727
$normalizedRules = (new ValidationRuleNormalizer($rules))->getRules();
1828

1929
$schema = FluentSchema::make()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"type": "object",
3+
"required": [
4+
"field1"
5+
],
6+
"properties": {
7+
"field1": {
8+
"type": "string"
9+
}
10+
}
11+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace LaravelRulesToSchema\Tests\Fixtures;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
7+
class CustomFormRequest extends FormRequest
8+
{
9+
public function rules(): array
10+
{
11+
return [
12+
'field1' => ['required', 'string'],
13+
];
14+
}
15+
}

tests/ParsingTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use Illuminate\Validation\Rule;
44
use Illuminate\Validation\Rules\Enum;
55
use LaravelRulesToSchema\Facades\LaravelRulesToSchema;
6+
use LaravelRulesToSchema\Tests\Fixtures\CustomFormRequest;
67
use LaravelRulesToSchema\Tests\Fixtures\TestEnum;
78
use LaravelRulesToSchema\Tests\Fixtures\TestIntBackedEnum;
89
use LaravelRulesToSchema\Tests\Fixtures\TestModel;
@@ -231,3 +232,8 @@
231232
expect(LaravelRulesToSchema::parse($rules)->compile())
232233
->toMatchSnapshot();
233234
});
235+
236+
test('parse for request class', function () {
237+
expect(LaravelRulesToSchema::parse(CustomFormRequest::class)->compile())
238+
->toMatchSnapshot();
239+
});

0 commit comments

Comments
 (0)