Security News
Supply Chain Attack Detected in Solana's web3.js Library
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
cron-validator
Advanced tools
The cron-validator npm package is used to validate and parse cron expressions. It ensures that cron expressions are syntactically correct and can be used to schedule tasks in a reliable manner.
Validate Cron Expression
This feature allows you to validate a cron expression to check if it is syntactically correct. The `isValidCron` function returns a boolean indicating whether the provided cron expression is valid.
const cronValidator = require('cron-validator');
const isValid = cronValidator.isValidCron('0 0 * * *');
console.log(isValid); // true
Custom Validation Options
This feature allows you to validate a cron expression with custom options. For example, you can specify whether to include seconds in the cron expression or allow blank days.
const cronValidator = require('cron-validator');
const options = { seconds: true, allowBlankDay: true };
const isValid = cronValidator.isValidCron('0 0 0 * * *', options);
console.log(isValid); // true
Parse Cron Expression
This feature allows you to parse a cron expression into its individual components. The `parseCron` function returns an object with the parsed components of the cron expression.
const cronValidator = require('cron-validator');
const parsed = cronValidator.parseCron('0 0 * * *');
console.log(parsed); // { minutes: '0', hours: '0', dayOfMonth: '*', months: '*', dayOfWeek: '*' }
The cron-parser package is used to parse and manipulate cron expressions. It provides more advanced features such as generating the next execution time for a given cron expression. Compared to cron-validator, cron-parser focuses more on parsing and scheduling rather than just validation.
The node-cron package is a task scheduler in pure JavaScript for node.js based on cron syntax. It allows you to schedule tasks using cron expressions and provides validation as part of its scheduling functionality. While cron-validator focuses solely on validation, node-cron provides a complete scheduling solution.
The cron package is a cron job scheduler for node.js. It allows you to schedule jobs using cron expressions and provides validation as part of its job scheduling functionality. Similar to node-cron, it offers a more comprehensive solution for scheduling tasks compared to the validation-focused cron-validator.
Cron Validator is a util that allows you to validate a cron expression, similar to what crontab guru does, but in your code base.
npm install cron-validator
Require syntax:
const cron = require('cron-validator');
if (cron.isValidCron('* * * * *')) {
// Do something
}
Or import syntax with TypeScript:
import { isValidCron } from 'cron-validator'
if (isValidCron('* * * * *')) {
// Do something
}
Support for seconds can be enabled by passing the seconds
flag as true in options:
const cron = require('cron-validator');
cron.isValidCron('* * * * * *');
// false
cron.isValidCron('* * * * * *', { seconds: true });
// true
The same goes to enable the alias
support for months and weekdays:
const cron = require('cron-validator');
cron.isValidCron('* * * * mon');
// false
cron.isValidCron('* * * * mon', { alias: true });
// true
Likewise, the allowBlankDay
flag can be enabled to mark days or weekdays blank with a ?
symbol:
const cron = require('cron-validator');
cron.isValidCron('* * * * ?');
// false
cron.isValidCron('* * * * ?', { allowBlankDay: true });
// true
The allowSevenAsSunday
flag can be enabled to enable support for digit 7 as Sunday:
const cron = require('cron-validator');
cron.isValidCron('* * * * 7');
// false
cron.isValidCron('* * * * 7', { allowSevenAsSunday: true });
// true
?
symbol.Many great cron libraries already exists on NPM, why this one?
Libraries like node-cron are primarily made to schedule jobs using a cron expression, not validate those cron expressions. They come with additional behaviors not always required. They also bring their own set of defaults which might be in conflicts with the defaults of other external systems. We needed something to validate an expression before sending it off to an external system, so we created this to be a little more strict and configurable, with a more specific behavior.
We decided to go for the naive approach first, which results in lenghty code and tests, but also making it easier to reason about cron expressions and their specific rules.
FAQs
Validates cron expressions
The npm package cron-validator receives a total of 163,530 weekly downloads. As such, cron-validator popularity was classified as popular.
We found that cron-validator demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.