Skip to content

Commit

Permalink
Treat floats and ints as numeric
Browse files Browse the repository at this point in the history
  • Loading branch information
pezholio committed Feb 27, 2014
1 parent 4b9f035 commit 485352d
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 30 deletions.
56 changes: 41 additions & 15 deletions lib/csvlint/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,55 @@ module Csvlint
module Types

SIMPLE_FORMATS = {
'http://www.w3.org/2001/XMLSchema#string' => lambda { |value, constraints| value },
'http://www.w3.org/2001/XMLSchema#int' => lambda { |value, constraints| Integer value },
'http://www.w3.org/2001/XMLSchema#anyURI' => lambda do |value, constraints|
'string' => lambda { |value, constraints| value },
'numeric' => lambda do |value, constraints|
begin
Integer value
rescue ArgumentError
Float value
end
end,
'uri' => lambda do |value, constraints|
u = URI.parse value
raise ArgumentError unless u.kind_of?(URI::HTTP) || u.kind_of?(URI::HTTPS)
u
end,
'http://www.w3.org/2001/XMLSchema#dateTime' => lambda do |value, constraints|
'dateTime' => lambda do |value, constraints|
date_pattern = constraints["datePattern"] || "%Y-%m-%dT%H:%M:%SZ"
d = DateTime.strptime(value, date_pattern)
raise ArgumentError unless d.strftime(date_pattern) == value
d
end,
'http://www.w3.org/2001/XMLSchema#date' => lambda do |value, constraints|
'date' => lambda do |value, constraints|
date_pattern = constraints["datePattern"] || "%Y-%m-%d"
d = Date.strptime(value, date_pattern)
raise ArgumentError unless d.strftime(date_pattern) == value
d
end,
'http://www.w3.org/2001/XMLSchema#time' => lambda do |value, constraints|
'time' => lambda do |value, constraints|
date_pattern = constraints["datePattern"] || "%H:%M:%S"
d = DateTime.strptime(value, date_pattern)
raise ArgumentError unless d.strftime(date_pattern) == value
d
end,
}

TYPE_VALIDATIONS = SIMPLE_FORMATS.merge({
'http://www.w3.org/2001/XMLSchema#double' => lambda { |value, constraints| Float value },
TYPE_VALIDATIONS = {
'http://www.w3.org/2001/XMLSchema#string' => SIMPLE_FORMATS['string'],
'http://www.w3.org/2001/XMLSchema#int' => lambda { |value, constraints| Integer value },
'http://www.w3.org/2001/XMLSchema#float' => lambda { |value, constraints| Float value },
'http://www.w3.org/2001/XMLSchema#nonPositiveInteger' => lambda do |value, constraints|
i = Integer value
raise ArgumentError unless i <= 0
i
end,
'http://www.w3.org/2001/XMLSchema#double' => lambda { |value, constraints| Float value },
'http://www.w3.org/2001/XMLSchema#anyURI' => SIMPLE_FORMATS['uri'],
'http://www.w3.org/2001/XMLSchema#boolean' => lambda do |value, constraints|
return true if ['true', '1'].include? value
return false if ['false', '0'].include? value
raise ArgumentError
end,
'http://www.w3.org/2001/XMLSchema#nonPositiveInteger' => lambda do |value, constraints|
i = Integer value
raise ArgumentError unless i <= 0
i
end,
'http://www.w3.org/2001/XMLSchema#negativeInteger' => lambda do |value, constraints|
i = Integer value
raise ArgumentError unless i < 0
Expand All @@ -61,6 +70,24 @@ module Types
raise ArgumentError unless i > 0
i
end,
'http://www.w3.org/2001/XMLSchema#dateTime' => lambda do |value, constraints|
date_pattern = constraints["datePattern"] || "%Y-%m-%dT%H:%M:%SZ"
d = DateTime.strptime(value, date_pattern)
raise ArgumentError unless d.strftime(date_pattern) == value
d
end,
'http://www.w3.org/2001/XMLSchema#date' => lambda do |value, constraints|
date_pattern = constraints["datePattern"] || "%Y-%m-%d"
d = Date.strptime(value, date_pattern)
raise ArgumentError unless d.strftime(date_pattern) == value
d
end,
'http://www.w3.org/2001/XMLSchema#time' => lambda do |value, constraints|
date_pattern = constraints["datePattern"] || "%H:%M:%S"
d = DateTime.strptime(value, date_pattern)
raise ArgumentError unless d.strftime(date_pattern) == value
d
end,
'http://www.w3.org/2001/XMLSchema#gYear' => lambda do |value, constraints|
date_pattern = constraints["datePattern"] || "%Y"
d = Date.strptime(value, date_pattern)
Expand All @@ -73,8 +100,7 @@ module Types
raise ArgumentError unless d.strftime(date_pattern) == value
d
end
})

}
end

end
41 changes: 26 additions & 15 deletions spec/validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,23 @@
validator.build_formats(row, 1)
formats = validator.instance_variable_get("@formats")

formats[0].first.should == "http://www.w3.org/2001/XMLSchema#string"
formats[1].first.should == "http://www.w3.org/2001/XMLSchema#int"
formats[2].first.should == "http://www.w3.org/2001/XMLSchema#anyURI"
formats[3].first.should == "http://www.w3.org/2001/XMLSchema#dateTime"
formats[4].first.should == "http://www.w3.org/2001/XMLSchema#date"
formats[5].first.should == "http://www.w3.org/2001/XMLSchema#time"
formats[0].first.should == "string"
formats[1].first.should == "numeric"
formats[2].first.should == "uri"
formats[3].first.should == "dateTime"
formats[4].first.should == "date"
formats[5].first.should == "time"
end

it "treats floats and ints the same" do
row = ["12", "3.1476"]

validator = Csvlint::Validator.new("http://example.com/example.csv")
validator.build_formats(row, 1)
formats = validator.instance_variable_get("@formats")

formats[0].first.should == "numeric"
formats[1].first.should == "numeric"
end

it "should ignore blank arrays" do
Expand Down Expand Up @@ -130,9 +141,9 @@
formats = validator.instance_variable_get("@formats")

formats.should == [
["http://www.w3.org/2001/XMLSchema#string",
"http://www.w3.org/2001/XMLSchema#string",
"http://www.w3.org/2001/XMLSchema#string"]
["string",
"string",
"string"]
]
end

Expand All @@ -151,9 +162,9 @@
formats = validator.instance_variable_get("@formats")

formats.should == [
["http://www.w3.org/2001/XMLSchema#string"],
["http://www.w3.org/2001/XMLSchema#int"],
["http://www.w3.org/2001/XMLSchema#string"]
["string"],
["numeric"],
["string"]
]
end

Expand All @@ -163,9 +174,9 @@

it "should return a warning if columns have inconsistent values" do
formats = [
["http://www.w3.org/2001/XMLSchema#string", "http://www.w3.org/2001/XMLSchema#string", "http://www.w3.org/2001/XMLSchema#string"],
["http://www.w3.org/2001/XMLSchema#string", "http://www.w3.org/2001/XMLSchema#int", "http://www.w3.org/2001/XMLSchema#string"],
["http://www.w3.org/2001/XMLSchema#int", "http://www.w3.org/2001/XMLSchema#int", "http://www.w3.org/2001/XMLSchema#int"],
["string", "string", "string"],
["string", "numeric", "string"],
["numeric", "numeric", "numeric"],
]

validator = Csvlint::Validator.new("http://example.com/example.csv")
Expand Down

0 comments on commit 485352d

Please sign in to comment.