Skip to content

Commit 07c6e8c

Browse files
committed
feat: add IsValid top level func
1 parent 7357f70 commit 07c6e8c

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ gron.IsDue(expr) // true|false, nil
4747
gron.IsDue(expr, time.Date(2021, time.April, 1, 1, 1, 0, 0, time.UTC)) // true|false, nil
4848
```
4949

50+
> Validity can be checked without instantiation:
51+
52+
```go
53+
import "github.com/adhocore/gronx"
54+
55+
gronx.IsValid("* * * * *") // true
56+
```
57+
5058
### Batch Due Check
5159

5260
If you have multiple cron expressions to check due on same reference time use `BatchDue()`:

gronx.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ func New() *Gronx {
7575
// IsDue checks if cron expression is due for given reference time (or now).
7676
// It returns bool or error if any.
7777
func (g *Gronx) IsDue(expr string, ref ...time.Time) (bool, error) {
78-
ref = append(ref, time.Now())
78+
if len(ref) == 0 {
79+
ref = append(ref, time.Now())
80+
}
7981
g.C.SetRef(ref[0])
8082

8183
segs, err := Segments(expr)
@@ -157,12 +159,16 @@ func (g *Gronx) SegmentsDue(segs []string) (bool, error) {
157159
return true, nil
158160
}
159161

162+
// IsValid checks if cron expression is valid.
163+
// It returns bool.
164+
func (g *Gronx) IsValid(expr string) bool { return IsValid(expr) }
165+
160166
// checker for validity
161167
var checker = &SegmentChecker{ref: time.Now()}
162168

163169
// IsValid checks if cron expression is valid.
164170
// It returns bool.
165-
func (g *Gronx) IsValid(expr string) bool {
171+
func IsValid(expr string) bool {
166172
segs, err := Segments(expr)
167173
if err != nil {
168174
return false

gronx_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ func TestIsValid(t *testing.T) {
6060
if !gron.IsValid("* 00 * * *") {
6161
t.Errorf("expected true, got false")
6262
}
63+
if expr := "* * * * *"; IsValid(expr) != gron.IsValid(expr) {
64+
t.Error("IsValid func and method must return same")
65+
}
6366
})
6467

6568
t.Run("is not valid", func(t *testing.T) {

0 commit comments

Comments
 (0)