Skip to content

Commit

Permalink
update required validator to account for a required boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
mantis-toboggan-md committed Dec 20, 2023
1 parent 725b555 commit 2ff9339
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
6 changes: 4 additions & 2 deletions pkg/capi/components/CCVariables/Variable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import LabeledSelect from '@shell/components/form/LabeledSelect';
import formRulesGenerator, { Validator } from '@shell/utils/validators/formRules';
import type { ClusterClassVariable } from '../../types/clusterClass';
import { openAPIV3SchemaValidators } from '../../util/validators';
import { isDefined, openAPIV3SchemaValidators } from '../../util/validators';
export default defineComponent({
name: 'CCVariable',
Expand Down Expand Up @@ -98,7 +98,9 @@ export default defineComponent({
const required = this.variable?.required;
if (required) {
out.push(formRulesGenerator(this.$store.getters['i18n/t'], { key: this.variable.name }).required as Validator);
// out.push(formRulesGenerator(this.$store.getters['i18n/t'], { key: this.variable.name }).required as Validator);
out.push(val => !isDefined(val) ? this.$store.getters['i18n/t']('validation.required', { key: this.variable.name }) : null);
}
return out;
Expand Down
17 changes: 12 additions & 5 deletions pkg/capi/components/CCVariables/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { PropType } from 'vue';
import { randomStr } from '@shell/utils/string';
import { ClusterClassVariable } from '../../types/clusterClass';
import type { CapiClusterVariable } from '../../types/cluster.x-k8s.io.cluster';
import { isDefined } from '../../util/validators';
import Variable from './Variable.vue';
export default defineComponent({
Expand Down Expand Up @@ -90,12 +91,15 @@ export default defineComponent({
}
const oldDefault = (old || []).find(d => d.name === existingVar.name)?.schema?.openAPIV3Schema?.default;
if (oldDefault && existingVar.value === oldDefault) {
if (isDefined(oldDefault) && existingVar.value === oldDefault) {
delete existingVar.value;
}
const newDefault = neuDef.schema?.openAPIV3Schema?.default;
let newDefault = neuDef.schema?.openAPIV3Schema?.default;
if (newDefault && !existingVar.value) {
if (neuDef.schema?.openAPIV3Schema?.type === 'boolean' && !newDefault) {
newDefault = false;
}
if (isDefined(newDefault) && !existingVar.value) {
existingVar.value = newDefault;
}
acc.push(existingVar);
Expand All @@ -104,9 +108,12 @@ export default defineComponent({
}, []);
neu.forEach((def) => {
const newDefault = def.schema?.openAPIV3Schema?.default;
let newDefault = def.schema?.openAPIV3Schema?.default;
if (newDefault && !out.find(v => v.name === def.name)) {
if (def.schema?.openAPIV3Schema?.type === 'boolean' && !newDefault) {
newDefault = false;
}
if (isDefined(newDefault) && !out.find(v => v.name === def.name)) {
out.push({ name: def.name, value: newDefault });
}
});
Expand Down
2 changes: 2 additions & 0 deletions pkg/capi/util/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,5 @@ export const openAPIV3SchemaValidators = function(t: Translation, { key = 'Value

return out;
};

export const isDefined = (val: any) => val || val === false;

0 comments on commit 2ff9339

Please sign in to comment.