Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

cron-validate

Airfooox1.2mMIT1.5.2TypeScript support: included

cron-validate is a cron-expression validator written in TypeScript.

cron-validator, cron-validation, validation, cron, cron-expression, typescript

readme

cron-validate

typescript dependencies Status semantic-release styled with prettier npm dependencies Status

Cron-validate is a cron-expression validator written in TypeScript. The validation options are customizable and cron fields like seconds and years are supported.

Installation

Package is available on npm:

npm install -S cron-validate

Usage

Basic usage

import cron from 'cron-validate'

const cronResult = cron('* * * * *')
if (cronResult.isValid()) {
  // !cronResult.isError()
  // valid code
} else {
  // error code
}

Result system

The cron function returns a Result-type, which is either Valid<T, E> or Err<T, E>.

For checking the returned result, just use result.isValid() or result.isError()

Both result types contain values:

import cron from 'cron-validate'

const cronResult = cron('* * * * *')
if (cronResult.isValid()) {
  const validValue = cronResult.getValue()

  // The valid value is a object containing all cron fields
  console.log(validValue)
  // In this case, it would be:
  // { seconds: undefined, minutes: '*', hours: '*', daysOfMonth: '*', months: '*', daysOfWeek: '*', years: undefiend }
} else {
  const errorValue = cronResult.getError()

  // The error value contains an array of strings, which represent the cron validation errors.
  console.log(errorValue) // string[] of error messages
}

Make sure to test the result type beforehand, because getValue() only works on Valid and getError() only works on Err. If you don't check, it will throw an error.

For further information, you can check out https://github.com/gDelgado14/neverthrow, because I used and modified his code for this package. (Therefor not every documented function on his package is available on this package.)

Options / Configuration

To configure the validator, cron-validate uses a preset system. There are already defined presets (default, npm-node-cron or aws), but you can also define your own preset to use for your system. You can also use the override property to set certain option on single cron validates.

Presets

The following presets are already defined by cron-validate:

To select a preset for your validation, you can simply do this:

cron('* * * * *', {
  preset: 'npm-cron-schedule',
})

Defining and using your own preset

To define your own preset, use this:

registerOptionPreset('YOUR-PRESET-ID', {
  presetId: 'YOUR-PRESET-ID',
  useSeconds: false,
  useYears: false,
  useAliases: false, // optional, default to false
  useBlankDay: false,
  allowOnlyOneBlankDayField: false,
  allowStepping: true, // optional, defaults to true
  mustHaveBlankDayField: false, // optional, default to false
  useLastDayOfMonth: false, // optional, default to false
  useLastDayOfWeek: false, // optional, default to false
  useNearestWeekday: false, // optional, default to false
  useNthWeekdayOfMonth: false, // optional, default to false
  seconds: {
    minValue: 0,
    maxValue: 59,
    lowerLimit: 0, // optional, default to minValue
    upperLimit: 59, // optional, default to maxValue
  },
  minutes: {
    minValue: 0,
    maxValue: 59,
    lowerLimit: 0, // optional, default to minValue
    upperLimit: 59, // optional, default to maxValue
  },
  hours: {
    minValue: 0,
    maxValue: 23,
    lowerLimit: 0, // optional, default to minValue
    upperLimit: 23, // optional, default to maxValue
  },
  daysOfMonth: {
    minValue: 1,
    maxValue: 31,
    lowerLimit: 1, // optional, default to minValue
    upperLimit: 31, // optional, default to maxValue
  },
  months: {
    minValue: 0,
    maxValue: 12,
    lowerLimit: 0, // optional, default to minValue
    upperLimit: 12, // optional, default to maxValue
  },
  daysOfWeek: {
    minValue: 1,
    maxValue: 7,
    lowerLimit: 1, // optional, default to minValue
    upperLimit: 7, // optional, default to maxValue
  },
  years: {
    minValue: 1970,
    maxValue: 2099,
    lowerLimit: 1970, // optional, default to minValue
    upperLimit: 2099, // optional, default to maxValue
  },
})

