tableschema 0.3.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +21 -0
- data/.travis.yml +15 -1
- data/README.md +164 -129
- data/Rakefile +10 -1
- data/bin/console +2 -6
- data/{etc/schemas → lib/profiles}/geojson.json +0 -1
- data/lib/profiles/table-schema.json +1625 -0
- data/lib/profiles/topojson.json +311 -0
- data/lib/tableschema.rb +5 -3
- data/lib/tableschema/constraints/constraints.rb +12 -24
- data/lib/tableschema/constraints/enum.rb +6 -2
- data/lib/tableschema/constraints/max_length.rb +6 -2
- data/lib/tableschema/constraints/maximum.rb +12 -2
- data/lib/tableschema/constraints/min_length.rb +6 -2
- data/lib/tableschema/constraints/minimum.rb +12 -2
- data/lib/tableschema/constraints/pattern.rb +9 -2
- data/lib/tableschema/constraints/required.rb +6 -15
- data/lib/tableschema/constraints/unique.rb +12 -0
- data/lib/tableschema/defaults.rb +9 -0
- data/lib/tableschema/exceptions.rb +15 -2
- data/lib/tableschema/field.rb +39 -20
- data/lib/tableschema/helpers.rb +32 -15
- data/lib/tableschema/infer.rb +31 -28
- data/lib/tableschema/model.rb +57 -34
- data/lib/tableschema/schema.rb +40 -6
- data/lib/tableschema/table.rb +75 -26
- data/lib/tableschema/types/any.rb +1 -0
- data/lib/tableschema/types/array.rb +2 -1
- data/lib/tableschema/types/base.rb +9 -21
- data/lib/tableschema/types/date.rb +1 -0
- data/lib/tableschema/types/datetime.rb +1 -0
- data/lib/tableschema/types/duration.rb +31 -0
- data/lib/tableschema/types/geojson.rb +27 -5
- data/lib/tableschema/types/geopoint.rb +4 -3
- data/lib/tableschema/types/integer.rb +1 -0
- data/lib/tableschema/types/number.rb +40 -25
- data/lib/tableschema/types/object.rb +2 -1
- data/lib/tableschema/types/string.rb +8 -0
- data/lib/tableschema/types/time.rb +1 -0
- data/lib/tableschema/types/year.rb +34 -0
- data/lib/tableschema/types/yearmonth.rb +52 -0
- data/lib/tableschema/validate.rb +45 -29
- data/lib/tableschema/version.rb +1 -1
- data/tableschema.gemspec +2 -1
- metadata +31 -12
- data/etc/schemas/json-table-schema.json +0 -102
- data/lib/tableschema/data.rb +0 -60
- data/lib/tableschema/types/null.rb +0 -37
@@ -1,102 +0,0 @@
|
|
1
|
-
{
|
2
|
-
"$schema": "http://json-schema.org/draft-04/schema#",
|
3
|
-
"title": "JSON Table Schema",
|
4
|
-
"description": "JSON Schema for validating JSON Table structures",
|
5
|
-
"type": "object",
|
6
|
-
"properties": {
|
7
|
-
"fields": {
|
8
|
-
"type": "array",
|
9
|
-
"minItems": 1,
|
10
|
-
"items": {
|
11
|
-
"type": "object",
|
12
|
-
"properties": {
|
13
|
-
"name": {
|
14
|
-
"type": "string"
|
15
|
-
},
|
16
|
-
"title": {
|
17
|
-
"type": "string"
|
18
|
-
},
|
19
|
-
"description": {
|
20
|
-
"type": "string"
|
21
|
-
},
|
22
|
-
"type": {
|
23
|
-
"enum": [ "string", "number", "integer", "date", "time", "datetime", "boolean", "binary", "object", "geopoint", "geojson", "array", "any" ]
|
24
|
-
},
|
25
|
-
"format": {
|
26
|
-
"type": "string"
|
27
|
-
},
|
28
|
-
"constraints": {
|
29
|
-
"type": "object",
|
30
|
-
"properties": {
|
31
|
-
"required": {
|
32
|
-
"type": "boolean"
|
33
|
-
},
|
34
|
-
"minLength": {
|
35
|
-
"type": "integer"
|
36
|
-
},
|
37
|
-
"maxLength": {
|
38
|
-
"type": "integer"
|
39
|
-
},
|
40
|
-
"unique": {
|
41
|
-
"type": "boolean"
|
42
|
-
},
|
43
|
-
"pattern": {
|
44
|
-
"type": "string"
|
45
|
-
},
|
46
|
-
"minimum": {
|
47
|
-
"oneOf": [
|
48
|
-
{"type": "string"},
|
49
|
-
{"type": "number"}
|
50
|
-
]
|
51
|
-
},
|
52
|
-
"maximum": {
|
53
|
-
"oneOf": [
|
54
|
-
{"type": "string"},
|
55
|
-
{"type": "number"}
|
56
|
-
]
|
57
|
-
}
|
58
|
-
}
|
59
|
-
}
|
60
|
-
},
|
61
|
-
"required": ["name"]
|
62
|
-
}
|
63
|
-
},
|
64
|
-
"primaryKey": {
|
65
|
-
"oneOf": [
|
66
|
-
{"type": "string"},
|
67
|
-
{"type": "array"}
|
68
|
-
]
|
69
|
-
},
|
70
|
-
"foreignKeys": {
|
71
|
-
"type": "array",
|
72
|
-
"items": {
|
73
|
-
"type": "object",
|
74
|
-
"required": ["fields", "reference"],
|
75
|
-
"properties": {
|
76
|
-
"fields": {
|
77
|
-
"oneOf": [
|
78
|
-
{"type": "string"},
|
79
|
-
{"type": "array"}
|
80
|
-
]
|
81
|
-
},
|
82
|
-
"reference": {
|
83
|
-
"type": "object",
|
84
|
-
"required": ["resource", "fields"],
|
85
|
-
"properties": {
|
86
|
-
"resource": {
|
87
|
-
"type": "string"
|
88
|
-
},
|
89
|
-
"fields": {
|
90
|
-
"oneOf": [
|
91
|
-
{"type": "string"},
|
92
|
-
{"type": "array"}
|
93
|
-
]
|
94
|
-
}
|
95
|
-
}
|
96
|
-
}
|
97
|
-
}
|
98
|
-
}
|
99
|
-
}
|
100
|
-
},
|
101
|
-
"required": ["fields"]
|
102
|
-
}
|
data/lib/tableschema/data.rb
DELETED
@@ -1,60 +0,0 @@
|
|
1
|
-
module TableSchema
|
2
|
-
module Data
|
3
|
-
|
4
|
-
attr_reader :errors
|
5
|
-
|
6
|
-
def cast_rows(rows, fail_fast = true, limit = nil)
|
7
|
-
@errors ||= []
|
8
|
-
parsed_rows = []
|
9
|
-
rows.each_with_index do |r, i|
|
10
|
-
begin
|
11
|
-
break if limit && (limit <= i)
|
12
|
-
r = r.fields if r.class == CSV::Row
|
13
|
-
parsed_rows << cast_row(r, fail_fast)
|
14
|
-
rescue MultipleInvalid, ConversionError => e
|
15
|
-
raise e if fail_fast == true
|
16
|
-
@errors << e if e.is_a?(ConversionError)
|
17
|
-
end
|
18
|
-
end
|
19
|
-
check_for_errors
|
20
|
-
parsed_rows
|
21
|
-
end
|
22
|
-
|
23
|
-
alias_method :convert, :cast_rows
|
24
|
-
|
25
|
-
def cast_row(row, fail_fast = true)
|
26
|
-
@errors ||= []
|
27
|
-
raise_header_error(row) if row.count != fields.count
|
28
|
-
fields.each_with_index do |field,i|
|
29
|
-
row[i] = cast_column(field, row[i], fail_fast)
|
30
|
-
end
|
31
|
-
check_for_errors
|
32
|
-
row
|
33
|
-
end
|
34
|
-
|
35
|
-
alias_method :convert_row, :cast_row
|
36
|
-
|
37
|
-
private
|
38
|
-
|
39
|
-
def raise_header_error(row)
|
40
|
-
raise(TableSchema::ConversionError.new("The number of items to convert (#{row.count}) does not match the number of headers in the schema (#{fields.count})"))
|
41
|
-
end
|
42
|
-
|
43
|
-
def check_for_errors
|
44
|
-
raise(TableSchema::MultipleInvalid.new("There were errors parsing the data")) if @errors.count > 0
|
45
|
-
end
|
46
|
-
|
47
|
-
def cast_column(field, col, fail_fast)
|
48
|
-
field.cast_value(col)
|
49
|
-
rescue Exception => e
|
50
|
-
if fail_fast == true
|
51
|
-
raise e
|
52
|
-
else
|
53
|
-
@errors << e
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
alias_method :convert_column, :cast_column
|
58
|
-
|
59
|
-
end
|
60
|
-
end
|
@@ -1,37 +0,0 @@
|
|
1
|
-
module TableSchema
|
2
|
-
module Types
|
3
|
-
class Null < Base
|
4
|
-
|
5
|
-
def name
|
6
|
-
'null'
|
7
|
-
end
|
8
|
-
|
9
|
-
def self.supported_constraints
|
10
|
-
[
|
11
|
-
'required',
|
12
|
-
'pattern',
|
13
|
-
'enum',
|
14
|
-
]
|
15
|
-
end
|
16
|
-
|
17
|
-
def type
|
18
|
-
::NilClass
|
19
|
-
end
|
20
|
-
|
21
|
-
def null_values
|
22
|
-
['null', 'none', 'nil', 'nan', '-', '']
|
23
|
-
end
|
24
|
-
|
25
|
-
def cast_default(value)
|
26
|
-
if value.is_a?(type)
|
27
|
-
return value
|
28
|
-
elsif null_values.include?(value.to_s.downcase)
|
29
|
-
nil
|
30
|
-
else
|
31
|
-
raise TableSchema::InvalidCast.new("#{value} is not a #{name}")
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|