summaryrefslogtreecommitdiffstatshomepage
path: root/validators.go
blob: 2f256f1cbbbe6d743c63b5a9c617066da7ec428f (plain) (blame)
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
// Copyright 2011 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build windows

package walk

import (
	"errors"
	"fmt"
	"math"
	"regexp"
)

type Validator interface {
	Validate(v interface{}) error
}

type ValidationError struct {
	title   string
	message string
}

func NewValidationError(title, message string) *ValidationError {
	return &ValidationError{title: title, message: message}
}

func (ve *ValidationError) Title() string {
	return ve.title
}

func (ve *ValidationError) Message() string {
	return ve.message
}

func (ve *ValidationError) Error() string {
	return fmt.Sprintf("%s - %s", ve.title, ve.message)
}

type RangeValidator struct {
	min float64
	max float64
}

func NewRangeValidator(min, max float64) (*RangeValidator, error) {
	if max < min {
		return nil, errors.New("max < min")
	}

	return &RangeValidator{min: min, max: max}, nil
}

func (rv *RangeValidator) Min() float64 {
	return rv.min
}

func (rv *RangeValidator) Max() float64 {
	return rv.max
}

func (rv *RangeValidator) Reset(min, max float64) error {
	if max < min {
		return errors.New("max < min")
	}

	rv.min, rv.max = min, max

	return nil
}

func (rv *RangeValidator) Validate(v interface{}) error {
	f64 := v.(float64)

	if f64 < rv.min || f64 > rv.max {
		var msg string
		if math.Abs(rv.min-math.Floor(rv.min)) < math.SmallestNonzeroFloat64 &&
			math.Abs(rv.max-math.Floor(rv.max)) < math.SmallestNonzeroFloat64 {

			msg = fmt.Sprintf(tr("Please enter a number from %.f to %.f.", "walk"),
				rv.min, rv.max)
		} else {
			msg = fmt.Sprintf(tr("Please enter a number from %s to %s.", "walk"),
				FormatFloatGrouped(rv.min, 2), FormatFloatGrouped(rv.max, 2))
		}

		return NewValidationError(tr("Number out of allowed range", "walk"), msg)
	}

	return nil
}

type RegexpValidator struct {
	re *regexp.Regexp
}

func NewRegexpValidator(pattern string) (*RegexpValidator, error) {
	re, err := regexp.Compile(pattern)
	if err != nil {
		return nil, err
	}

	return &RegexpValidator{re}, nil
}

func (rv *RegexpValidator) Pattern() string {
	return rv.re.String()
}

func (rv *RegexpValidator) Validate(v interface{}) error {
	var matched bool

	switch val := v.(type) {
	case string:
		matched = rv.re.MatchString(val)

	case []byte:
		matched = rv.re.Match(val)

	case fmt.Stringer:
		matched = rv.re.MatchString(val.String())

	default:
		panic("Unsupported type")
	}

	if !matched {
		return errors.New(tr("The text does not match the required pattern.", "walk"))
	}

	return nil
}

type selectionRequiredValidator struct {
}

var selectionRequiredValidatorSingleton Validator = selectionRequiredValidator{}

func SelectionRequiredValidator() Validator {
	return selectionRequiredValidatorSingleton
}

func (selectionRequiredValidator) Validate(v interface{}) error {
	if v == nil {
		// For Widgets like ComboBox nil is passed to indicate "no selection".
		return NewValidationError(
			tr("Selection Required", "walk"),
			tr("Please select one of the provided options.", "walk"))
	}

	return nil
}