The preset properties explained:

  • presetId: string
    • same id as in first function parameter
  • useSeconds: boolean
    • enables seconds field in cron expression
  • useYears: boolean
    • enables years field in cron expression
  • useAliases: boolean
    • enables aliases for month and daysOfWeek fields (ignores limits for month and daysOfWeek, so be aware of that)
  • useBlankDay: boolean
    • enables blank day notation '?' in daysOfMonth and daysOfWeek field
  • allowOnlyOneBlankDayField: boolean
    • requires a day field to not be blank (so not both day fields can be blank)
  • allowStepping: boolean
    • optional, will default to true
    • when set to false, disallows the use of the '/' operation for valid expressions
  • mustHaveBlankDayField: boolean
    • requires a day field to be blank (so not both day fields are specified)
    • when mixed with allowOnlyOneBlankDayField, it means that there will always be either day or day of week as ?
  • useLastDayOfMonth: boolean
    • enables the 'L' character to specify the last day of the month.
    • accept negative offset after the 'L' for nth last day of the month.
    • e.g.: L-2 would me the 2nd to last day of the month.
  • useLastDayOfWeek: boolean
    • enables the 'L' character to specify the last occurrence of a weekday in a month.
    • e.g.: 5L would mean the last friday of the month.
  • useNearestWeekday: boolean
    • enables the 'W' character to specify the use of the closest weekday.
    • e.g.: 15W would mean the weekday (mon-fri) closest to the 15th when the 15th is on sat-sun.
  • useNthWeekdayOfMonth: boolean

    • enables the '#' character to specify the Nth weekday of the month.
    • e.g.: 6#3 would mean the 3rd friday of the month (assuming 6 = friday).
  • in cron fields (like seconds, minutes etc.):

    • minValue: number
      • minimum value of your cron interpreter (like npm-node-cron only supports 0-6 for weekdays)
      • can't be set as override
    • maxValue: number
      • minimum value of your cron interpreter (like npm-node-cron only supports 0-6 for weekdays)
      • can't be set as override
    • lowerLimit?: number
      • lower limit for validation
      • equal or greater than minValue
      • if not set, default to minValue
    • upperLimit?: number
      • upper limit for validation
      • equal or lower than maxValue
      • if not set, defaults to maxValue

Override preset options

If you want to override a option for single cron validations, you can use the override property:

console.log(cron('* * * * * *', {
  preset: 'default', // second field not supported in default preset
  override: {
    useSeconds: true // override preset option
  }
}))

console.log(cron('* 10-20 * * * *', {
  preset: 'default',
  override: {
    minutes: {
      lowerLimit: 10, // override preset option
      upperLimit: 20 // override preset option
    }
  }
}))

Examples

import cron from 'cron-validate'

console.log(cron('* * * * *').isValid()) // true

console.log(cron('* * * * *').isError()) // false

console.log(cron('* 2,3,4 * * *').isValid()) // true

console.log(cron('0 */2 */5 * *').isValid()) // true

console.log(cron('* * * * * *', { override: { useSeconds: true } }).isValid()) // true

console.log(cron('* * * * * *', { override: { useYears: true } }).isValid()) // true

console.log(
  cron('30 * * * * *', {
    override: {
      useSeconds: true,
      seconds: {
        lowerLimit: 20,
        upperLimit: 40,
      },
    },
  }).isValid()
) // true

console.log(
  cron('* 3 * * *', {
    override: {
      hours: {
        lowerLimit: 0,
        upperLimit: 2,
      },
    },
  }).isValid()
) // false

console.log(
  cron('* * ? * *', {
    override: {
      useBlankDay: true,
    },
  }).isValid()
) // true

console.log(
  cron('* * ? * ?', {
    override: {
      useBlankDay: true,
      allowOnlyOneBlankDayField: true,
    },
  }).isValid()
) // false

