From 5805171bdc3e2d60b073f6042c0c2e5ca96cd93c Mon Sep 17 00:00:00 2001 From: Leigh Dodds Date: Tue, 18 Feb 2014 10:35:53 +0000 Subject: [PATCH] Allow schema and fields to have title and description --- lib/csvlint/field.rb | 6 ++++-- lib/csvlint/schema.rb | 12 +++++++----- spec/schema_spec.rb | 8 +++++++- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/lib/csvlint/field.rb b/lib/csvlint/field.rb index 81656d53..8f109039 100644 --- a/lib/csvlint/field.rb +++ b/lib/csvlint/field.rb @@ -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 }, @@ -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 diff --git a/lib/csvlint/schema.rb b/lib/csvlint/schema.rb index 0476ca70..10112790 100644 --- a/lib/csvlint/schema.rb +++ b/lib/csvlint/schema.rb @@ -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 @@ -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) diff --git a/spec/schema_spec.rb b/spec/schema_spec.rb index 04c34c2d..b7947e82 100644 --- a/spec/schema_spec.rb +++ b/spec/schema_spec.rb @@ -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}" } } ] @@ -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