-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdataset.go
146 lines (125 loc) · 3.31 KB
/
dataset.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package validate
import (
"fmt"
"github.com/qri-io/dataset"
"github.com/qri-io/jsonschema"
)
// Dataset checks that a dataset is valid for use
// returning the first error encountered, nil if valid
func Dataset(ds *dataset.Dataset) error {
if ds == nil {
return nil
}
if ds.Commit == nil {
err := fmt.Errorf("commit is required")
log.Debug(err.Error())
return err
} else if err := Commit(ds.Commit); err != nil {
err := fmt.Errorf("commit: %s", err.Error())
log.Debug(err.Error())
return err
}
if ds.Structure != nil {
if err := Structure(ds.Structure); err != nil {
return fmt.Errorf("structure: %s", err.Error())
}
}
return nil
}
// Commit checks that a dataset Commit is valid for use
// returning the first error encountered, nil if valid
func Commit(cm *dataset.Commit) error {
if cm == nil {
return nil
}
if cm.Title == "" {
// return fmt.Errorf("title is required")
} else if len(cm.Title) > 100 {
return fmt.Errorf("title is too long. %d length exceeds 100 character limit", len(cm.Title))
}
return nil
}
// Structure checks that a dataset structure is valid for use
// returning the first error encountered, nil if valid
func Structure(s *dataset.Structure) error {
if s == nil {
return nil
}
df := s.DataFormat()
if df == dataset.UnknownDataFormat {
return fmt.Errorf("format is required")
} else if df == dataset.CSVDataFormat {
if s.Schema == nil {
return fmt.Errorf("csv data format requires a schema")
}
}
if err := Schema(s.Schema); err != nil {
return fmt.Errorf("schema: %s", err.Error())
}
return nil
}
// csvMetaSchema is a jsonschema for validating CSV schema definitions
var csvMetaSchema = jsonschema.Must(`{
"type": "object",
"properties": {
"type": {
"const": "array"
},
"items": {
"type": "object",
"properties": {
"type": {
"const": "array"
},
"items": {
"type": "array",
"items": {
"type": "object",
"minItems": 1,
"properties": {
"title": {
"type": "string"
},
"type": true
}
}
}
}
}
}
}`)
// jsonMetaSchema is a jsonschema for validating JSON schema definitions
// var jsonMetaSchema = jsonschema.Must(``)
// Schema checks that a dataset schema is valid for use
// returning the first error encountered, nil if valid
func Schema(sch map[string]interface{}) error {
if sch == nil {
return fmt.Errorf("schema is required")
}
// TODO (b5): Um, like, finish this
// if len(s.Fields) == 0 {
// return fmt.Errorf("fields are required")
// } else if err := Fields(s.Fields); err != nil {
// return fmt.Errorf("fields: %s", err.Error())
// }
return nil
}
// Fields checks that a slice of dataset fields is valid for use
// returning the first error encountered, nil if valid
// func Fields(fields []*dataset.Field) error {
// if fields == nil {
// return nil
// }
// checkedFieldNames := map[string]bool{}
// for _, field := range fields {
// if err := ValidName(field.Name); err != nil {
// return err
// }
// seen := checkedFieldNames[field.Name]
// if seen {
// return fmt.Errorf("error: cannot use the same name, '%s' more than once", field.Name)
// }
// checkedFieldNames[field.Name] = true
// }
// return nil
// }