-
Notifications
You must be signed in to change notification settings - Fork 1
/
validate.go
45 lines (37 loc) · 1.09 KB
/
validate.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
package validate
import (
"errors"
"regexp"
"github.com/lib-x/edgetts/internal/communicateOption"
)
var (
validPitchPattern = regexp.MustCompile(`^[+-]\d+Hz$`)
validVoicePattern = regexp.MustCompile(`^([a-z]{2,})-([A-Z]{2,})-(.+Neural)$`)
validRateVolumePattern = regexp.MustCompile(`^[+-]\d+%$`)
)
var (
InvalidVoiceError = errors.New("invalid voice")
InvalidPitchError = errors.New("invalid pitch")
InvalidRateError = errors.New("invalid rate")
InvalidVolumeError = errors.New("invalid volume")
)
// WithCommunicateOption validate With a CommunicateOption
func WithCommunicateOption(c *communicateOption.CommunicateOption) error {
// WithCommunicateOption voice
if !validVoicePattern.MatchString(c.Voice) {
return InvalidVoiceError
}
// WithCommunicateOption pitch
if !validPitchPattern.MatchString(c.Pitch) {
return InvalidPitchError
}
// WithCommunicateOption rate
if !validRateVolumePattern.MatchString(c.Rate) {
return InvalidRateError
}
// WithCommunicateOption volume
if !validRateVolumePattern.MatchString(c.Volume) {
return InvalidVolumeError
}
return nil
}