-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexclusive_maximum.go
109 lines (89 loc) · 2.3 KB
/
exclusive_maximum.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
package jsonschema
import (
"fmt"
"reflect"
"github.com/aacebo/jsonschema/coerce"
)
// https://json-schema.org/understanding-json-schema/reference/numeric#range
func exclusiveMaximum(key string) Keyword {
return Keyword{
Default: false,
Compile: func(ns *Namespace, ctx Context, config reflect.Value) []SchemaError {
errs := []SchemaError{}
if config.Kind() == reflect.Bool {
maximum := reflect.Indirect(reflect.ValueOf(ctx.Schema["maximum"]))
if !maximum.IsValid() {
errs = append(errs, SchemaError{
Path: ctx.Path,
Keyword: key,
Message: `"maximum" is required when "boolean"`,
})
}
} else {
config = coerce.Float(config)
if !config.CanFloat() {
errs = append(errs, SchemaError{
Path: ctx.Path,
Keyword: key,
Message: `must be a "boolean" or "number"`,
})
return errs
}
exclusiveMinimum := reflect.Indirect(reflect.ValueOf(ctx.Schema["exclusiveMinimum"]))
if exclusiveMinimum.CanFloat() && exclusiveMinimum.Float() > config.Float() {
errs = append(errs, SchemaError{
Path: ctx.Path,
Keyword: key,
Message: `must be greater than or equal to "exclusiveMinimum"`,
})
}
}
return errs
},
Validate: func(ns *Namespace, ctx Context, config reflect.Value, value reflect.Value) []SchemaError {
errs := []SchemaError{}
if !value.IsValid() {
return errs
}
value = coerce.Float(value)
if !value.CanFloat() {
return errs
}
if config.Kind() == reflect.Bool {
if !config.Bool() {
return errs
}
maximum := reflect.Indirect(reflect.ValueOf(ctx.Schema["maximum"]))
if !maximum.IsValid() {
return errs
}
maximum = coerce.Float(maximum)
if value.Float() > maximum.Float()-1 {
errs = append(errs, SchemaError{
Path: ctx.Path,
Keyword: key,
Message: fmt.Sprintf(
`"%v" is greater than "%v"`,
value.Float(),
maximum.Float()-1,
),
})
}
} else {
config = coerce.Float(config)
if value.Float() > config.Float() {
errs = append(errs, SchemaError{
Path: ctx.Path,
Keyword: key,
Message: fmt.Sprintf(
`"%v" is greater than "%v"`,
value.Float(),
config.Float(),
),
})
}
}
return errs
},
}
}