@clausehq/flows-analysis
Advanced tools
Comparing version 0.1.2 to 0.1.3
@@ -1,5 +0,106 @@ | ||
const scheduledTrigger = (triggerType) => { | ||
if (triggerType && triggerType.$class === 'io.clause.flows.trigger.Schedule') { | ||
const humanInterval = require('human-interval'); | ||
const dayjs = require('dayjs'); | ||
const cronParser = require('cron-parser'); | ||
const PERIODIC_INTERVAL_MINIMUM_MS = 1000 * 60 * 10; | ||
const buildError = (errName, message) => { | ||
const err = new Error(errName); | ||
err.name = errName; | ||
err.message = message; | ||
return err; | ||
}; | ||
const buildValidationError = (message) => buildError( | ||
'validationError', | ||
message, | ||
); | ||
const isHumanIntervalValid = humanIntervalString => !Number.isNaN( | ||
humanInterval(humanIntervalString) | ||
); | ||
const isIso8601Valid = iso8601String => dayjs(iso8601String).isValid(); | ||
const isCronExpressionValid = cronExpressionString => { | ||
try { | ||
cronParser.parseExpression(cronExpressionString); | ||
return true; | ||
} catch (err) { | ||
if (err.message.includes('Validation') || err.message.includes('Constraint')) { | ||
return false; | ||
} | ||
throw err; | ||
} | ||
}; | ||
const getPeriodicCadenceInMsFromCronExpression = cronExpressionString => cronParser | ||
.parseExpression(cronExpressionString).next()._date.ts | ||
- cronParser | ||
.parseExpression(cronExpressionString).prev()._date.ts; | ||
const validateTypeOnce = ({ interval }) => { | ||
if (isIso8601Valid(interval)) { | ||
if (dayjs(interval).diff(dayjs()) < 0) { | ||
throw buildValidationError( | ||
`Invalid scheduled flow once-off interval: ${interval}. The time set should not be in the past.` | ||
); | ||
} | ||
return true; | ||
} | ||
if (isHumanIntervalValid(interval)) { | ||
if (humanInterval(interval) < 0) { | ||
throw buildValidationError( | ||
`Invalid scheduled flow once-off interval: ${interval}. The time set should not be in the past.` | ||
); | ||
} | ||
return true; | ||
} | ||
throw buildValidationError( | ||
`Invalid scheduled flow once-off interval: ${interval}. Please use either the Human Interval format or an ISO 8601 string.` | ||
); | ||
}; | ||
const validateTypePeriodic = ({ interval }) => { | ||
if (isHumanIntervalValid(interval)) { | ||
if (humanInterval(interval) < PERIODIC_INTERVAL_MINIMUM_MS) { | ||
throw buildValidationError( | ||
`Invalid scheduled flow periodic interval: ${interval}. The interval should not be less than 10 minutes.` | ||
); | ||
} | ||
return true; | ||
} | ||
if (isCronExpressionValid(interval)) { | ||
if (getPeriodicCadenceInMsFromCronExpression(interval) < PERIODIC_INTERVAL_MINIMUM_MS) { | ||
throw buildValidationError( | ||
`Invalid scheduled flow periodic interval: ${interval}. The interval should not be less than 10 minutes.` | ||
); | ||
} | ||
return true; | ||
} | ||
throw buildValidationError( | ||
`Invalid scheduled flow periodic interval: ${interval}. Please use either the Human Interval format or a CRON expression.` | ||
); | ||
}; | ||
const validateScheduledFlowTriggerType = triggerType => { | ||
if (typeof triggerType.type !== 'string' || typeof triggerType.interval !== 'string') { | ||
throw buildValidationError('Scheduled flow type or interval missing.'); | ||
} | ||
switch (triggerType.type) { | ||
case 'once': | ||
return validateTypeOnce(triggerType); | ||
case 'periodic': | ||
return validateTypePeriodic(triggerType); | ||
default: | ||
throw buildValidationError( | ||
`Invalid scheduled flow scheduling type: ${triggerType.type}.` | ||
); | ||
} | ||
}; | ||
const triggerType = triggerType => { | ||
if (triggerType && triggerType.$class === 'io.clause.flows.trigger.Schedule') { | ||
return validateScheduledFlowTriggerType(triggerType); | ||
} | ||
return true; | ||
@@ -9,3 +110,4 @@ }; | ||
module.exports = { | ||
scheduledTrigger, | ||
triggerType, | ||
validateScheduledFlowTriggerType, | ||
}; |
{ | ||
"name": "@clausehq/flows-analysis", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "A utility for doing static analysis on flows", | ||
@@ -18,2 +18,5 @@ "license": "UNLICENSED", | ||
"dependencies": { | ||
"cron-parser": "^3.4.0", | ||
"dayjs": "^1.10.4", | ||
"human-interval": "^2.0.1", | ||
"jsonpath": "^1.0.2" | ||
@@ -20,0 +23,0 @@ }, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20877
8
569
4
+ Addedcron-parser@^3.4.0
+ Addeddayjs@^1.10.4
+ Addedhuman-interval@^2.0.1
+ Addedcall-bind@1.0.7(transitive)
+ Addedcron-parser@3.5.0(transitive)
+ Addeddayjs@1.11.13(transitive)
+ Addeddefine-data-property@1.1.4(transitive)
+ Addeddefine-properties@1.2.1(transitive)
+ Addedes-define-property@1.0.0(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedget-intrinsic@1.2.4(transitive)
+ Addedgopd@1.0.1(transitive)
+ Addedhas-property-descriptors@1.0.2(transitive)
+ Addedhas-proto@1.0.3(transitive)
+ Addedhas-symbols@1.0.3(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedhuman-interval@2.0.1(transitive)
+ Addedis-nan@1.3.2(transitive)
+ Addedluxon@1.28.1(transitive)
+ Addednumbered@1.1.0(transitive)
+ Addedobject-keys@1.1.1(transitive)
+ Addedset-function-length@1.2.2(transitive)