Skip to content

Commit

Permalink
Merge a53aff7 into 78ad613
Browse files Browse the repository at this point in the history
  • Loading branch information
SmetDenis committed Mar 11, 2024
2 parents 78ad613 + a53aff7 commit 661c76a
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 16 deletions.
121 changes: 106 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,127 @@
[![Stable Version](https://poser.pugx.org/jbzoo/csv-blueprint/version)](https://packagist.org/packages/jbzoo/csv-blueprint/) [![Total Downloads](https://poser.pugx.org/jbzoo/csv-blueprint/downloads)](https://packagist.org/packages/jbzoo/csv-blueprint/stats) [![Docker Pulls](https://img.shields.io/docker/pulls/jbzoo/csv-blueprint.svg)](https://hub.docker.com/r/jbzoo/csv-blueprint) [![Dependents](https://poser.pugx.org/jbzoo/csv-blueprint/dependents)](https://packagist.org/packages/jbzoo/csv-blueprint/dependents?order_by=downloads) [![GitHub License](https://img.shields.io/github/license/jbzoo/csv-blueprint)](https://github.com/JBZoo/Csv-Blueprint/blob/master/LICENSE)


* [Introduction](#introduction)
* [Features](#features)
* [Usage](#usage)
* [As GitHub Action](#as-github-action)
* [As Docker container](#as-docker-container)
* [As PHP binary](#as-php-binary)
* [As PHP project](#as-php-project)
* [Schema Definition](#schema-definition)
* [Schema file examples](#schema-file-examples)
* [Unit tests and check code style](#unit-tests-and-check-code-style)
* [License](#license)


### Installing
## Introduction
The JBZoo/Csv-Blueprint tool is a powerful and flexible utility designed for validating CSV files against
a predefined schema specified in YAML format. With the capability to run both locally and in Docker environments,
JBZoo/Csv-Blueprint is an ideal choice for integrating into CI/CD pipelines, such as GitHub Actions,
to ensure the integrity of CSV data in your projects.


## Features
* **Schema-based Validation**: Define the structure and rules for your CSV files in an intuitive [YAML format](schema-examples/full.yml), enabling precise validation against your data's expected format.
* **Flexible Configuration**: Support for custom delimiters, quote characters, enclosures, and encoding settings to handle a wide range of CSV formats.
* **Comprehensive Rule Set**: Includes a broad set of validation rules, such as non-empty fields, exact values, regular expressions, numeric constraints, date formats, and more, catering to various data validation needs.
* **Docker Support**: Easily integrate into any workflow with Docker, providing a seamless experience for development, testing, and production environments.
* **GitHub Actions Integration**: Automate CSV validation in your CI/CD pipeline, enhancing the quality control of your data in pull requests and deployments.
* **Various ways to report** issues that can be easily integrated with GithHub, Gitlab, TeamCity, etc. The default output is a human-readable table. [See Live Demo](https://github.com/JBZoo/Csv-Blueprint/actions/workflows/demo.yml).



## Usage

Also see demo in the [GitHub Actions](.github/workflows/demo.yml) file.

### As GitHub Action

```yml
- name: Validate CSV file
uses: jbzoo/csv-blueprint@master
with:
csv: tests/fixtures/demo.csv
schema: tests/schemas/demo_invalid.yml
output: table
```

### As Docker container
Ensure you have Docker installed on your machine.

```sh
composer require jbzoo/csv-blueprint
# Pull the Docker image
docker pull jbzoo/csv-blueprint

# Run the tool inside Docker
docker run --rm \
--workdir=/parent-host \
-v `pwd`:/parent-host \
jbzoo/csv-blueprint \
validate:csv \
--csv=./tests/fixtures/demo.csv \
--schema=./tests/schemas/demo_invalid.yml
```


### Usage
### As PHP binary
Ensure you have PHP installed on your machine.

As Docker container:
```sh
wget https://github.com/JBZoo/CI-Report-Converter/releases/latest/download/csv-blueprint.phar
chmod +x ./csv-blueprint.phar
./csv-blueprint.phar --csv=./tests/fixtures/demo.csv --schema=./tests/schemas/demo_invalid.yml
```

### As PHP project
Ensure you have PHP installed on your machine.
Then, you can use the following commands to build from source and run the tool.

```sh
@docker run --rm \
-v `pwd`:/parent-host \
jbzoo/csv-blueprint \
validate:csv \
--csv=/parent-host/tests/fixtures/demo.csv \
--schema=/parent-host/tests/schemas/demo_invalid.yml \
--ansi
git clone git@github.com:jbzoo/csv-blueprint.git csv-blueprint
cd csv-blueprint
make build
./csv-blueprint validate:csv --csv=./tests/fixtures/demo.csv --schema=./tests/schemas/demo_invalid.yml
```

### Schema Definition
Define your CSV validation schema in a YAML file.

This example defines a simple schema for a CSV file with a header row, specifying that the id column must not be empty and must contain integer values.
Also it checks that the name column is not empty and has a minimum length of 3 characters.

```yaml

Here's an example to get you started:
```yml
csv:
delimiter: ,
quote_char: \
enclosure: "\""
columns:
- name: id
rules:
not_empty: true
is_int: true
- name: name
rules:
not_empty: true
min_length: 3
```

### Schema file examples

<details>
<summary>Click to see: YAML format (with comment)</summary>
In the [example Yml file](schema-examples/full.yml) you can find a detailed description of all features.

**Important notes**
* I have deliberately refused typing of columns and replaced them with rules,
which can be combined in any sequence and completely at your discretion.
This gives you great flexibility when validating CSV files.

* All fields (unless explicitly stated otherwise) are optional and you can choose not to declare them. Up to you.

```yml
# It's a full example of the CSV schema file in YAML format.
Expand Down Expand Up @@ -93,8 +186,6 @@ columns:
```

</details>


<details>
<summary>Click to see: JSON Format</summary>
Expand Down
1 change: 1 addition & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ runs:
- ${{ inputs.schema }}
- '--output'
- ${{ inputs.output }}
- '--ansi'
- '-vvv'
4 changes: 3 additions & 1 deletion tests/Blueprint/MiscTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ private function testCheckExampleInReadme(
$tmpl = \implode("\n", ["```{$type}", $filepath, '```']);
}

$tmpl = $this->getSpoiler("Click to see: {$title}", $tmpl);
if ($type !== 'yml') {
$tmpl = $this->getSpoiler("Click to see: {$title}", $tmpl);
}

isFileContains($tmpl, PROJECT_ROOT . '/README.md');
}
Expand Down

0 comments on commit 661c76a

Please sign in to comment.