(Planned) Features

  • <input checked="" disabled="" type="checkbox"> Basic cron validation.
  • <input checked="" disabled="" type="checkbox"> Error messenges with information about invalid cron expression.
  • <input checked="" disabled="" type="checkbox"> Seconds field support.
  • <input checked="" disabled="" type="checkbox"> Years field support.
  • <input checked="" disabled="" type="checkbox"> Option presets (classic cron, node-cron, etc.)
  • <input checked="" disabled="" type="checkbox"> Blank '?' daysOfMonth/daysOfWeek support
  • <input checked="" disabled="" type="checkbox"> Last day of month.
  • <input checked="" disabled="" type="checkbox"> Last specific weekday of month. (e.g. last Tuesday)
  • <input checked="" disabled="" type="checkbox"> Closest weekday to a specific day of the month.
  • <input checked="" disabled="" type="checkbox"> Nth specific weekday of month. (e.g. 2nd Tuesday)
  • <input checked="" disabled="" type="checkbox"> Cron alias support.

Contributors


Used by:


changelog

1.5.0 (2025-03-13)

Bug Fixes

Features

  • add allowStepping option (#290) (f6a0c53)
  • add check for non-integer numbers (#291) (2977282)
  • add check for second step number (max value, step range) (#291) (13e875b)

1.4.5 (2022-11-20)

Bug Fixes

  • day of week occurence number cannot be bigger than 5 (#263) (62cbb9b)

1.4.4 (2022-10-12)

Bug Fixes

  • update AWS cloudwatch preset (f583267)

1.4.3 (2021-03-31)

Bug Fixes

  • remove import type for backwards compatibility (3e4d8ab)
  • deps: update dependency yup to v0.32.9 (#166) (0193a3f)

1.4.2 (2020-12-25)

Bug Fixes

  • deps: update dependency yup to v0.32.8 (#146) (fc2783d)

1.4.1 (2020-11-21)

Bug Fixes

  • deps: update dependency yup to v0.30.0 (#140) (5679b0f)

1.4.0 (2020-10-25)

Features

1.3.1 (2020-08-21)

Bug Fixes

  • fix wrong wildcard ('*') check with lower-/upperLimit (df8b6f2)

1.3.0 (2020-08-14)

Bug Fixes

  • try to fix default export (3a66f3f)
  • deps: update dependency yup to v0.29.3 (#94) (0080c06)

Features

1.2.0 (2020-07-29)

Bug Fixes

  • deps: update dependency yup to v0.29.2 (#87) (cb8cdfe)
  • add explicit return types to avoid Result<unknown, ...> (#78) (177e301)

Features

  • Extending AWS Preset wildcards and checks. (#80) (a7baca0)

1.1.5 (2020-07-14)

Bug Fixes

  • fix require().default for non-es modules (2b0eb95), closes #69

1.1.4 (2020-05-31)

Bug Fixes

  • deps: update dependency yup to v0.29.1 (58eab14)

1.1.3 (2020-05-20)

Bug Fixes

  • fixed missing required fields in yup schema (6ba1270)
  • deps: update dependency yup to v0.29.0 (1bfcbc6)

1.1.2 (2020-05-01)

Bug Fixes

  • deps: update dependency yup to v0.28.5 (27c6068)

1.1.1 (2020-04-27)

Bug Fixes

  • deps: update dependency yup to v0.28.4 (de60998)

1.1.0 (2020-04-18)

Features

  • add blank day '?' support (ca71a28)

1.0.4 (2020-04-15)

Bug Fixes

  • err not containing string array but string (4d5a972)

1.0.3 (2020-04-15)

Bug Fixes

  • update package.json for npm description (72132c5)

1.0.2 (2020-04-15)

Bug Fixes

  • update README.md (trigger release) (3880d42)

1.0.1 (2020-04-15)

Bug Fixes

  • add package-lock.json to git release (trigger a new version) (9f85bd4)

1.0.0 (2020-04-15)

Bug Fixes

  • add check for second step element (76d59fa)
  • Change imports of option type (d447be5)
  • fix useSeconds not being set on options by preset (d77ec8c)
  • fix value limits not applying to wildcard '*' (00a3379)
  • fix wrong checks (2f6290e)

Features

  • add getOptionPreset and getOptionPresets (9db79fe)
  • Add individual checker files and add helper functions (24ecf74)
  • add option validation with yup (c08f210)
  • Add option.ts file, check for value limits (032dcbe)
  • Add preset system for options (bac86ee)
  • Add reponse types for typesafe returns (f85f36a)
  • add two additional option presets (489e472)
  • Update result (5bd3fa1)