Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change made to the constraint parameter in order that it is more cons… #140

Merged
merged 3 commits into from
Aug 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in csvlint.rb.gemspec
gemspec
gemspec
3 changes: 1 addition & 2 deletions lib/csvlint/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ def validate_header(header)
found_header = header.to_csv(:row_sep => '')
expected_header = @fields.map{ |f| f.name }.to_csv(:row_sep => '')
if found_header != expected_header
build_warnings(:malformed_header, :schema, 1, nil, found_header, expected_header)
build_warnings(:malformed_header, :schema, 1, nil, found_header, "expectedHeader" => expected_header)
end

return valid?
end

Expand Down
1 change: 0 additions & 1 deletion lib/csvlint/validate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,6 @@ def check_consistency
private

def parse_extension(source)
# byebug
case source
when File
return File.extname( source.path )
Expand Down
64 changes: 33 additions & 31 deletions spec/schema_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'spec_helper'

describe Csvlint::Schema do

it "should tolerate missing fields" do
schema = Csvlint::Schema.from_json_table("http://example.org", {})
expect( schema ).to_not be(nil)
Expand All @@ -16,38 +16,38 @@
expect( schema.fields[0].name ).to eql("test")
expect( schema.fields[0].constraints ).to eql({})
end

it "should validate against the schema" do
field = Csvlint::Field.new("test", { "required" => true } )
field2 = Csvlint::Field.new("test", { "minLength" => 3 } )
schema = Csvlint::Schema.new("http://example.org", [field, field2] )

expect( schema.validate_row( ["", "x"] ) ).to eql(false)
expect( schema.errors.size ).to eql(2)
expect( schema.errors.first.type).to eql(:missing_value)
expect( schema.errors.first.category).to eql(:schema)
expect( schema.errors.first.column).to eql(1)
expect( schema.validate_row( ["abc", "1234"] ) ).to eql(true)

end

it "should include validations for missing columns" do
minimum = Csvlint::Field.new("test", { "minLength" => 3 } )
required = Csvlint::Field.new("test2", { "required" => true } )
schema = Csvlint::Schema.new("http://example.org", [minimum, required] )

expect( schema.validate_row( ["abc", "x"] ) ).to eql(true)

expect( schema.validate_row( ["abc"] ) ).to eql(false)
expect( schema.errors.size ).to eql(1)
expect( schema.errors.first.type).to eql(:missing_value)

schema = Csvlint::Schema.new("http://example.org", [required, minimum] )
expect( schema.validate_row( ["abc"] ) ).to eql(false)
expect( schema.errors.size ).to eql(1)
expect( schema.errors.first.type).to eql(:min_length)
expect( schema.errors.first.type).to eql(:min_length)
end

it "should warn if the data has fewer columns" do
minimum = Csvlint::Field.new("test", { "minLength" => 3 } )
required = Csvlint::Field.new("test2", { "maxLength" => 5 } )
Expand All @@ -60,10 +60,10 @@
expect( schema.warnings.first.row).to eql(1)
expect( schema.warnings.first.column).to eql(2)

#no ragged row error
#no ragged row error
expect( schema.errors.size ).to eql(0)
end

it "should warn if the data has additional columns" do
minimum = Csvlint::Field.new("test", { "minLength" => 3 } )
required = Csvlint::Field.new("test2", { "required" => true } )
Expand All @@ -79,28 +79,28 @@
expect( schema.warnings[1].type).to eql(:extra_column)
expect( schema.warnings[1].column).to eql(4)

#no ragged row error
expect( schema.errors.size ).to eql(0)
end
#no ragged row error
expect( schema.errors.size ).to eql(0)
end

context "when validating header" do
it "should warn if column names are different to field names" do
minimum = Csvlint::Field.new("minimum", { "minLength" => 3 } )
required = Csvlint::Field.new("required", { "required" => true } )
schema = Csvlint::Schema.new("http://example.org", [minimum, required] )

expect( schema.validate_header(["minimum", "required"]) ).to eql(true)
expect( schema.warnings.size ).to eql(0)

expect( schema.validate_header(["wrong", "required"]) ).to eql(true)
expect( schema.warnings.size ).to eql(1)
expect( schema.warnings.first.row ).to eql(1)
expect( schema.warnings.first.type ).to eql(:malformed_header)
expect( schema.warnings.first.content ).to eql('wrong,required')
expect( schema.warnings.first.column ).to eql(nil)
expect( schema.warnings.first.category ).to eql(:schema)
expect( schema.warnings.first.constraints ).to eql('minimum,required')

expect schema.warnings.first.constraints.has_value?('minimum,required')
# expect( schema.warnings.first.constraints.values ).to eql(['minimum,required'])
expect( schema.validate_header(["minimum", "Required"]) ).to eql(true)
expect( schema.warnings.size ).to eql(1)

Expand All @@ -118,7 +118,8 @@
expect( schema.warnings.first.content ).to eql("minimum")
expect( schema.warnings.first.column ).to eql(nil)
expect( schema.warnings.first.category ).to eql(:schema)
expect( schema.warnings.first.constraints ).to eql('minimum,required')
expect schema.warnings.first.constraints.has_value?('minimum,required')
# expect( schema.warnings.first.constraints.values ).to eql(['minimum,required'])

end

Expand All @@ -133,19 +134,20 @@
expect( schema.warnings.first.content ).to eql("wrong,required")
expect( schema.warnings.first.column ).to eql(nil)
expect( schema.warnings.first.category ).to eql(:schema)
expect( schema.warnings.first.constraints ).to eql('minimum')
# expect( schema.warnings.first.constraints.values ).to eql('minimum')
expect( schema.warnings.first.constraints.has_value?('minimum'))

end

end

context "when parsing JSON Tables" do
before(:each) do

before(:each) do
@example=<<-EOL
{
"title": "Schema title",
"description": "schema",
"description": "schema",
"fields": [
{ "name": "ID", "constraints": { "required": true }, "title": "id", "description": "house identifier" },
{ "name": "Price", "constraints": { "required": true, "minLength": 1 } },
Expand All @@ -155,11 +157,11 @@
EOL
stub_request(:get, "http://example.com/example.json").to_return(:status => 200, :body => @example)
end

it "should create a schema from a pre-parsed JSON table" do
json = JSON.parse( @example )
schema = Csvlint::Schema.from_json_table("http://example.org", json)

expect( schema.uri ).to eql("http://example.org")
expect( schema.title ).to eql("Schema title")
expect( schema.description ).to eql("schema")
Expand All @@ -169,15 +171,15 @@
expect( schema.fields[0].title ).to eql("id")
expect( schema.fields[0].description ).to eql("house identifier")
end

it "should create a schema from a JSON Table URL" do
schema = Csvlint::Schema.load_from_json_table("http://example.com/example.json")
expect( schema.uri ).to eql("http://example.com/example.json")
expect( schema.fields.length ).to eql(3)
expect( schema.fields[0].name ).to eql("ID")
expect( schema.fields[0].constraints["required"] ).to eql(true)

end
end
end
end

end