1
0
Fork 0
freesewing/sites/shared/components/workbench/views/edit/settings-validator.mjs

92 lines
3 KiB
JavaScript
Raw Normal View History

2023-06-06 16:58:59 -05:00
/** A utility for validating a gist against a patternConfig */
class SettingsValidator {
givenSettings
patternConfig
2022-12-14 16:37:17 -06:00
errors
valid = true
2023-06-06 16:58:59 -05:00
setGist(givenSettings, patternConfig) {
this.givenSettings = givenSettings
this.patternConfig = patternConfig
2022-12-14 16:37:17 -06:00
this.errors = {}
2022-12-22 11:46:26 -06:00
this.valid = true
2022-12-14 16:37:17 -06:00
}
2022-12-16 11:58:39 -06:00
/** check that the required measurements are all there and the correct type */
2022-12-14 16:37:17 -06:00
validateMeasurements() {
2023-06-06 16:58:59 -05:00
if (!this.givenSettings.measurements && this.patternConfig.measurements.length) {
2022-12-16 11:58:39 -06:00
this.errors.measurements = 'MissingMeasurements'
this.valid = false
return
}
2022-12-14 16:37:17 -06:00
this.errors.measurements = {}
2023-06-06 16:58:59 -05:00
for (const m of this.patternConfig.measurements || []) {
if (this.givenSettings.measurements[m] === undefined) {
2022-12-14 16:37:17 -06:00
this.errors.measurements[m] = 'MissingMeasurement'
this.valid = false
2023-06-06 16:58:59 -05:00
} else if (isNaN(this.givenSettings.measurements[m])) {
2022-12-14 16:37:17 -06:00
this.errors.measurements[m] = 'TypeError'
this.valid = false
}
}
}
2023-06-06 16:58:59 -05:00
/** check validity of any options that are included */
2022-12-14 16:37:17 -06:00
validateOptions() {
2022-12-15 19:27:05 -06:00
this.errors.options = {}
2023-06-06 16:58:59 -05:00
const configOpts = this.patternConfig.options
const settingsOpts = this.givenSettings.options
for (const o in settingsOpts) {
2022-12-15 19:27:05 -06:00
const configOpt = configOpts[o]
2023-06-06 16:58:59 -05:00
const settingsOpt = settingsOpts[o]
2022-12-16 11:58:39 -06:00
// if the option doesn't exist on the pattern
2022-12-15 19:27:05 -06:00
if (!configOpt) {
this.errors.options[o] = 'UnknownOption'
}
// if it's a constant option, mark that it can't be overwritten
else if (typeof configOpt !== 'object') {
this.errors.options[o] = 'ConstantOption'
}
// if it's a list option but the selection isn't in the list, mark it an unknown selection
else if (configOpt.list !== undefined) {
2023-06-06 16:58:59 -05:00
if (!configOpt.list.includes(settingsOpt) && settingsOpt != configOpt.dflt)
2022-12-15 19:27:05 -06:00
this.error.options[o] = 'UnknownOptionSelection'
}
// if it's a boolean option but the gist value isn't a boolean. mark a type error
else if (configOpts[o].bool !== undefined) {
2023-06-06 16:58:59 -05:00
if (typeof settingsOpt !== 'boolean') this.errors.options[o] = 'TypeError'
2022-12-15 19:27:05 -06:00
}
// all other options are numbers, so check it's a number
2023-06-06 16:58:59 -05:00
else if (isNaN(settingsOpt)) {
2022-12-15 19:27:05 -06:00
this.errors.options[o] = 'TypeError'
}
// if still no error, check the bounds
else {
2023-06-06 16:58:59 -05:00
const checkNum = configOpt.pct ? settingsOpt * 100 : settingsOpt
2022-12-15 19:27:05 -06:00
if (checkNum < configOpt.min || checkNum > configOpt.max) {
this.errors.options[o] = 'RangeError'
}
}
if (this.errors.options[o]) this.valid = false
}
2022-12-14 16:37:17 -06:00
}
2022-12-16 11:58:39 -06:00
/** run all validations */
2022-12-14 16:37:17 -06:00
validate() {
this.validateMeasurements()
this.validateOptions()
return { valid: this.valid, errors: this.errors }
}
}
2023-06-06 16:58:59 -05:00
const validator = new SettingsValidator()
2022-12-22 11:46:26 -06:00
2022-12-16 11:58:39 -06:00
/** make and run a gist validator */
2023-06-06 16:58:59 -05:00
export function validateSettings(givenSettings, patternConfig) {
validator.setGist(givenSettings, patternConfig)
2022-12-14 16:37:17 -06:00
return validator.validate()
}