From 6e695a160e33dac8afe6741b590d0ab5134393a4 Mon Sep 17 00:00:00 2001 From: David Tarrant Date: Thu, 7 Nov 2024 21:17:51 +0000 Subject: [PATCH] Fixes and adding examples --- index.js | 29 ++++--- public/examples/examples.json | 34 +++++++++ views/about.ejs | 16 +--- views/examples.ejs | 138 +++++++++++++++++++++++++++------- 4 files changed, 162 insertions(+), 55 deletions(-) create mode 100644 public/examples/examples.json diff --git a/index.js b/index.js index 80dbcd6..82b4e2f 100644 --- a/index.js +++ b/index.js @@ -49,6 +49,13 @@ app.set('view engine', 'ejs'); app.use(cors()); app.use(express.static(__dirname + '/public')); // Public directory +app.use((req, res, next) => { + res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate'); // HTTP 1.1. + res.setHeader('Pragma', 'no-cache'); // HTTP 1.0. + res.setHeader('Expires', '0'); // Proxies. + next(); +}); + app.use((req, res, next) => { // Read package.json file fs.readFile(path.join(__dirname, 'package.json'), 'utf8', (err, data) => { @@ -151,17 +158,16 @@ app.get('/validate', async (req, res) => { } // Collect dialect options from the query params - const dialect = { - delimiter: req.query.delimiter, - doubleQuote: req.query.doubleQuote === 'true', - lineTerminator: req.query.lineTerminator, - nullSequence: req.query.nullSequence, - quoteChar: req.query.quoteChar, - escapeChar: req.query.escapeChar, - skipInitialSpace: req.query.skipInitialSpace === 'true', - header: req.query.header === 'true', - caseSensitiveHeader: req.query.caseSensitiveHeader === 'true', - }; + const dialect = {}; + if (req.query.delimiter) dialect.delimiter = req.query.delimiter; + if (req.query.doubleQuote) dialect.doubleQuote = req.query.doubleQuote === 'true'; + if (req.query.lineTerminator) dialect.lineTerminator = req.query.lineTerminator; + if (req.query.nullSequence) dialect.nullSequence = req.query.nullSequence; + if (req.query.quoteChar) dialect.quoteChar = req.query.quoteChar; + if (req.query.escapeChar) dialect.escapeChar = req.query.escapeChar; + if (req.query.skipInitialSpace) dialect.skipInitialSpace = req.query.skipInitialSpace === 'true'; + if (req.query.header) dialect.header = req.query.header === 'true'; + if (req.query.caseSensitiveHeader) dialect.caseSensitiveHeader = req.query.caseSensitiveHeader === 'true'; if (Object.keys(dialect).length > 0) { form.append('dialect', JSON.stringify(dialect)); @@ -301,7 +307,6 @@ app.post('/validate', upload.fields([{ name: 'file' }, { name: 'schema' }]), asy if (Object.keys(dialect).length > 0) { form.append('dialect', JSON.stringify(dialect)); } - // Send the form data to the Ruby server const response = await axios.post(CSVLINT_API, form, { headers: form.getHeaders(), diff --git a/public/examples/examples.json b/public/examples/examples.json new file mode 100644 index 0000000..0ff6c0e --- /dev/null +++ b/public/examples/examples.json @@ -0,0 +1,34 @@ +[ + { + "title": "Student List", + "description": "A simple CSV file containing a list of students with their names and ages.", + "expectedResult": "This CSV should validate successfully without errors or warnings.", + "csvUrl": "https://raw.githubusercontent.com/theodi/csvlint.io/refs/heads/main/public/examples/students_perfect.csv", + "dialect": "", + "schemaUrl": "" + }, + { + "title": "Mixed types", + "description": "A simple CSV file containing a list of students with their names and ages.", + "expectedResult": "This CSV should produce warnings due to data inconsistencies in the age column.", + "csvUrl": "https://raw.githubusercontent.com/theodi/csvlint.io/refs/heads/main/public/examples/students_warnings.csv", + "dialect": "", + "schemaUrl": "" + }, + { + "title": "Pipe Separator", + "description": "A simple CSV file containing a list of students with their names and ages.", + "expectedResult": "This CSV is valid but the separator is a pipe character.", + "csvUrl": "https://raw.githubusercontent.com/theodi/csvlint.io/refs/heads/main/public/examples/students_pipe.txt", + "dialect": "delimiter=|", + "schemaUrl": "" + }, + { + "title": "Schema validation", + "description": "Same file as in the first example, but this time the schema will throw errors.", + "expectedResult": "This CSV should be invalid based upon the schema.", + "csvUrl": "https://raw.githubusercontent.com/theodi/csvlint.io/refs/heads/main/public/examples/students_perfect.csv", + "dialect": "", + "schemaUrl": "https://raw.githubusercontent.com/theodi/csvlint.io/refs/heads/main/public/examples/studentsSchema.json" + } + ] diff --git a/views/about.ejs b/views/about.ejs index 08fdcf3..1c24a70 100644 --- a/views/about.ejs +++ b/views/about.ejs @@ -36,23 +36,11 @@

:csv%20 %20warnings yellow

-

If you click on that you'll get a pop-up that gives you some code for embedding the badge in your page. You can put that next to some CSV and it means you and other people will be able to see whether the CSV is valid or not.

- -

Wear your valid CSV badge with pride!

- -

Fix that CSV

- -

We can fix some of the errors that we find in CSV files, such as bad encodings. At the bottom of the page that shows you how to improve the file, you'll see a button that says Download Standardised CSV File.

- -

That won't fix all the problems: we won't delete empty lines or try to fix up values that are in the wrong format. We can't change the way your server provides CSV either, so you'll still be warned if it's not using the right Content-Type header.

- -

Find a Schema

- -

The Recent schemas page gives a list of schemas that people have been using to validate their CSV files. See if there's a schema that you could use!

+

You can also see the embed code to allow you to embed the badge on your own website.

Dialects

-Dialect +Dialect

diff --git a/views/examples.ejs b/views/examples.ejs index f4cfb54..e23c0a6 100644 --- a/views/examples.ejs +++ b/views/examples.ejs @@ -1,44 +1,124 @@ <%- include('./partials/header') %>
+

CSV Validation Examples

Below are some example CSV files demonstrating different validation scenarios. Each example shows the contents of the CSV file, the expected validation result, and a link to validate it. Click on the badge to view the validation results for each example.

-

Example: Student List

-

This example demonstrates a simple CSV file containing a list of students with their names and ages. The CSV file is expected to pass validation without errors.

- - -

Contents of students.csv

-
Loading file content...
- - -

Expected Result

-

This CSV should validate successfully, as it is well-formed and contains consistent data types in each column. No errors or warnings are expected.

- - -

Validation Badge

-

Click the badge below to view the validation results for this example:

- - Validation Badge for students.csv - + + + + + + + + + + + + + +
ExampleContentsExpected ResultActual Result