-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate.go
More file actions
249 lines (246 loc) · 7.2 KB
/
Copy pathvalidate.go
File metadata and controls
249 lines (246 loc) · 7.2 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package codemeta
import (
"fmt"
"sort"
"strings"
)
type validator struct {
diagnostics []Diagnostic
limit int
truncated bool
}
// Validate returns diagnostics in source order, capped including a truncation entry.
func (d *Document) Validate() []Diagnostic {
if d == nil {
return nil
}
limit := d.maxDiagnostics
if limit == 0 {
limit = 100
}
c := validator{limit: limit}
c.context(d.root.Get(keywordContext), keywordContext, d.root.pos)
var terms map[string]termDefinition
if d.context.usable() {
terms = contextTerms[d.context.version]
}
c.object(d.root, "", terms)
if c.truncated {
last := c.diagnostics[len(c.diagnostics)-1]
c.diagnostics[len(c.diagnostics)-1] = Diagnostic{Code: "diagnostic_limit", Path: last.Path, Message: "further diagnostics omitted", Position: last.Position}
}
return c.diagnostics
}
func diagnosticLess(a, b Diagnostic) bool {
if a.Line != b.Line {
return a.Line < b.Line
}
if a.Column != b.Column {
return a.Column < b.Column
}
if a.Path != b.Path {
return a.Path < b.Path
}
return a.Code < b.Code
}
func (c *validator) add(pos Position, path, code, message string) {
d := Diagnostic{Position: pos, Path: path, Code: code, Message: message}
i := sort.Search(len(c.diagnostics), func(i int) bool { return !diagnosticLess(c.diagnostics[i], d) })
if len(c.diagnostics) < c.limit {
c.diagnostics = append(c.diagnostics, Diagnostic{})
} else {
c.truncated = true
if i == len(c.diagnostics) {
return
}
}
copy(c.diagnostics[i+1:], c.diagnostics[i:len(c.diagnostics)-1])
c.diagnostics[i] = d
}
func (c *validator) context(v Value, path string, fallback Position) {
state := inspectContext(v)
pos := v.pos
if pos.Line == 0 {
pos = fallback
}
if state.missing {
c.add(pos, path, "missing_context", "@context is missing")
}
if state.unknown || (state.version == "" && !state.missing && !state.modified && !state.conflicting) {
c.add(pos, path, "unsupported_version", "context is not one of the pinned CodeMeta contexts")
}
if state.modified {
c.add(pos, path, "context_modified", "inline context definitions cannot be applied using the pinned tables")
}
if state.conflicting {
c.add(pos, path, "context_conflict", "context combines different CodeMeta versions")
}
}
func fieldPath(parent, name string) string {
if parent == "" {
return name
}
return parent + "." + name
}
func itemPath(parent string, i int) string { return fmt.Sprintf("%s[%d]", parent, i) }
func (c *validator) object(v Value, path string, terms map[string]termDefinition) {
if path != "" {
if local := v.Get(keywordContext); local.kind != Missing {
if local.kind != Object || !unchangedDefinitions(local, terms) {
c.context(local, fieldPath(path, keywordContext), v.pos)
}
terms = scopedTerms(v, terms)
}
}
seen := map[string]bool{}
for _, field := range v.fields {
if field.Name == keywordContext {
continue
}
childPath := fieldPath(path, field.Name)
name := expandIRI(field.Name, terms)
if seen[name] {
c.add(field.Position, childPath, "alias_conflict", "multiple keys resolve to the same term")
}
seen[name] = true
if strings.HasPrefix(name, "@") {
c.keyword(field.Value, name, childPath, terms)
continue
}
def, known := resolveTerm(field.Name, terms)
if terms != nil && !known {
code := "unknown_term"
if knownElsewhere(field.Name) {
code = "term_version"
}
c.add(field.Position, childPath, code, "term is not defined by the declared context")
}
c.property(field.Value, def, childPath, terms)
}
}
func knownElsewhere(name string) bool {
for _, terms := range contextTerms {
if _, ok := resolveTerm(name, terms); ok {
return true
}
}
return false
}
func (c *validator) property(v Value, def termDefinition, path string, terms map[string]termDefinition) {
if v.kind == Array {
for i, item := range v.items {
c.property(item, def, itemPath(path, i), terms)
}
return
}
if v.kind == Null {
return
}
if v.kind == Object {
if list := v.Get(keywordList); list.kind != Missing {
c.property(list, def, fieldPath(path, keywordList), terms)
c.container(v, keywordList, path, terms)
return
}
if set := v.Get(keywordSet); set.kind != Missing {
c.property(set, def, fieldPath(path, keywordSet), terms)
c.container(v, keywordSet, path, terms)
return
}
if literal := v.Get(keywordValue); literal.kind != Missing {
if literal.kind != Object && literal.kind != Array {
c.property(literal, def, fieldPath(path, keywordValue), terms)
}
c.object(v, path, terms)
return
}
}
reference := v.kind == String && def.coercion == keywordID
referenceObject := v.kind == Object && resolvedGet(v, keywordID, scopedTerms(v, terms)).kind == String
if def.expected != "" && !reference && !referenceObject && !matchesExpected(v, def.expected) {
c.add(v.pos, path, "value_type", "expected "+def.expected)
}
if v.kind == String {
c.stringValue(v, def, path)
}
if v.kind == Object {
if isAgentRange(def.expected) {
agent := Agent{value: v, terms: scopedTerms(v, terms)}
if agent.Kind() == AgentConflict {
c.add(v.pos, path, "agent_conflict", "agent has conflicting person and organization fields")
}
}
c.object(v, path, terms)
}
}
func (c *validator) container(v Value, key, path string, terms map[string]termDefinition) {
for _, field := range v.fields {
if field.Name == key {
continue
}
name := expandIRI(field.Name, terms)
childPath := fieldPath(path, field.Name)
if name != keywordIndex {
c.add(field.Position, childPath, "container_field", "list and set objects may only also contain @index")
}
if strings.HasPrefix(name, "@") {
c.keyword(field.Value, name, childPath, terms)
} else {
c.property(field.Value, termDefinition{}, childPath, terms)
}
}
}
func (c *validator) keyword(v Value, name, path string, terms map[string]termDefinition) {
switch name {
case keywordID:
if v.kind != String {
c.add(v.pos, path, "value_type", "@id must be a string")
}
case keywordType:
c.typeValue(v, path, terms)
case "@graph", "@included", "@reverse":
c.property(v, termDefinition{}, path, terms)
case keywordList, keywordSet:
c.property(v, termDefinition{}, path, terms)
case keywordValue:
if v.kind == Object || v.kind == Array {
c.add(v.pos, path, "value_type", "@value must be a scalar")
}
case "@language", "@direction", keywordIndex:
if v.kind != String {
c.add(v.pos, path, "value_type", name+" must be a string")
}
default:
c.add(v.pos, path, "unknown_keyword", "unknown JSON-LD keyword")
}
}
func (c *validator) typeValue(v Value, path string, terms map[string]termDefinition) {
if v.kind == Array {
for i, item := range v.items {
if item.kind != String {
c.add(item.pos, itemPath(path, i), "value_type", "@type items must be strings")
} else {
c.typeValue(item, itemPath(path, i), terms)
}
}
return
}
if v.kind != String {
c.add(v.pos, path, "value_type", "@type must be a string or an array of strings")
return
}
if terms == nil {
return
}
if _, ok := resolveTerm(v.text, terms); ok {
return
}
if iri := expandIRI(v.text, terms); strings.Contains(iri, ":") {
return
}
code := "unknown_type"
if knownElsewhere(v.text) {
code = "type_version"
}
c.add(v.pos, path, code, "type is not defined by the declared context")
}