Skip to content

Commit

Permalink
Merge pull request #57 from theodi/feature-schema-metadata
Browse files Browse the repository at this point in the history
Allow schema and fields to have title and description
  • Loading branch information
Floppy committed Feb 18, 2014
2 parents 2db7728 + 5805171 commit c251947
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
6 changes: 4 additions & 2 deletions lib/csvlint/field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Csvlint
class Field
include Csvlint::ErrorCollector

attr_reader :name, :constraints
attr_reader :name, :constraints, :title, :description

TYPE_VALIDATIONS = {
'http://www.w3.org/2001/XMLSchema#int' => lambda { |value| Integer value },
Expand Down Expand Up @@ -43,10 +43,12 @@ class Field
end
}

def initialize(name, constraints={})
def initialize(name, constraints={}, title=nil, description=nil)
@name = name
@constraints = constraints || {}
@uniques = Set.new
@title = title
@description = description
reset
end

Expand Down
12 changes: 7 additions & 5 deletions lib/csvlint/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ class Schema

include Csvlint::ErrorCollector

attr_reader :uri, :fields
attr_reader :uri, :fields, :title, :description

def initialize(uri, fields=[])
def initialize(uri, fields=[], title=nil, description=nil)
@uri = uri
@fields = fields
@title = title
@description = description
reset
end

Expand Down Expand Up @@ -48,10 +50,10 @@ def validate_row(values, row=nil)
def Schema.from_json_table(uri, json)
fields = []
json["fields"].each do |field_desc|
fields << Csvlint::Field.new( field_desc["name"] , field_desc["constraints"] )
fields << Csvlint::Field.new( field_desc["name"] , field_desc["constraints"],
field_desc["title"], field_desc["description"] )
end if json["fields"]

return Schema.new( uri , fields )
return Schema.new( uri , fields, json["title"], json["description"] )
end

def Schema.load_from_json_table(uri)
Expand Down
8 changes: 7 additions & 1 deletion spec/schema_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,10 @@
before(:each) do
@example=<<-EOL
{
"title": "Schema title",
"description": "schema",
"fields": [
{ "name": "ID", "constraints": { "required": true } },
{ "name": "ID", "constraints": { "required": true }, "title": "id", "description": "house identifier" },
{ "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}" } }
]
Expand All @@ -114,9 +116,13 @@
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")
expect( schema.fields.length ).to eql(3)
expect( schema.fields[0].name ).to eql("ID")
expect( schema.fields[0].constraints["required"] ).to eql(true)
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
Expand Down

0 comments on commit c251947

Please sign in to comment.