Skip to content

Commit

Permalink
initial commit of package
Browse files Browse the repository at this point in the history
  • Loading branch information
nwilging committed May 24, 2022
1 parent 669a349 commit 1211f81
Show file tree
Hide file tree
Showing 15 changed files with 8,210 additions and 2 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Coverage
on:
workflow_call:

jobs:
run-coverage:
name: Run Tests with Coverage
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Composer
uses: php-actions/composer@v5
with:
php_extensions: xdebug
- name: Run Tests
env:
XDEBUG_MODE: coverage
run: vendor/bin/phpunit
- name: Commit Coverage
uses: timkrase/[email protected]
with:
report: clover.xml
report_type: clover
coverage_badge_path: ./.github/coverage-badge.svg
repo_token: ${{ secrets.GH_ACCESS_TOKEN }}
push_badge: true
13 changes: 13 additions & 0 deletions .github/workflows/develop-branch.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: Develop Branch
on:
push:
branches:
- develop

jobs:
run-tests:
name: Tests
uses: ./.github/workflows/test.yml
run-coverage:
name: Coverage
uses: ./.github/workflows/coverage.yml
13 changes: 13 additions & 0 deletions .github/workflows/main-branch.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: Main Branch
on:
push:
branches:
- main

jobs:
run-tests:
name: Tests
uses: ./.github/workflows/test.yml
run-coverage:
name: Coverage
uses: ./.github/workflows/coverage.yml
8 changes: 8 additions & 0 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: Pull Request
on:
pull_request:

jobs:
test:
name: Run Tests
uses: ./.github/workflows/test.yml
21 changes: 21 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Test
on:
workflow_call:

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
php: ["7.4", "8.0", "8.1"]
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Composer
uses: php-actions/composer@v5
with:
php_version: ${{ matrix.php }}
- name: Run Tests
run: vendor/bin/phpunit --no-coverage
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/vendor/
.env
.phpunit.result.cache
tests/html-coverage
clover.xml
.idea/
64 changes: 62 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,62 @@
# laravel-strict-types-validation
A validator to require strict types on request data
# Strict Types Form Request Validation

![Tests](https://github.com/nwilging/laravel-strict-types-validation/actions/workflows/main-branch.yml/badge.svg?branch=main)
![Coverage](./.github/coverage-badge.svg)
[![Latest Stable Version](http://poser.pugx.org/nwilging/laravel-strict-types-validation/v)](https://packagist.org/packages/nwilging/laravel-strict-types-validation)
[![License](http://poser.pugx.org/nwilging/laravel-strict-types-validation/license)](https://packagist.org/packages/nwilging/laravel-strict-types-validation)
[![Total Downloads](http://poser.pugx.org/nwilging/laravel-strict-types-validation/downloads)](https://packagist.org/packages/nwilging/laravel-strict-types-validation)

Ensures incoming form request data is a certain datatype.

---

### About

While Laravel includes many useful validation rules out of the box, [it lacks the ability to validate _data type_ as well
as _content type_](https://laravel.com/docs/9.x/validation#rule-integer). There have been a [couple](https://github.com/laravel/framework/issues/18918)
[complaints](https://github.com/laravel/ideas/issues/1719) about this over the years, but due to the versatile nature of
Laravel, it doesn't seem likely that validation rules such as `integer` or `boolean` will begin validating that the data
is actually of the desired type.

This package provides a way for you to require the incoming data to be of a given type, such as `int`, `bool`, `float`, etc.

---

# Installation

### Pre Requisites
1. Laravel v8+
2. PHP 7.4+

### Install with Composer

```
composer require nwilging/laravel-strict-types-validation
```

---

# Usage

When constructing validation rules, simply add `type:<desired type>` to the validation rules string/array.

```
$rules = [
'id' => 'required|type:int', # This will require the incoming `id` to be an integer.
];
```

### Failure Messages

The failure message format is:
```
The :attribute must be of type :type
```

Where `attribute` is the attribute being validated (`id` from the above example) and `type` is the desired type to validate
against (`int` in the above example).

If the above example failed, we would receive this message:
```
The id must be of type int
```
36 changes: 36 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "nwilging/laravel-strict-types-validation",
"description": "A validator for laravel to require strict types in form request data",
"type": "library",
"license": "MIT",
"autoload": {
"psr-4": {
"Nwilging\\LaravelStrictValidation\\": "src/",
"Nwilging\\LaravelStrictValidationTests\\": "tests/"
}
},
"authors": [
{
"name": "Nick Wilging",
"email": "[email protected]"
}
],
"require": {
"php": ">=7.4",
"laravel/framework": ">=8"
},
"extra": {
"laravel": {
"providers": [
"Nwilging\\LaravelStrictValidation\\Providers\\LaravelStrictValidationProvider"
]
}
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"mockery/mockery": "^1.5",
"squizlabs/php_codesniffer": "^3.6",
"dealerinspire/laravel-coding-standard": "^2.0",
"orchestra/testbench": "^7.5"
}
}
Loading

0 comments on commit 1211f81

Please sign in to comment.