{"id":21894764,"url":"https://github.com/zumba/csv-policy","last_synced_at":"2025-08-11T22:35:56.429Z","repository":{"id":57092222,"uuid":"14871946","full_name":"zumba/csv-policy","owner":"zumba","description":"Simple, policy-driven csv rules validation","archived":false,"fork":false,"pushed_at":"2014-02-28T02:26:03.000Z","size":372,"stargazers_count":8,"open_issues_count":1,"forks_count":1,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-07-19T16:42:03.442Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://engineering.zumba.com","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zumba.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-12-02T19:10:43.000Z","updated_at":"2016-07-20T16:37:23.000Z","dependencies_parsed_at":"2022-08-22T21:40:14.363Z","dependency_job_id":null,"html_url":"https://github.com/zumba/csv-policy","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/zumba/csv-policy","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zumba%2Fcsv-policy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zumba%2Fcsv-policy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zumba%2Fcsv-policy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zumba%2Fcsv-policy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zumba","download_url":"https://codeload.github.com/zumba/csv-policy/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zumba%2Fcsv-policy/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269969265,"owners_count":24505427,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-11T02:00:10.019Z","response_time":75,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-28T13:28:02.238Z","updated_at":"2025-08-11T22:35:56.405Z","avatar_url":"https://github.com/zumba.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CsvPolicy\n\n[![Build Status](https://travis-ci.org/zumba/csv-policy.png)](https://travis-ci.org/zumba/csv-policy)\n\n\n**CsvPolicy** is a simple, policy-based validation library for CSV files.  Create rules for columns in a CSV file and the validator will load them, parse the file, and report rule violations.\n\n*CsvPolicy requires PHP \u003e= 5.4*\n\n## Example\nSuppose you have the following business requirements for a CSV file named `products.csv` that will be uploaded to your application:\n\n1. It must contain the following column names: `id`, `value`\n2. It must contain at least one of the following columns: `upc`, `code`, `stock`\n3. The `id` column's values must be unique.\n4. The `value` column must only contain numeric values\n\nCsvPolicy allows you to model these rules as classes.  Required and optional fields are handled by the `Validator` class directly, so we only need to define rules for the `id` column and the `value` column.\n\nCsvPolicy uses Traits for common validation operations (e.g. checking all values in a column are unique).  The folowing rule will ensure that the values in the `id` field are unique:\n```php\nnamespace Zumba\\CsvPolicy\\Rule\\Products;\n\nuse \\Zumba\\CsvPolicy\\Behavior;\n\nclass Id extends \\Zumba\\CsvPolicy\\Rule {\n\n\tuse Unique;\n\n\tpublic function getErrorMessage($input) {\n\t\treturn 'Id must be unique.  Duplicate found: ' . $input;\n\t}\n}\n```\n\nYou can override the `Rule::validationLogic` method to define custom validation for that column.  The validator will use this method to check all of the values in the CSV for the column.\n```php\nnamespace Zumba\\CsvPolicy\\Rule\\Products;\n\nclass Value extends \\Zumba\\CsvPolicy\\Rule {\n\tpublic function getErrorMessage($input) {\n\t\treturn 'Value must only contain numeric values. Non-numeric value found: ' . $input;\n\t}\n\tpublic function validationLogic($input) {\n\t\treturn is_numeric($input);\n\t}\n}\n```\n\nFinally, you configure your validator and use it to check if the CSV file is valid:\n```php\n$validator = new \\Zumba\\CsvPolicy\\Validator();\n$validator-\u003econfig([\n\t'rulesPath' =\u003e './path/to/custom/rules'\n\t'requiredFields' =\u003e [\n\n\t\t// string field names are always required\n\t\t'id', 'value',\n\n\t\t// arrays of field names indicate that at least one field is required, but not all\n\t\t['upc', 'code', 'stock']\n\t]\n]);\n\n$valid = $validator-\u003eisValid('./path/to/products.csv');\nif (!$valid){\n\t// errors is an array of rules violations\n\t$errors = $validator-\u003egetErrors();\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzumba%2Fcsv-policy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzumba%2Fcsv-policy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzumba%2Fcsv-policy/lists"}