forked from santhosh-tekuri/jsonschema
-
Notifications
You must be signed in to change notification settings - Fork 6
/
validation_context.go
50 lines (42 loc) · 1.18 KB
/
validation_context.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
package jsonschema
import (
"fmt"
"strconv"
"strings"
)
// ValidationErrorContext
type ValidationErrorContext interface {
AddContext(instancePtr, schemaPtr string)
FinishInstanceContext()
}
// ValidationErrorContextRequired is used as error context when one or more required properties are missing.
type ValidationErrorContextRequired struct {
// Missing contains JSON Pointers to all missing properties.
Missing []string
}
func (r *ValidationErrorContextRequired) AddContext(instancePtr, _ string) {
for k, p := range r.Missing {
r.Missing[k] = joinPtr(instancePtr, p)
}
}
func (r *ValidationErrorContextRequired) FinishInstanceContext() {
for k, p := range r.Missing {
if len(p) == 0 {
r.Missing[k] = "#"
} else {
r.Missing[k] = "#/" + p
}
}
}
func validationRequiredError(properties []string) *ValidationError {
missing := make([]string, len(properties))
for k := range missing {
missing[k] = strconv.Quote(properties[k])
properties[k] = escape(properties[k])
}
return &ValidationError{
SchemaPtr: "required",
Message: fmt.Sprintf("missing properties: %s", strings.Join(missing, ", ")),
Context: &ValidationErrorContextRequired{Missing: properties},
}
}