-
Notifications
You must be signed in to change notification settings - Fork 4
/
validator.go
54 lines (49 loc) · 1.72 KB
/
validator.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
package unis
// Validator is just another interface
// for string utilities.
// All validators should implement this interface.
// Contains only one function "Valid" which accepts
// a string and returns a boolean and an error.
// It should compare that string "str" with
// something and returns a true, nil or false, err.
//
// Validators can be used side by side with Processors.
//
// See .If for more.
type Validator interface {
Valid(str string) (ok bool, err error)
}
// ValidatorFunc is just an "alias" for the Validator interface.
// It implements the Validator.
type ValidatorFunc func(str string) (bool, error)
// Valid accepts
// a string and returns a boolean and an error.
// It should compare that string "str" with
// something and returns a true, nil or false, err.
func (v ValidatorFunc) Valid(str string) (bool, error) {
return v(str)
}
func newFailure(err error) ValidatorFunc {
return func(string) (bool, error) {
return false, err
}
}
// If receives a "validator" Validator and two Processors,
// the first processor will be called when that validator passed,
// the second processor will be called when the validator failed.
// Both of the processors ("succeed" and "failure"), as always,
// can be results of .NewChain.
//
// Returns a new string processor which checks the "validator"
// against the "original" string, if passed then it runs the
// "succeed", otherwise it runs the "failure".
//
// Remember: it returns a ProcessorFunc, meaning that can be used in a new chain too.
func If(validator Validator, succeed Processor, failure Processor) ProcessorFunc {
return func(original string) string {
if ok, _ := validator.Valid(original); ok {
return succeed.Process(original)
}
return failure.Process(original)
}
}