Class: Csvlint::Schema
- Inherits:
-
Object
- Object
- Csvlint::Schema
- Includes:
- ErrorCollector
- Defined in:
- lib/csvlint/schema.rb
Instance Attribute Summary collapse
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Attributes included from ErrorCollector
#errors, #info_messages, #warnings
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(uri, fields = [], title = nil, description = nil) ⇒ Schema
constructor
A new instance of Schema.
- #validate_header(header) ⇒ Object
- #validate_row(values, row = nil) ⇒ Object
Methods included from ErrorCollector
#build_errors, #build_info_messages, #build_warnings, #reset, #valid?
Constructor Details
#initialize(uri, fields = [], title = nil, description = nil) ⇒ Schema
Returns a new instance of Schema.
11 12 13 14 15 16 17 |
# File 'lib/csvlint/schema.rb', line 11 def initialize(uri, fields=[], title=nil, description=nil) @uri = uri @fields = fields @title = title @description = description reset end |
Instance Attribute Details
#description ⇒ Object (readonly)
Returns the value of attribute description.
9 10 11 |
# File 'lib/csvlint/schema.rb', line 9 def description @description end |
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
9 10 11 |
# File 'lib/csvlint/schema.rb', line 9 def fields @fields end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
9 10 11 |
# File 'lib/csvlint/schema.rb', line 9 def title @title end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
9 10 11 |
# File 'lib/csvlint/schema.rb', line 9 def uri @uri end |
Class Method Details
.from_json_table(uri, json) ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/csvlint/schema.rb', line 50 def Schema.from_json_table(uri, json) fields = [] json["fields"].each do |field_desc| 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, json["title"], json["description"] ) end |
.load_from_json_table(uri) ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/csvlint/schema.rb', line 59 def Schema.load_from_json_table(uri) begin json = JSON.parse( open(uri).read ) return Schema.from_json_table(uri,json) rescue return nil end end |
Instance Method Details
#validate_header(header) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/csvlint/schema.rb', line 19 def validate_header(header) reset header.each_with_index do |name,i| build_warnings(:header_name, :schema, nil, i+1, name) if fields[i].name != name end return valid? end |
#validate_row(values, row = nil) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/csvlint/schema.rb', line 27 def validate_row(values, row=nil) reset if values.length < fields.length fields[values.size..-1].each_with_index do |field, i| build_warnings(:missing_column, :schema, row, values.size+i+1) end end if values.length > fields.length values[fields.size..-1].each_with_index do |data_column, i| build_warnings(:extra_column, :schema, row, fields.size+i+1) end end fields.each_with_index do |field,i| value = values[i] || "" result = field.validate_column(value, row, i+1) @errors += fields[i].errors @warnings += fields[i].warnings end return valid? end |