forked from qri-io/jsonschema
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeywords_numeric.go
99 lines (83 loc) · 3.31 KB
/
keywords_numeric.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
package jsonschema
import (
"fmt"
)
// MultipleOf MUST be a number, strictly greater than 0.
// MultipleOf validates that a numeric instance is valid only if division
// by this keyword's value results in an integer.
type MultipleOf float64
// NewMultipleOf allocates a new MultipleOf validator
func NewMultipleOf() Validator {
return new(MultipleOf)
}
// Validate implements the Validator interface for MultipleOf
func (m MultipleOf) Validate(propPath string, data interface{}, errs *[]ValError) {
if num, ok := data.(float64); ok {
div := num / float64(m)
if float64(int(div)) != div {
AddError(errs, propPath, data, fmt.Sprintf("must be a multiple of %f", m))
}
}
}
// Maximum MUST be a number, representing an inclusive upper limit
// for a numeric instance.
// If the instance is a number, then this keyword validates only if the instance is less than or exactly equal to "Maximum".
type Maximum float64
// NewMaximum allocates a new Maximum validator
func NewMaximum() Validator {
return new(Maximum)
}
// Validate implements the Validator interface for Maximum
func (m Maximum) Validate(propPath string, data interface{}, errs *[]ValError) {
if num, ok := data.(float64); ok {
if num > float64(m) {
AddError(errs, propPath, data, fmt.Sprintf("must be less than or equal to %f", m))
}
}
}
// ExclusiveMaximum MUST be number, representing an exclusive upper limit for a numeric instance.
// If the instance is a number, then the instance is valid only if it has a value
// strictly less than (not equal to) "Exclusivemaximum".
type ExclusiveMaximum float64
// NewExclusiveMaximum allocates a new ExclusiveMaximum validator
func NewExclusiveMaximum() Validator {
return new(ExclusiveMaximum)
}
// Validate implements the Validator interface for ExclusiveMaximum
func (m ExclusiveMaximum) Validate(propPath string, data interface{}, errs *[]ValError) {
if num, ok := data.(float64); ok {
if num >= float64(m) {
AddError(errs, propPath, data, fmt.Sprintf("must be less than %f", m))
}
}
}
// Minimum MUST be a number, representing an inclusive lower limit for a numeric instance.
// If the instance is a number, then this keyword validates only if the instance is greater than or exactly equal to "Minimum".
type Minimum float64
// NewMinimum allocates a new Minimum validator
func NewMinimum() Validator {
return new(Minimum)
}
// Validate implements the Validator interface for Minimum
func (m Minimum) Validate(propPath string, data interface{}, errs *[]ValError) {
if num, ok := data.(float64); ok {
if num < float64(m) {
AddError(errs, propPath, data, fmt.Sprintf("must be greater than or equal to %f", m))
}
}
}
// ExclusiveMinimum MUST be number, representing an exclusive lower limit for a numeric instance.
// If the instance is a number, then the instance is valid only if it has a value strictly greater than (not equal to) "ExclusiveMinimum".
type ExclusiveMinimum float64
// NewExclusiveMinimum allocates a new ExclusiveMinimum validator
func NewExclusiveMinimum() Validator {
return new(ExclusiveMinimum)
}
// Validate implements the Validator interface for ExclusiveMinimum
func (m ExclusiveMinimum) Validate(propPath string, data interface{}, errs *[]ValError) {
if num, ok := data.(float64); ok {
if num <= float64(m) {
AddError(errs, propPath, data, fmt.Sprintf("must be greater than %f", m))
}
}
}