mirror of
https://github.com/vatesfr/xen-orchestra.git
synced 2026-11-09 17:06:13 -06:00
27 lines
995 B
JavaScript
27 lines
995 B
JavaScript
import { isExpression, compileExpression } from './_expressionPredicate.mjs'
|
|
import { invalidParameters } from 'xo-common/api-errors.js'
|
|
|
|
export function assertValidSettingsExpression(settings, keys) {
|
|
for (const [scheduleId, scheduleSettings] of Object.entries(settings ?? {})) {
|
|
for (const key of keys) {
|
|
assertValidExpression(`settings/${scheduleId === '' ? 'general' : scheduleId}/${key}`, scheduleSettings?.[key])
|
|
}
|
|
}
|
|
}
|
|
|
|
export function assertValidExpression(name, value) {
|
|
if (isExpression(value)) {
|
|
try {
|
|
compileExpression(value)
|
|
} catch (error) {
|
|
throw invalidParameters(`Invalid ${name} expression: ${error.message}`, [
|
|
{ instancePath: `/${name}`, message: error.message },
|
|
])
|
|
}
|
|
} else if (typeof value === 'string' && value.trim() === '') {
|
|
throw invalidParameters(`Invalid ${name} expression: must be a non-empty expression`, [
|
|
{ instancePath: `/${name}`, message: 'must be a non-empty expression' },
|
|
])
|
|
}
|
|
}
|