-
Notifications
You must be signed in to change notification settings - Fork 1
/
validation.go
73 lines (54 loc) · 1.56 KB
/
validation.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
package calc
import "errors"
// Validation describes operation validation rules.
type Validation func(args []float64) error
func commonValidation(args []float64) error {
if args == nil {
return errors.New("arguments list cannot be nil")
}
return nil
}
func minOneArgumentValidation(args []float64) error {
if err := commonValidation(args); err != nil {
return err
}
if len(args) < 1 {
return errors.New("arguments list should have at least one element")
}
return nil
}
func minTwoArgumentsValidation(args []float64) error {
if err := commonValidation(args); err != nil {
return err
}
if len(args) < 2 {
return errors.New("arguments list should have at least two elements")
}
return nil
}
// AddValidation addition arguments validation rules.
var AddValidation Validation = minTwoArgumentsValidation
// SubValidation subtraction arguments validation rules.
var SubValidation Validation = minTwoArgumentsValidation
// MulValidation multiplication arguments validation rules.
var MulValidation Validation = minTwoArgumentsValidation
// DivValidation division arguments validation rules.
func DivValidation(args []float64) error {
if err := minTwoArgumentsValidation(args); err != nil {
return err
}
if args[1] == 0 {
return errors.New("divisor cannot be zero")
}
return nil
}
// SqrtValidation square root arguments validation rules.
func SqrtValidation(args []float64) error {
if err := minOneArgumentValidation(args); err != nil {
return err
}
if args[0] < 0 {
return errors.New("square root argument cannot be negative")
}
return nil
}