add rough jt launch prompt validation

This commit is contained in:
Keith Grant
2020-05-04 15:57:32 -07:00
parent 11752e123d
commit 1ac92b0493
6 changed files with 76 additions and 19 deletions

View File

@@ -18,11 +18,15 @@ import mergeExtraVars from './mergeExtraVars';
import { useSteps, useVisitedSteps } from './hooks';
function LaunchPrompt({ config, resource, onLaunch, onCancel, i18n }) {
const { steps, initialValues, isReady, contentError } = useSteps(
config,
resource,
i18n
);
// const [formErrors, setFormErrors] = useState({});
const {
steps,
initialValues,
isReady,
validate,
formErrors,
contentError,
} = useSteps(config, resource, i18n);
const [visitedSteps, visitStep] = useVisitedSteps(config);
if (contentError) {
@@ -33,10 +37,10 @@ function LaunchPrompt({ config, resource, onLaunch, onCancel, i18n }) {
}
// TODO move into hook?
const validate = values => {
// return {};
return { limit: ['required field'] };
};
// const validate = values => {
// // return {};
// return { limit: ['required field'] };
// };
// TODO move into hook?
const submit = values => {

View File

@@ -5,16 +5,8 @@ import useOtherPromptsStep from './steps/useOtherPromptsStep';
import useSurveyStep from './steps/useSurveyStep';
import usePreviewStep from './steps/usePreviewStep';
// const INVENTORY = 'inventory';
// const CREDENTIALS = 'credentials';
// const PASSWORDS = 'passwords';
// const OTHER_PROMPTS = 'other';
// const SURVEY = 'survey';
// const PREVIEW = 'preview';
export function useSteps(config, resource, i18n) {
// TODO pass in form errors?
const formErrors = {};
const [formErrors, setFormErrors] = useState({});
const inventory = useInventoryStep(config, resource, i18n);
const credentials = useCredentialsStep(config, resource, i18n);
const otherPrompts = useOtherPromptsStep(config, resource, i18n);
@@ -54,7 +46,21 @@ export function useSteps(config, resource, i18n) {
survey.error ||
preview.error;
return { steps, initialValues, isReady, contentError };
const validate = values => {
const errors = {
...inventory.validate(values),
...credentials.validate(values),
...otherPrompts.validate(values),
...survey.validate(values),
};
setFormErrors(errors);
if (Object.keys(errors).length) {
return errors;
}
return false;
};
return { steps, initialValues, isReady, validate, formErrors, contentError };
}
export function usePromptErrors(config) {

View File

@@ -5,9 +5,18 @@ import CredentialsStep from './CredentialsStep';
const STEP_ID = 'credentials';
export default function useCredentialsStep(config, resource, i18n) {
const validate = values => {
const errors = {};
if (!values.credentials || !values.credentials.length) {
errors.credentials = i18n._(t`Credentials must be selected`);
}
return errors;
};
return {
step: getStep(config, i18n),
initialValues: getInitialValues(config, resource),
validate,
isReady: true,
error: null,
};

View File

@@ -5,9 +5,18 @@ import InventoryStep from './InventoryStep';
const STEP_ID = 'inventory';
export default function useInventoryStep(config, resource, i18n) {
const validate = values => {
const errors = {};
if (!values.inventory) {
errors.inventory = i18n._(t`An inventory must be selected`);
}
return errors;
};
return {
step: getStep(config, i18n),
initialValues: getInitialValues(config, resource),
validate,
isReady: true,
error: null,
};

View File

@@ -5,9 +5,18 @@ import OtherPromptsStep from './OtherPromptsStep';
const STEP_ID = 'other';
export default function useOtherPrompt(config, resource, i18n) {
const validate = values => {
const errors = {};
if (config.ask_job_type_on_launch && !values.job_type) {
errors.job_type = i18n._(t`This field must not be blank`);
}
return errors;
};
return {
step: getStep(config, i18n),
initialValues: getInitialValues(config, resource),
validate,
isReady: true,
error: null,
};

View File

@@ -27,6 +27,7 @@ export default function useSurveyStep(config, resource, i18n) {
return {
step: getStep(config, survey, i18n),
initialValues: getInitialValues(config, survey),
validate: getValidate(config, survey, i18n),
survey,
isReady: !isLoading && !!survey,
error,
@@ -58,3 +59,22 @@ function getInitialValues(config, survey) {
});
return values;
}
function getValidate(config, survey, i18n) {
return values => {
if (!config.survey_enabled || !survey || !survey.spec) {
return {};
}
const errors = {};
survey.spec.forEach(question => {
// TODO validate min/max
// TODO allow 0
if (question.required && !values[question.variable]) {
errors[`survey_${question.variable}`] = i18n._(
t`This field must not be blank`
);
}
});
return errors;
};
}