├── .github ├── FUNDING.yml └── workflows │ └── main.yml ├── LICENSE ├── Readme.md ├── composer.json └── src ├── CsvValidator.php └── CsvValidatorServiceProvider.php /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: minuteoflaravel 2 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | - name: Upload coverage reports to Codecov 2 | uses: codecov/codecov-action@v4.0.1 3 | with: 4 | token: ${{ secrets.CODECOV_TOKEN }} 5 | slug: minuteoflaravel/laravel-csv-validator 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Minute of Laravel 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # Laravel CSV Validator 2 | This package adds a CSV validator to your Laravel project. This validator parses uploaded file using parsecsv/php-parsecsv library and validation is passed when there are no errors during parsing. 3 | 4 | This package doesn't validate against a MIME type or file extension. 5 | ## Installation 6 | You can install package via composer: 7 | 8 | ```bash 9 | composer require minuteoflaravel/laravel-csv-validator 10 | ``` 11 | 12 | ## Example 13 | Check if a file has a CSV format: 14 | 15 | ```php 16 | $request->validate([ 17 | 'uploaded_file' => 'csv', 18 | ]); 19 | ``` 20 | 21 | ## Custom error messages 22 | 23 | If you need to add your custom translatable error message then just add it as always to resources/lang/en/validation.php file: 24 | 25 | ```php 26 | 'csv' => 'The :attribute must be a CSV file.', 27 | ``` 28 | 29 | ## License 30 | 31 | The MIT License (MIT). Please see [License File](LICENSE) for more information. 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "minuteoflaravel/laravel-csv-validator", 3 | "description": "Adds CSV validator to your Laravel project", 4 | "keywords": [ 5 | "laravel", 6 | "csv", 7 | "csv-validator", 8 | "minuteoflaravel" 9 | ], 10 | "homepage": "https://github.com/minuteoflaravel/laravel-csv-validator", 11 | "require": { 12 | "php": "^7.4|^8.0|^8.1", 13 | "parsecsv/php-parsecsv": "^1.3" 14 | }, 15 | "license": "MIT", 16 | "type": "library", 17 | "autoload": { 18 | "psr-4": { 19 | "MinuteOfLaravel\\CsvValidator\\": "src" 20 | } 21 | }, 22 | "authors": [ 23 | { 24 | "name": "Minute Of Laravel", 25 | "email": "contact@minuteoflaravel.com", 26 | "homepage": "https://minuteoflaravel.com" 27 | } 28 | ], 29 | "extra": { 30 | "laravel": { 31 | "providers": [ 32 | "MinuteOfLaravel\\CsvValidator\\CsvValidatorServiceProvider" 33 | ], 34 | "aliases": { 35 | "CsvValidator": "MinuteOfLaravel\\CsvValidator\\CsvValidator" 36 | } 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/CsvValidator.php: -------------------------------------------------------------------------------- 1 | getRealPath(); 27 | } else if ($value instanceof \Illuminate\Http\UploadedFile) { 28 | $file = $value->getPathname(); 29 | } else { 30 | throw new \Exception('CSV Validator: Unknown instance of uploaded file'); 31 | } 32 | 33 | $csv->parseFile($file); 34 | 35 | return empty($csv->error); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/CsvValidatorServiceProvider.php: -------------------------------------------------------------------------------- 1 |