### Install csvlint gem Source: https://github.com/data-liberation-front/csvlint.rb/blob/main/README.md Installation instructions for the gem via Gemfile or direct command. ```bash gem 'csvlint' ``` ```bash $ bundle ``` ```bash $ gem install csvlint ``` -------------------------------- ### Enter Ruby REPL Source: https://github.com/data-liberation-front/csvlint.rb/blob/main/docker_notes_for_windows.txt Starts an interactive Ruby Read-Eval-Print Loop (REPL) session within the Docker container. Allows for direct execution of Ruby code. ```bash docker run -it --rm -v %CD%:/tmp csvlint irb ``` -------------------------------- ### Define a JSON Table Schema Source: https://github.com/data-liberation-front/csvlint.rb/wiki/Usage Example configuration file defining field constraints such as required status, minimum length, and regex patterns. ```json { "fields": [ { "name": "id", "constraints": { "required": true, "type": "http://www.w3.org/TR/xmlschema-2/#integer" } }, { "name": "price", "constraints": { "required": true, "minLength": 1 } }, { "name": "postcode", "constraints": { "required": true, "pattern": "[A-Z]{1,2}[0-9][0-9A-Z]? ?[0-9][A-Z]{2}" } } ] } ``` -------------------------------- ### Generate Gemfile.lock with Docker Source: https://github.com/data-liberation-front/csvlint.rb/blob/main/docker_notes_for_windows.txt Run this command within a Docker environment on MS Windows to generate the Gemfile.lock file. It mounts the current directory to the container and installs dependencies. ```bash docker run --rm -v %CD%:/usr/src/app -w /usr/src/app ruby:2.5 bundle install ``` -------------------------------- ### Define CSV on the Web Metadata Source: https://github.com/data-liberation-front/csvlint.rb/wiki/Usage Example configuration file using the CSVW standard to define table schema and column datatypes. ```json { "@context": "http://www.w3.org/ns/csvw", "url": "http://example.com/example1.csv", "tableSchema": { "columns": [ { "name": "id", "required": true, "datatype": { "base": "integer" } }, { "name": "price", "required": true, "datatype": { "base": "string", "minLength": 1 } }, { "name": "postcode", "required": true } ] } } ``` -------------------------------- ### JSON Table Schema Definition Source: https://context7.com/data-liberation-front/csvlint.rb/llms.txt Example of defining a schema using the JSON Table Schema format. ```APIDOC ## JSON Table Schema Definition Define field constraints using JSON Table Schema format with type validation, length constraints, patterns, and uniqueness. ### Schema Example ```json { "title": "Product Catalog Schema", "description": "Schema for validating product data exports", "fields": [ { "name": "product_id", "title": "Product ID", "description": "Unique product identifier", "constraints": { "required": true, "unique": true, "type": "http://www.w3.org/2001/XMLSchema#positiveInteger" } }, { "name": "name", "constraints": { "required": true, "minLength": 1, "maxLength": 255 } }, { "name": "price", "constraints": { "required": true, "type": "http://www.w3.org/2001/XMLSchema#float", "minimum": "0.01", "maximum": "999999.99" } }, { "name": "sku", "constraints": { "required": true, "pattern": "^[A-Z]{3}-[0-9]{6}$" } }, { "name": "created_date", "constraints": { "required": true, "type": "http://www.w3.org/2001/XMLSchema#date", "datePattern": "%Y-%m-%d" } }, { "name": "category", "constraints": { "enum": ["electronics", "clothing", "home", "sports"] } } ] } ``` ``` -------------------------------- ### CSV on the Web Metadata Schema Source: https://context7.com/data-liberation-front/csvlint.rb/llms.txt Example of defining a schema using the W3C CSV on the Web standard format. ```APIDOC ## CSV on the Web Metadata Schema Define schemas using W3C CSV on the Web standard format with table groups and column definitions. ### Schema Example ```json { "@context": "http://www.w3.org/ns/csvw", "url": "http://example.com/products.csv", "tableSchema": { "columns": [ { "name": "id", "titles": "Product ID", "required": true, "datatype": { "base": "integer", "minimum": 1 } }, { "name": "name", "titles": "Product Name", "required": true, "datatype": { "base": "string", "minLength": 1, "maxLength": 255 } }, { "name": "price", "required": true, "datatype": { "base": "decimal", "minimum": 0 } }, { "name": "in_stock", "datatype": "boolean" } ], "primaryKey": "id" } } ``` ``` -------------------------------- ### Define Content-Type Header Source: https://github.com/data-liberation-front/csvlint.rb/blob/main/README.md Example of a Content-Type header indicating the presence of a header row as specified in RFC 4180. ```text Content-Type: text/csv; header=present ``` -------------------------------- ### Build Docker Image from Source Source: https://github.com/data-liberation-front/csvlint.rb/blob/main/docker_notes_for_windows.txt Builds a Docker image tagged as 'csvlint' from the source code. Ensure you are in the directory containing the Dockerfile. The trailing dot is significant. ```bash docker build -t csvlint . ``` -------------------------------- ### Run CSVW tests Source: https://github.com/data-liberation-front/csvlint.rb/blob/main/README.md Execute the CSV on the Web test suite from the command line. ```bash $ bin/run-csvw-tests ``` -------------------------------- ### Command Line Usage Source: https://github.com/data-liberation-front/csvlint.rb/wiki/Usage Instructions on how to validate CSV files using the csvlint command-line tool. ```APIDOC ## Command Line Usage After installing the gem, you can validate a CSV on the command line like so: ```bash csvlint myfile.csv ``` This will output the validation results, including any warnings or errors. Example Output: ``` myfile.csv is INVALID 1. blank_rows. Row: 3 1. title_row. 2. inconsistent_values. Column: 14 ``` You can also specify a schema file for validation: ```bash csvlint myfile.csv --schema=schema.json ``` ``` -------------------------------- ### Load Schema from String Source: https://context7.com/data-liberation-front/csvlint.rb/llms.txt Initialize a schema directly from a JSON string for dynamic or embedded configurations. ```ruby require 'csvlint' schema_json = <<-JSON { "fields": [ { "name": "id", "constraints": { "required": true, "type": "http://www.w3.org/2001/XMLSchema#integer" } }, { "name": "name", "constraints": { "required": true, "minLength": 1, "maxLength": 100 } }, { "name": "email", "constraints": { "required": true, "pattern": "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$" } } ] } JSON schema = Csvlint::Schema.load_from_string("inline-schema", schema_json) csv_data = "id,name,email\n1,John,john@example.com\n2,Jane,invalid-email" validator = Csvlint::Validator.new(StringIO.new(csv_data), nil, schema) puts validator.valid? # => false validator.errors.each do |error| puts "#{error.type} at row #{error.row}, column #{error.column}" end ``` -------------------------------- ### Run pre-commit manually Source: https://github.com/data-liberation-front/csvlint.rb/blob/main/README.md Command to trigger a manual run of pre-commit. ```bash pre-commit run -a ``` -------------------------------- ### Configure pre-commit hook Source: https://github.com/data-liberation-front/csvlint.rb/blob/main/README.md Configuration for using csvlint with the pre-commit framework. ```yaml repos: # `pre-commit autoupdate` to get latest available tags - repo: https://github.com/Data-Liberation-Front/csvlint.rb rev: v1.2.0 hooks: - id: csvlint ``` -------------------------------- ### Run tests with Rake Source: https://github.com/data-liberation-front/csvlint.rb/blob/main/README.md Execute the test suite using Rake tasks. ```bash $ rake ``` ```bash $ rake spec ``` ```bash $ rake features ``` -------------------------------- ### Configure CSV Dialect Source: https://github.com/data-liberation-front/csvlint.rb/wiki/Usage Pass a dialect hash to the constructor to configure how the CSV parser interprets the file structure. ```ruby dialect = { "header" => true, "delimiter" => "," } validator = Csvlint::Validator.new( "http://example.org/data.csv", dialect ) ``` -------------------------------- ### Initialize CSV Validator in Ruby Source: https://github.com/data-liberation-front/csvlint.rb/wiki/Usage Initialize the validator using different input sources such as URLs, files, or StringIO objects. ```ruby require 'csvlint' validator = Csvlint::Validator.new( "http://example.org/data.csv" ) validator = Csvlint::Validator.new( File.new("/path/to/my/data.csv" )) validator = Csvlint::Validator.new( StringIO.new( my_data_in_a_string ) ) ``` -------------------------------- ### Run CSV Lint Command Line Tool Source: https://github.com/data-liberation-front/csvlint.rb/blob/main/docker_notes_for_windows.txt Lints a specified CSV file using the csvlint command-line tool inside a Docker container. Mounts the current directory to /tmp and passes the CSV file path. ```bash docker run -it --rm -v %CD%:/tmp csvlint ./bin/csvlint --dump-errors /tmp/file-to-lint.csv ``` -------------------------------- ### Enter Linux Container Shell Source: https://github.com/data-liberation-front/csvlint.rb/blob/main/docker_notes_for_windows.txt Provides interactive access to the Linux container's bash shell. This is useful for exploring the container's environment or running commands directly. ```bash docker run -it --rm -v %CD%:/tmp csvlint bash ``` -------------------------------- ### Load Schema with Csvlint::Schema Source: https://context7.com/data-liberation-front/csvlint.rb/llms.txt Load JSON Table Schema or CSVW metadata from a URI or file path to enforce data constraints. ```ruby require 'csvlint' # Load schema from URL schema = Csvlint::Schema.load_from_uri("http://example.org/schema.json") # Load schema from local file schema = Csvlint::Schema.load_from_uri("/path/to/schema.json") # Validate CSV against schema validator = Csvlint::Validator.new( "http://example.org/data.csv", nil, # use default dialect schema ) ``` -------------------------------- ### Accessing Schema Metadata Source: https://context7.com/data-liberation-front/csvlint.rb/llms.txt Demonstrates how to access metadata properties of a loaded schema object. ```APIDOC ## Accessing Schema Metadata This section shows how to retrieve metadata associated with a CSVLint schema object. ### Example ```ruby puts schema.uri # => "http://example.com/schema.json" puts schema.title # => "My Data Schema" puts schema.description # => "Schema for validating data" puts schema.fields # => Array of Csvlint::Field objects ``` ``` -------------------------------- ### Validate Data Against Schema Source: https://github.com/data-liberation-front/csvlint.rb/blob/main/README.md Load a schema from a JSON URI and initialize the validator with the schema object. ```ruby schema = Csvlint::Schema.load_from_json(uri) validator = Csvlint::Validator.new( "http://example.org/data.csv", nil, schema ) ``` -------------------------------- ### Apply Validation Options and Callbacks Source: https://context7.com/data-liberation-front/csvlint.rb/llms.txt Control validation depth with line limits or monitor progress using lambda callbacks. ```ruby require 'csvlint' # Limit validation to first 100 lines (for large files) options = { limit_lines: 100 } validator = Csvlint::Validator.new( "http://example.org/large-data.csv", {}, # empty dialect (use defaults) nil, # no schema options ) # Progress callback for each validated line options = { lambda: ->(validator) { print validator.valid? ? "." : "!" puts " Line #{validator.current_line}" if validator.current_line % 1000 == 0 } } validator = Csvlint::Validator.new( File.new("/path/to/data.csv"), {}, nil, options ) # Output: ........ Line 1000 # ....!... Line 2000 ``` -------------------------------- ### Create and Validate Csvlint Field Source: https://context7.com/data-liberation-front/csvlint.rb/llms.txt Demonstrates how to create a Csvlint::Field object with various constraints and validate individual column values against these constraints. Use this to define and enforce rules for specific data fields. ```ruby require 'csvlint' # Create a field with multiple constraints field = Csvlint::Field.new( "email", # name { # constraints "required" => true, "pattern" => "^[^"]+@[^"]+\\\.[^"]+$", "minLength" => 5, "maxLength" => 254 }, "Email Address", # title (optional) "User's primary email" # description (optional) ) # Access field properties puts field.name # => "email" puts field.title # => "Email Address" puts field.description # => "User's primary email" puts field.constraints # => {"required"=>true, "pattern"=>...} # Validate a single column value field.validate_column("test@example.com", 1, 3, []) puts field.valid? # => true puts field.errors # => [] field.validate_column("invalid", 2, 3, []) puts field.valid? # => false puts field.errors.first.type # => :pattern ``` -------------------------------- ### Run Tests with Docker Source: https://github.com/data-liberation-front/csvlint.rb/blob/main/docker_notes_for_windows.txt Executes the tests for the csvlint project within a Docker container. This command runs the 'rake' task inside the 'csvlint' image. ```bash docker run -it --rm csvlint rake ``` -------------------------------- ### Initialize Csvlint::Validator Source: https://context7.com/data-liberation-front/csvlint.rb/llms.txt The core class for programmatic validation. It accepts URLs, files, or IO objects and performs validation upon instantiation. ```ruby require 'csvlint' # Validate from URL (includes HTTP header checks) validator = Csvlint::Validator.new("http://example.org/data.csv") # Validate from local file validator = Csvlint::Validator.new(File.new("/path/to/data.csv")) # Validate from string content csv_content = "name,age,email\nJohn,25,john@example.com\nJane,30,jane@example.com" validator = Csvlint::Validator.new(StringIO.new(csv_content)) # Check validation results puts validator.valid? # => true or false puts validator.errors # => Array of Csvlint::ErrorMessage objects puts validator.warnings # => Array of warning messages puts validator.info_messages # => Array of info messages # Access CSV metadata puts validator.encoding # => "UTF-8" puts validator.content_type # => "text/csv" (for URL sources) puts validator.extension # => ".csv" puts validator.row_count # => 3 puts validator.headers # => HTTP headers hash (for URL sources) puts validator.data # => Array of parsed row arrays ``` -------------------------------- ### Validate CSV with Schema Source: https://github.com/data-liberation-front/csvlint.rb/wiki/Usage Load a schema from a JSON URI and initialize a validator instance for a specific CSV file. ```ruby schema = Csvlint::Schema.load_from_json(uri) validator = Csvlint::Validator.new( "http://example.org/data.csv", nil, schema ) ``` -------------------------------- ### Validate CSV files via Command Line Interface Source: https://context7.com/data-liberation-front/csvlint.rb/llms.txt Use the csvlint command to validate local or remote CSV files, apply schemas, and format output. ```bash # Basic validation of a local file csvlint myfile.csv # Example output for invalid file: # myfile.csv is INVALID # 1. blank_rows. Row: 3 # 2. title_row. # 3. inconsistent_values. Column: 14 # Validate with a JSON Table Schema csvlint myfile.csv --schema=schema.json # Validate a remote CSV file csvlint http://example.org/data.csv # Output errors as JSON csvlint myfile.csv --json # Treat warnings as errors (exit code 1 if any warnings) csvlint myfile.csv --werror # Pretty print error objects for debugging csvlint myfile.csv --dump-errors # Validate from stdin cat myfile.csv | csvlint ``` -------------------------------- ### Validate CSV via Command Line Source: https://github.com/data-liberation-front/csvlint.rb/wiki/Usage Basic command-line usage for validating a CSV file, with an optional schema file. ```bash csvlint myfile.csv ``` ```bash csvlint myfile.csv --schema=schema.json ``` -------------------------------- ### Csvlint::Schema.load_from_string Source: https://context7.com/data-liberation-front/csvlint.rb/llms.txt Loads a schema definition directly from a JSON string. ```APIDOC ## Csvlint::Schema.load_from_string Load a schema directly from a JSON string, useful when the schema is embedded or generated dynamically. ### Usage ```ruby require 'csvlint' schema_json = <<-JSON { "fields": [ { "name": "id", "constraints": { "required": true, "type": "http://www.w3.org/2001/XMLSchema#integer" } }, { "name": "name", "constraints": { "required": true, "minLength": 1, "maxLength": 100 } }, { "name": "email", "constraints": { "required": true, "pattern": "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$" } } ] } JSON schema = Csvlint::Schema.load_from_string("inline-schema", schema_json) csv_data = "id,name,email\n1,John,john@example.com\n2,Jane,invalid-email" validator = Csvlint::Validator.new(StringIO.new(csv_data), nil, schema) puts validator.valid? # => false validator.errors.each do |error| puts "#{error.type} at row #{error.row}, column #{error.column}" end # Output: pattern at row 3, column 3 ``` ``` -------------------------------- ### Access Validation Results in Ruby Source: https://github.com/data-liberation-front/csvlint.rb/wiki/Usage Methods to retrieve validation status, error messages, warnings, and file metadata after running the validator. ```ruby #invoke the validation validator.validate #check validation status validator.valid? #access array of errors, each is an Csvlint::ErrorMessage object validator.errors #access array of warnings validator.warnings #access array of information messages validator.info_messages #get some information about the CSV file that was validated validator.encoding validator.content_type validator.extension validator.row_count #retrieve HTTP headers from request validator.headers ``` -------------------------------- ### Define CSV on the Web Metadata Source: https://github.com/data-liberation-front/csvlint.rb/blob/main/README.md A sample CSV on the Web metadata file structure for schema validation. ```json { "@context": "http://www.w3.org/ns/csvw", "url": "http://example.com/example1.csv", "tableSchema": { "columns": [ { "name": "id", "required": true, "datatype": { "base": "integer" } }, { "name": "price", "required": true, "datatype": { "base": "string", "minLength": 1 } }, { "name": "postcode", "required": true } ] } } ``` -------------------------------- ### Csvlint::Schema.load_from_uri Source: https://context7.com/data-liberation-front/csvlint.rb/llms.txt Loads a validation schema from a remote URL or local file path. ```APIDOC ## Csvlint::Schema.load_from_uri ### Description Loads a JSON Table Schema or CSVW metadata file to be used for validating CSV content. ### Parameters #### Arguments - **uri** (String) - Required - The URL or file path to the schema definition. ### Request Example ```ruby schema = Csvlint::Schema.load_from_uri("http://example.org/schema.json") ``` ### Response #### Success Response (Object) - **schema** (Csvlint::Schema) - A schema object ready to be passed to the Csvlint::Validator. ``` -------------------------------- ### Controlling CSV Parsing (Dialect) Source: https://github.com/data-liberation-front/csvlint.rb/wiki/Usage How to configure the CSV parsing dialect when creating a validator instance. ```APIDOC ## Controlling CSV Parsing (Dialect) The validator allows configuration of the CSV Dialect by passing a dialect hash to the constructor. ### Dialect Configuration ```ruby dialect = { "header" => true, "delimiter" => "," } validator = Csvlint::Validator.new( "http://example.org/data.csv", dialect ) ``` The dialect hash should conform to the [CSV Dialect JSON structure](http://dataprotocols.org/csv-dialect/). Note that even with custom dialect settings, the validator will still report errors or warnings for invalid CSV structures (e.g., missing headers, inconsistent delimiters). The parser also checks the `header` parameter in the `Content-Type` header for remote CSV files, as specified in [RFC 4180](http://www.ietf.org/rfc/rfc4180.txt). Example `Content-Type` header: ``` Content-Type: text/csv; header=present ``` ``` -------------------------------- ### Csvlint::Validator.new Source: https://context7.com/data-liberation-front/csvlint.rb/llms.txt The primary class for initializing CSV validation. It accepts a source, optional dialect, schema, and configuration options. ```APIDOC ## Csvlint::Validator.new ### Description Initializes a new validator instance and automatically performs validation on the provided source. ### Parameters #### Arguments - **source** (String/File/IO) - Required - The CSV source (URL, file path, or IO object). - **dialect** (Hash) - Optional - Configuration for CSV parsing (e.g., delimiter, header, quoteChar). - **schema** (Csvlint::Schema) - Optional - A schema object for content validation. - **options** (Hash) - Optional - Additional settings like `limit_lines` or `lambda` (callback). ### Request Example ```ruby validator = Csvlint::Validator.new("http://example.org/data.csv", { "delimiter" => "," }, nil, { limit_lines: 100 }) ``` ### Response #### Success Response (Object) - **valid?** (Boolean) - Returns true if the CSV is valid. - **errors** (Array) - List of Csvlint::ErrorMessage objects. - **warnings** (Array) - List of warning messages. - **info_messages** (Array) - List of informational messages. ``` -------------------------------- ### Define CSV on the Web Metadata Source: https://context7.com/data-liberation-front/csvlint.rb/llms.txt Configure table schemas using the W3C CSV on the Web standard. ```json { "@context": "http://www.w3.org/ns/csvw", "url": "http://example.com/products.csv", "tableSchema": { "columns": [ { "name": "id", "titles": "Product ID", "required": true, "datatype": { "base": "integer", "minimum": 1 } }, { "name": "name", "titles": "Product Name", "required": true, "datatype": { "base": "string", "minLength": 1, "maxLength": 255 } }, { "name": "price", "required": true, "datatype": { "base": "decimal", "minimum": 0 } }, { "name": "in_stock", "datatype": "boolean" } ], "primaryKey": "id" } } ``` -------------------------------- ### CSVW TableGroup Validation with Csvlint Source: https://context7.com/data-liberation-front/csvlint.rb/llms.txt Shows how to load CSVW metadata from a string and use it to validate multiple related CSV files. This is useful for complex datasets with foreign key relationships. ```ruby require 'csvlint' # CSVW metadata defining two related tables csvw_metadata = <<-JSON { "@context": "http://www.w3.org/ns/csvw", "tables": [ { "url": "file:///path/to/orders.csv", "tableSchema": { "columns": [ {"name": "order_id", "datatype": "integer", "required": true}, {"name": "customer_id", "datatype": "integer", "required": true}, {"name": "total", "datatype": "decimal"} ], "foreignKeys": [{ "columnReference": "customer_id", "reference": { "resource": "file:///path/to/customers.csv", "columnReference": "id" } }] } }, { "url": "file:///path/to/customers.csv", "tableSchema": { "columns": [ {"name": "id", "datatype": "integer", "required": true}, {"name": "name", "datatype": "string", "required": true} ], "primaryKey": "id" } } ] } JSON schema = Csvlint::Schema.load_from_string("metadata.json", csvw_metadata) # Schema is a TableGroup with multiple tables puts schema.class # => Csvlint::Csvw::TableGroup puts schema.tables.keys # => ["file:///path/to/orders.csv", "file:///path/to/customers.csv"] # Validate each table in the group schema.tables.each do |url, table| validator = Csvlint::Validator.new(url, nil, schema) puts "#{url}: #{validator.valid? ? 'VALID' : 'INVALID'}" end ``` -------------------------------- ### Define JSON Table Schema Source: https://github.com/data-liberation-front/csvlint.rb/blob/main/README.md A sample JSON Table Schema configuration defining field constraints for validation. ```json { "fields": [ { "name": "id", "constraints": { "required": true, "type": "http://www.w3.org/TR/xmlschema-2/#integer" } }, { "name": "price", "constraints": { "required": true, "minLength": 1 } }, { "name": "postcode", "constraints": { "required": true, "pattern": "[A-Z]{1,2}[0-9][0-9A-Z]? ?[0-9][A-Z]{2}" } } ] } ``` -------------------------------- ### Configure CSV Dialect for Validator Source: https://context7.com/data-liberation-front/csvlint.rb/llms.txt Define custom parsing rules like delimiters and line terminators to handle non-standard CSV formats. ```ruby require 'csvlint' # Custom dialect for tab-separated values without header dialect = { "header" => false, # No header row "delimiter" => "\t", # Tab-separated "quoteChar" => "'", # Single quote for quoting "skipInitialSpace" => true, # Skip spaces after delimiter "lineTerminator" => "\r\n" # Windows line endings } validator = Csvlint::Validator.new( File.new("/path/to/data.tsv"), dialect ) # Check if header was detected puts validator.header? # => false # Access dialect settings used puts validator.dialect # => {"header"=>false, "delimiter"=>"\t", ...} ``` -------------------------------- ### Access Schema Metadata Source: https://context7.com/data-liberation-front/csvlint.rb/llms.txt Retrieve core properties from a loaded schema object. ```ruby puts schema.uri # => "http://example.org/schema.json" puts schema.title # => "My Data Schema" puts schema.description # => "Schema for validating data" puts schema.fields # => Array of Csvlint::Field objects ``` -------------------------------- ### Configure Csvlint as a Pre-commit Hook Source: https://context7.com/data-liberation-front/csvlint.rb/llms.txt Provides configuration for integrating csvlint into a git pre-commit workflow to automatically validate CSV files before commits. This ensures data integrity at the source. ```yaml # .pre-commit-config.yaml repos: - repo: https://github.com/Data-Liberation-Front/csvlint.rb rev: v1.2.0 hooks: - id: csvlint # Optional: only validate specific files files: \.csv$ # Optional: exclude test fixtures exclude: ^(test|spec)/fixtures/ ``` ```bash # Install pre-commit hooks pre-commit install # Run manually on all files pre-commit run -a # Run on staged files only pre-commit run csvlint ``` -------------------------------- ### Ruby API Usage Source: https://github.com/data-liberation-front/csvlint.rb/wiki/Usage How to use the csvlint gem within your Ruby code to validate CSV data from various sources. ```APIDOC ## Ruby API Usage The gem can be used programmatically in Ruby applications. It supports validating CSV data from URLs, File objects, or IO-style objects like StringIO. ### Initialization ```ruby require 'csvlint' # From a URL validator = Csvlint::Validator.new( "http://example.org/data.csv" ) # From a File object validator = Csvlint::Validator.new( File.new("/path/to/my/data.csv") ) # From a StringIO object validator = Csvlint::Validator.new( StringIO.new( my_data_in_a_string ) ) ``` When validating from a URL, additional checks on HTTP headers are performed. ### Validation and Results ```ruby # Invoke the validation validator.validate # Check validation status (returns true if valid, false otherwise) validator.valid? # Access validation errors (returns an array of Csvlint::ErrorMessage objects) validator.errors # Access validation warnings validator.warnings # Access information messages validator.info_messages ``` ### File Information ```ruby # Get information about the validated CSV file validator.encoding validator.content_type validator.extension validator.row_count ``` ### HTTP Headers ```ruby # Retrieve HTTP headers from the request (if validating from a URL) validator.headers ``` ``` -------------------------------- ### Refresh CSVW tests Source: https://github.com/data-liberation-front/csvlint.rb/blob/main/README.md Commands to remove existing test files and reload them. ```bash $ rm bin/run-csvw-tests $ rm features/csvw_validation_tests.feature $ rm -r features/fixtures/csvw ``` ```bash $ ruby features/support/load_tests.rb ``` -------------------------------- ### Warning Types Source: https://github.com/data-liberation-front/csvlint.rb/wiki/Usage List of possible warnings that can be reported by the validator. ```APIDOC ## Warnings The following types of warnings can be reported: * `:no_encoding`: The `Content-Type` header lacks a `charset` parameter. * `:encoding`: The character set is not UTF-8. * `:no_content_type`: The file is served without a `Content-Type` header. * `:excel`: No `Content-Type` header and the file extension is `.xls`. * `:check_options`: The CSV file appears to contain only a single column. * `:inconsistent_values`: Inconsistent data types found in the same column (less than 90% match). * `:empty_column_name`: A column in the CSV header has an empty name. * `:duplicate_column_name`: A column in the CSV header has a duplicate name. * `:title_row`: The first row appears to be a title row rather than headers. ``` -------------------------------- ### Callback on Line Validation Source: https://github.com/data-liberation-front/csvlint.rb/blob/main/README.md Utilize the `:lambda` option to provide a block of code that executes for each validated line, allowing access to the Validator object, such as retrieving the current line number. ```ruby options = { lambda: ->(validator) { puts validator.current_line } } validator = Csvlint::Validator.new( "http://example.org/data.csv", nil, nil, options ) => 1 2 3 4 ..... ``` -------------------------------- ### Limit CSV Lines for Validation Source: https://github.com/data-liberation-front/csvlint.rb/wiki/Usage Use the `:limit_lines` option to specify the maximum number of lines to validate. This is useful for quickly checking large CSV files. ```ruby options = { limit_lines: 100 } validator = Csvlint::Validator.new( "http://example.org/data.csv", nil, nil, options ) ``` -------------------------------- ### Pre-commit Hook Integration Source: https://context7.com/data-liberation-front/csvlint.rb/llms.txt Configure csvlint as a pre-commit hook for automated CSV validation in git repositories. This ensures that CSV files are validated before they are committed. ```APIDOC ## Pre-commit Hook Integration ### Description Configure csvlint as a pre-commit hook for automated CSV validation in git repositories. ### Configuration (`.pre-commit-config.yaml`) ```yaml repos: - repo: https://github.com/Data-Liberation-Front/csvlint.rb rev: v1.2.0 hooks: - id: csvlint # Optional: only validate specific files files: \.csv$ # Optional: exclude test fixtures exclude: ^(test|spec)/fixtures/ ``` ### Usage 1. **Install pre-commit hooks:** ```bash pre-commit install ``` 2. **Run manually on all files:** ```bash pre-commit run -a ``` 3. **Run on staged files only:** ```bash pre-commit run csvlint ``` ``` -------------------------------- ### Custom Line Validation with Lambda Source: https://github.com/data-liberation-front/csvlint.rb/wiki/Usage Provide a lambda function using the `:lambda` option to execute custom code for each validated line. The `Validator` object is accessible within the lambda, allowing access to properties like `current_line`. ```ruby options = { lambda: ->(validator) { puts validator.current_line } } validator = Csvlint::Validator.new( "http://example.org/data.csv", nil, nil, options ) ``` -------------------------------- ### Handle Csvlint Error Messages Source: https://context7.com/data-liberation-front/csvlint.rb/llms.txt Iterate through validation errors to access detailed context like row, column, and constraint failure information. ```ruby require 'csvlint' csv_data = <<-CSV id,name,email 1,John,john@example.com ,Jane,jane@example.com 3,,invalid-email CSV schema_json = '{"fields":[{"name":"id","constraints":{"required":true}},{"name":"name","constraints":{"required":true}},{"name":"email","constraints":{"pattern":"^.+@.+\\..+$"}}]}' schema = Csvlint::Schema.load_from_string("test", schema_json) validator = Csvlint::Validator.new(StringIO.new(csv_data), nil, schema) # Iterate through all errors validator.errors.each do |error| puts "Type: #{error.type}" # => :missing_value, :pattern puts "Category: #{error.category}" # => :schema, :structure, :context puts "Row: #{error.row}" # => Line number (1-based) puts "Column: #{error.column}" # => Column number (1-based) puts "Content: #{error.content}" # => Row content that caused error puts "Constraints: #{error.constraints}" # => Failed constraint details puts "---" end ``` -------------------------------- ### Full CSV Validation with Error Handling Source: https://context7.com/data-liberation-front/csvlint.rb/llms.txt This function validates a CSV file against an optional schema, capturing detailed errors, warnings, and info messages. It includes basic file not found and schema error handling. Use this for comprehensive programmatic CSV validation. ```ruby require 'csvlint' require 'json' def validate_csv_file(csv_path, schema_path = nil) results = { file: csv_path, valid: false, row_count: 0, errors: [], warnings: [], info: [] } begin # Load schema if provided schema = nil if schema_path schema = Csvlint::Schema.load_from_uri(schema_path) end # Create validator with progress tracking error_count = 0 options = { lambda: ->(v) { if v.errors.count > error_count $stderr.print "E" error_count = v.errors.count else $stderr.print "." end } } source = File.new(csv_path) validator = Csvlint::Validator.new(source, {}, schema, options) $stderr.puts results[:valid] = validator.valid? results[:row_count] = validator.row_count results[:encoding] = validator.encoding # Collect errors with details validator.errors.each do |e| results[:errors] << { type: e.type, category: e.category, row: e.row, column: e.column, message: format_error_message(e) } end # Collect warnings validator.warnings.each do |w| results[:warnings] << { type: w.type, category: w.category, row: w.row, column: w.column } end # Collect info messages validator.info_messages.each do |i| results[:info] << { type: i.type, category: i.category } end rescue Errno::ENOENT results[:errors] << { type: :file_not_found, message: "File not found: #{csv_path}" } rescue Csvlint::Csvw::MetadataError => e results[:errors] << { type: :invalid_schema, message: e.message } end results end def format_error_message(error) case error.type when :missing_value "Required value missing" when :pattern "Value doesn't match pattern: #{error.constraints['pattern']}" when :min_length "Value too short (min: #{error.constraints['minLength']})" when :max_length "Value too long (max: #{error.constraints['maxLength']})" when :invalid_type "Invalid type (expected: #{error.constraints['type']})" when :ragged_rows "Inconsistent column count" when :blank_rows "Empty row" else error.type.to_s.gsub('_', ' ').capitalize end end # Usage results = validate_csv_file("data.csv", "schema.json") puts JSON.pretty_generate(results) # Exit with appropriate code exit(results[:valid] ? 0 : 1) ``` -------------------------------- ### Error Types Source: https://github.com/data-liberation-front/csvlint.rb/wiki/Usage List of possible errors that can be reported by the validator. ```APIDOC ## Errors The following types of errors can be reported: * `:wrong_content_type`: The content type is not `text/csv`. * `:ragged_rows`: A row has a different number of columns than the first row. * `:blank_rows`: The row is completely empty. * `:invalid_encoding`: An encoding error occurred while parsing the row. * `:not_found`: HTTP 404 error when retrieving data from a URL. * `:stray_quote`: A missing or stray quote character was found. * `:unclosed_quote`: A quoted field is not properly closed. * `:whitespace`: A quoted column contains leading or trailing whitespace. * `:line_breaks`: Inconsistent or incorrectly specified line breaks. ``` -------------------------------- ### Define JSON Table Schema Source: https://context7.com/data-liberation-front/csvlint.rb/llms.txt Structure field constraints including types, length limits, and patterns using the JSON Table Schema format. ```json { "title": "Product Catalog Schema", "description": "Schema for validating product data exports", "fields": [ { "name": "product_id", "title": "Product ID", "description": "Unique product identifier", "constraints": { "required": true, "unique": true, "type": "http://www.w3.org/2001/XMLSchema#positiveInteger" } }, { "name": "name", "constraints": { "required": true, "minLength": 1, "maxLength": 255 } }, { "name": "price", "constraints": { "required": true, "type": "http://www.w3.org/2001/XMLSchema#float", "minimum": "0.01", "maximum": "999999.99" } }, { "name": "sku", "constraints": { "required": true, "pattern": "^[A-Z]{3}-[0-9]{6}$" } }, { "name": "created_date", "constraints": { "required": true, "type": "http://www.w3.org/2001/XMLSchema#date", "datePattern": "%Y-%m-%d" } }, { "name": "category", "constraints": { "enum": ["electronics", "clothing", "home", "sports"] } } ] } ``` -------------------------------- ### Csvlint::Field Source: https://context7.com/data-liberation-front/csvlint.rb/llms.txt Represents a schema field with constraints for validating individual column values. You can create a field with various constraints like 'required', 'pattern', 'minLength', and 'maxLength', and then use the 'validate_column' method to check if a given value conforms to these constraints. ```APIDOC ## Csvlint::Field ### Description Represents a schema field with constraints for validating individual column values. ### Usage ```ruby require 'csvlint' # Create a field with multiple constraints field = Csvlint::Field.new( "email", # name { # constraints "required" => true, "pattern" => "^[^@]+@[^@]+\\.[^@]+$", "minLength" => 5, "maxLength" => 254 }, "Email Address", # title (optional) "User's primary email" # description (optional) ) # Access field properties puts field.name # => "email" puts field.title # => "Email Address" puts field.description # => "User's primary email" puts field.constraints # => {"required"=>true, "pattern"=>...} # Validate a single column value field.validate_column("test@example.com", 1, 3, []) puts field.valid? # => true puts field.errors # => [] field.validate_column("invalid", 2, 3, []) puts field.valid? # => false puts field.errors.first.type # => :pattern ``` ### Properties - **name** (String): The name of the field. - **title** (String, optional): A human-readable title for the field. - **description** (String, optional): A detailed description of the field. - **constraints** (Hash): A hash containing validation rules for the field. - **required** (Boolean): Whether the field is mandatory. - **pattern** (String): A regular expression pattern the field value must match. - **minLength** (Integer): The minimum allowed length for the field value. - **maxLength** (Integer): The maximum allowed length for the field value. ### Methods - **validate_column(value, row_number, column_number, errors)**: Validates a single column value against the field's constraints. Modifies the `errors` array if validation fails. ``` -------------------------------- ### Csvlint::ErrorMessage Source: https://context7.com/data-liberation-front/csvlint.rb/llms.txt Details on accessing and interpreting validation error messages provided by CSVLint. ```APIDOC ## Csvlint::ErrorMessage Error, warning, and info messages provide detailed context about validation issues including location and content. ### Usage ```ruby require 'csvlint' csv_data = <<-CSV id,name,email 1,John,john@example.com ,Jane,jane@example.com 3,,invalid-email CSV schema_json = '{"fields":[{"name":"id","constraints":{"required":true}},{"name":"name","constraints":{"required":true}},{"name":"email","constraints":{"pattern":"^.+@.+\\..$"}}]}"' schema = Csvlint::Schema.load_from_string("test", schema_json) validator = Csvlint::Validator.new(StringIO.new(csv_data), nil, schema) # Iterate through all errors validator.errors.each do |error| puts "Type: #{error.type}" # => :missing_value, :pattern puts "Category: #{error.category}" # => :schema, :structure, :context puts "Row: #{error.row}" # => Line number (1-based) puts "Column: #{error.column}" # => Column number (1-based) puts "Content: #{error.content}" # => Row content that caused error puts "Constraints: #{error.constraints}" # => Failed constraint details puts "---" end # Example output: # Type: missing_value # Category: schema # Row: 3 # Column: 1 # Content: # Constraints: {"required"=>true} # --- # Type: missing_value # Category: schema # Row: 4 # Column: 2 # Content: # Constraints: {"required"=>true} ``` ``` -------------------------------- ### Error Reporting Source: https://github.com/data-liberation-front/csvlint.rb/wiki/Usage Details on how validation results are reported using Csvlint::ErrorMessage objects. ```APIDOC ## Error Reporting Validation results are provided via `Csvlint::ErrorMessage` objects, categorized into errors, warnings, and information messages. A validation is considered successful if no errors are present. Each message object contains the following context: * `category`: A symbol indicating the error category (e.g., `:structure`, `:schema`, `:context`). * `type`: A symbol indicating the specific type of error or warning. * `row`: The line number where the issue occurred. * `column`: The column number where the issue occurred. * `content`: The content of the row that generated the message. ```