Skip to content

Commit

Permalink
Add schema errors to cli json (#184)
Browse files Browse the repository at this point in the history
* Add schema errors to cli json

* Move constraints into a nested object

* Map constraints in a > 2.1.0 friendly way

* Bump version
  • Loading branch information
pezholio committed May 24, 2016
1 parent 49da86a commit f8fa9e1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 5 deletions.
26 changes: 26 additions & 0 deletions features/cli.feature
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,32 @@ Feature: CSVlint CLI
And the output should contain "1. Id: min_length. Row: 2,2. 5"
And the output should contain "1. malformed_header. Row: 1. Bob,1234,[email protected]"

Scenario: Schema errors with JSON
Given I have a CSV with the following content:
"""
"Bob","1234","[email protected]"
"Alice","5","[email protected]"
"""
And it is stored at the url "http://example.com/example1.csv"
And I have a schema with the following content:
"""
{
"fields": [
{ "name": "Name", "constraints": { "required": true } },
{ "name": "Id", "constraints": { "required": true, "minLength": 3 } },
{ "name": "Email", "constraints": { "required": true } }
]
}
"""
And the schema is stored at the url "http://example.com/schema.json"
When I run `csvlint http://example.com/example1.csv --schema http://example.com/schema.json --json`
Then the output should contain JSON
And the JSON should have a state of "invalid"
And the JSON should have 1 error
And error 1 should have the "type" "min_length"
And error 1 should have the "header" "Id"
And error 1 should have the constraint "min_length" "3"

Scenario: Invalid schema
Given I have a CSV with the following content:
"""
Expand Down
10 changes: 9 additions & 1 deletion features/step_definitions/cli_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,19 @@
expect(@json['validation']['state']).to eq(state)
end

Then(/^the JSON should have (\d+) error$/) do |count|
Then(/^the JSON should have (\d+) errors?$/) do |count|
@index = count.to_i - 1
expect(@json['validation']['errors'].count).to eq(count.to_i)
end

Then(/^that error should have the "(.*?)" "(.*?)"$/) do |k, v|
expect(@json['validation']['errors'][@index][k].to_s).to eq(v)
end

Then(/^error (\d+) should have the "(.*?)" "(.*?)"$/) do |index, k, v|
expect(@json['validation']['errors'][index.to_i - 1][k].to_s).to eq(v)
end

Then(/^error (\d+) should have the constraint "(.*?)" "(.*?)"$/) do |index, k, v|
expect(@json['validation']['errors'][index.to_i - 1]['constraints'][k].to_s).to eq(v)
end
18 changes: 15 additions & 3 deletions lib/csvlint/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
require 'pp'
require 'thor'

require 'active_support/inflector'

module Csvlint
class Cli < Thor

Expand Down Expand Up @@ -93,7 +95,9 @@ def print_error(index, error, dump, color)
end
output_string = "#{index+1}. "
if error.column && @schema && @schema.class == Csvlint::Schema
output_string += "#{@schema.fields[error.column - 1].name}: "
if @schema.fields[error.column - 1] != nil
output_string += "#{@schema.fields[error.column - 1].name}: "
end
end
output_string += "#{error.type}"
output_string += ". #{location}" unless location.empty?
Expand Down Expand Up @@ -167,12 +171,20 @@ def validate_csv(source, schema, dump, json)
end

def hashify(error)
{
h = {
type: error.type,
category: error.category,
row: error.row,
col: error.column,
col: error.column
}

if error.column && @schema && @schema.class == Csvlint::Schema && @schema.fields[error.column - 1] != nil
field = @schema.fields[error.column - 1]
h[:header] = field.name
h[:constraints] = Hash[field.constraints.map {|k,v| [k.underscore, v] }]
end

h
end

def report_lines
Expand Down
2 changes: 1 addition & 1 deletion lib/csvlint/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Csvlint
VERSION = "0.3.1"
VERSION = "0.3.2"
end

0 comments on commit f8fa9e1

Please sign in to comment.