forked from smartcontractkit/chainlink
/
validate.go
42 lines (34 loc) · 1.03 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
package cron
import (
"github.com/pelletier/go-toml"
"github.com/pkg/errors"
uuid "github.com/satori/go.uuid"
"github.com/DCMMC/chainlink/core/services/job"
"github.com/DCMMC/chainlink/core/utils"
)
func ValidatedCronSpec(tomlString string) (job.Job, error) {
var jb = job.Job{
ExternalJobID: uuid.NewV4(), // Default to generating a uuid, can be overwritten by the specified one in tomlString.
}
tree, err := toml.Load(tomlString)
if err != nil {
return jb, errors.Wrap(err, "toml error on load")
}
err = tree.Unmarshal(&jb)
if err != nil {
return jb, errors.Wrap(err, "toml unmarshal error on spec")
}
var spec job.CronSpec
err = tree.Unmarshal(&spec)
if err != nil {
return jb, errors.Wrap(err, "toml unmarshal error on job")
}
jb.CronSpec = &spec
if jb.Type != job.Cron {
return jb, errors.Errorf("unsupported type %s", jb.Type)
}
if err := utils.ValidateCronSchedule(spec.CronSchedule); err != nil {
return jb, errors.Wrapf(err, "while validating cron schedule '%v'", spec.CronSchedule)
}
return jb, nil
}