Skip to content

Commit

Permalink
fix: fixed missing required fields in yup schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin committed May 20, 2020
1 parent a6ed64f commit 6ba1270
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 148 deletions.
11 changes: 9 additions & 2 deletions src/fieldCheckers/dayOfMonthChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@ const checkDaysOfMonth = (cronData: CronData, options: Options) => {

const { daysOfMonth } = cronData

if (options.allowOnlyOneBlankDayField && options.useBlankDay && cronData.daysOfMonth === '?' && cronData.daysOfWeek === '?') {
return err([`Cannot use blank value in daysOfMonth and daysOfWeek field when allowOnlyOneBlankDayField option is enabled.`])
if (
options.allowOnlyOneBlankDayField &&
options.useBlankDay &&
cronData.daysOfMonth === '?' &&
cronData.daysOfWeek === '?'
) {
return err([
`Cannot use blank value in daysOfMonth and daysOfWeek field when allowOnlyOneBlankDayField option is enabled.`,
])
}

return checkField(daysOfMonth, 'daysOfMonth', options)
Expand Down
10 changes: 8 additions & 2 deletions src/fieldCheckers/dayOfWeekChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ const checkDaysOfWeek = (cronData: CronData, options: Options) => {

const { daysOfWeek } = cronData

if (options.allowOnlyOneBlankDayField && cronData.daysOfMonth === '?' && cronData.daysOfWeek === '?') {
return err([`Cannot use blank value in daysOfMonth and daysOfWeek field when allowOnlyOneBlankDayField option is enabled.`])
if (
options.allowOnlyOneBlankDayField &&
cronData.daysOfMonth === '?' &&
cronData.daysOfWeek === '?'
) {
return err([
`Cannot use blank value in daysOfMonth and daysOfWeek field when allowOnlyOneBlankDayField option is enabled.`,
])
}

return checkField(daysOfWeek, 'daysOfWeek', options)
Expand Down
6 changes: 3 additions & 3 deletions src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,9 @@ const checkField = (
return valid(true)
}

return err(
[`useBlankDay is not enabled, but is used in ${cronFieldType} field`]
)
return err([
`useBlankDay is not enabled, but is used in ${cronFieldType} field`,
])
}

return err([`blank notation is not allowed in ${cronFieldType} field`])
Expand Down
300 changes: 159 additions & 141 deletions src/option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,69 +92,71 @@ export const registerOptionPreset = (
})
}

const optionPresetSchema = yup.object({
presetId: yup.string().required(),
useSeconds: yup.boolean().required(),
useYears: yup.boolean().required(),
useBlankDay: yup.boolean().required(),
allowOnlyOneBlankDayField: yup.boolean().required(),
seconds: yup
.object({
minValue: yup.number().min(0).required(),
maxValue: yup.number().min(0).required(),
lowerLimit: yup.number().min(0),
upperLimit: yup.number().min(0),
})
.required(),
minutes: yup
.object({
minValue: yup.number().min(0).required(),
maxValue: yup.number().min(0).required(),
lowerLimit: yup.number().min(0),
upperLimit: yup.number().min(0),
})
.required(),
hours: yup
.object({
minValue: yup.number().min(0).required(),
maxValue: yup.number().min(0).required(),
lowerLimit: yup.number().min(0),
upperLimit: yup.number().min(0),
})
.required(),
daysOfMonth: yup
.object({
minValue: yup.number().min(0).required(),
maxValue: yup.number().min(0).required(),
lowerLimit: yup.number().min(0),
upperLimit: yup.number().min(0),
})
.required(),
months: yup
.object({
minValue: yup.number().min(0).required(),
maxValue: yup.number().min(0).required(),
lowerLimit: yup.number().min(0),
upperLimit: yup.number().min(0),
})
.required(),
daysOfWeek: yup
.object({
minValue: yup.number().min(0).required(),
maxValue: yup.number().min(0).required(),
lowerLimit: yup.number().min(0),
upperLimit: yup.number().min(0),
})
.required(),
years: yup
.object({
minValue: yup.number().min(0).required(),
maxValue: yup.number().min(0).required(),
lowerLimit: yup.number().min(0),
upperLimit: yup.number().min(0),
})
.required(),
})
const optionPresetSchema = yup
.object({
presetId: yup.string().required(),
useSeconds: yup.boolean().required(),
useYears: yup.boolean().required(),
useBlankDay: yup.boolean().required(),
allowOnlyOneBlankDayField: yup.boolean().required(),
seconds: yup
.object({
minValue: yup.number().min(0).required(),
maxValue: yup.number().min(0).required(),
lowerLimit: yup.number().min(0),
upperLimit: yup.number().min(0),
})
.required(),
minutes: yup
.object({
minValue: yup.number().min(0).required(),
maxValue: yup.number().min(0).required(),
lowerLimit: yup.number().min(0),
upperLimit: yup.number().min(0),
})
.required(),
hours: yup
.object({
minValue: yup.number().min(0).required(),
maxValue: yup.number().min(0).required(),
lowerLimit: yup.number().min(0),
upperLimit: yup.number().min(0),
})
.required(),
daysOfMonth: yup
.object({
minValue: yup.number().min(0).required(),
maxValue: yup.number().min(0).required(),
lowerLimit: yup.number().min(0),
upperLimit: yup.number().min(0),
})
.required(),
months: yup
.object({
minValue: yup.number().min(0).required(),
maxValue: yup.number().min(0).required(),
lowerLimit: yup.number().min(0),
upperLimit: yup.number().min(0),
})
.required(),
daysOfWeek: yup
.object({
minValue: yup.number().min(0).required(),
maxValue: yup.number().min(0).required(),
lowerLimit: yup.number().min(0),
upperLimit: yup.number().min(0),
})
.required(),
years: yup
.object({
minValue: yup.number().min(0).required(),
maxValue: yup.number().min(0).required(),
lowerLimit: yup.number().min(0),
upperLimit: yup.number().min(0),
})
.required(),
})
.required()

export interface Options {
presetId: string
Expand Down Expand Up @@ -272,84 +274,100 @@ export const validateOptions = (inputOptions: InputOptions) => {
...inputOptions.override,
}

const optionsSchema = yup.object({
presetId: yup.string().required(),
preset: optionPresetSchema.required(),
useSeconds: yup.boolean(),
useYears: yup.boolean(),
useBlankDay: yup.boolean(),
allowOnlyOneBlankDayField: yup.boolean(),
seconds: yup.object({
lowerLimit: yup
.number()
.min(preset.seconds.minValue)
.max(preset.seconds.maxValue),
upperLimit: yup
.number()
.min(preset.seconds.minValue)
.max(preset.seconds.maxValue),
}),
minutes: yup.object({
lowerLimit: yup
.number()
.min(preset.minutes.minValue)
.max(preset.minutes.maxValue),
upperLimit: yup
.number()
.min(preset.minutes.minValue)
.max(preset.minutes.maxValue),
}),
hours: yup.object({
lowerLimit: yup
.number()
.min(preset.hours.minValue)
.max(preset.hours.maxValue),
upperLimit: yup
.number()
.min(preset.hours.minValue)
.max(preset.hours.maxValue),
}),
daysOfMonth: yup.object({
lowerLimit: yup
.number()
.min(preset.daysOfMonth.minValue)
.max(preset.daysOfMonth.maxValue),
upperLimit: yup
.number()
.min(preset.daysOfMonth.minValue)
.max(preset.daysOfMonth.maxValue),
}),
months: yup.object({
lowerLimit: yup
.number()
.min(preset.months.minValue)
.max(preset.months.maxValue),
upperLimit: yup
.number()
.min(preset.months.minValue)
.max(preset.months.maxValue),
}),
daysOfWeek: yup.object({
lowerLimit: yup
.number()
.min(preset.daysOfWeek.minValue)
.max(preset.daysOfWeek.maxValue),
upperLimit: yup
.number()
.min(preset.daysOfWeek.minValue)
.max(preset.daysOfWeek.maxValue),
}),
years: yup.object({
lowerLimit: yup
.number()
.min(preset.years.minValue)
.max(preset.years.maxValue),
upperLimit: yup
.number()
.min(preset.years.minValue)
.max(preset.years.maxValue),
}),
})
const optionsSchema = yup
.object({
presetId: yup.string().required(),
preset: optionPresetSchema.required(),
useSeconds: yup.boolean().required(),
useYears: yup.boolean().required(),
useBlankDay: yup.boolean().required(),
allowOnlyOneBlankDayField: yup.boolean().required(),
seconds: yup
.object({
lowerLimit: yup
.number()
.min(preset.seconds.minValue)
.max(preset.seconds.maxValue),
upperLimit: yup
.number()
.min(preset.seconds.minValue)
.max(preset.seconds.maxValue),
})
.required(),
minutes: yup
.object({
lowerLimit: yup
.number()
.min(preset.minutes.minValue)
.max(preset.minutes.maxValue),
upperLimit: yup
.number()
.min(preset.minutes.minValue)
.max(preset.minutes.maxValue),
})
.required(),
hours: yup
.object({
lowerLimit: yup
.number()
.min(preset.hours.minValue)
.max(preset.hours.maxValue),
upperLimit: yup
.number()
.min(preset.hours.minValue)
.max(preset.hours.maxValue),
})
.required(),
daysOfMonth: yup
.object({
lowerLimit: yup
.number()
.min(preset.daysOfMonth.minValue)
.max(preset.daysOfMonth.maxValue),
upperLimit: yup
.number()
.min(preset.daysOfMonth.minValue)
.max(preset.daysOfMonth.maxValue),
})
.required(),
months: yup
.object({
lowerLimit: yup
.number()
.min(preset.months.minValue)
.max(preset.months.maxValue),
upperLimit: yup
.number()
.min(preset.months.minValue)
.max(preset.months.maxValue),
})
.required(),
daysOfWeek: yup
.object({
lowerLimit: yup
.number()
.min(preset.daysOfWeek.minValue)
.max(preset.daysOfWeek.maxValue),
upperLimit: yup
.number()
.min(preset.daysOfWeek.minValue)
.max(preset.daysOfWeek.maxValue),
})
.required(),
years: yup
.object({
lowerLimit: yup
.number()
.min(preset.years.minValue)
.max(preset.years.maxValue),
upperLimit: yup
.number()
.min(preset.years.minValue)
.max(preset.years.maxValue),
})
.required(),
})
.required()

const validatedConfig: Options = optionsSchema.validateSync(
unvalidatedConfig,
Expand Down

0 comments on commit 6ba1270

Please sign in to comment.