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

View File

@@ -5,16 +5,8 @@ import useOtherPromptsStep from './steps/useOtherPromptsStep';
import useSurveyStep from './steps/useSurveyStep'; import useSurveyStep from './steps/useSurveyStep';
import usePreviewStep from './steps/usePreviewStep'; 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) { export function useSteps(config, resource, i18n) {
// TODO pass in form errors? const [formErrors, setFormErrors] = useState({});
const formErrors = {};
const inventory = useInventoryStep(config, resource, i18n); const inventory = useInventoryStep(config, resource, i18n);
const credentials = useCredentialsStep(config, resource, i18n); const credentials = useCredentialsStep(config, resource, i18n);
const otherPrompts = useOtherPromptsStep(config, resource, i18n); const otherPrompts = useOtherPromptsStep(config, resource, i18n);
@@ -54,7 +46,21 @@ export function useSteps(config, resource, i18n) {
survey.error || survey.error ||
preview.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) { export function usePromptErrors(config) {

View File

@@ -5,9 +5,18 @@ import CredentialsStep from './CredentialsStep';
const STEP_ID = 'credentials'; const STEP_ID = 'credentials';
export default function useCredentialsStep(config, resource, i18n) { 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 { return {
step: getStep(config, i18n), step: getStep(config, i18n),
initialValues: getInitialValues(config, resource), initialValues: getInitialValues(config, resource),
validate,
isReady: true, isReady: true,
error: null, error: null,
}; };

View File

@@ -5,9 +5,18 @@ import InventoryStep from './InventoryStep';
const STEP_ID = 'inventory'; const STEP_ID = 'inventory';
export default function useInventoryStep(config, resource, i18n) { 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 { return {
step: getStep(config, i18n), step: getStep(config, i18n),
initialValues: getInitialValues(config, resource), initialValues: getInitialValues(config, resource),
validate,
isReady: true, isReady: true,
error: null, error: null,
}; };

View File

@@ -5,9 +5,18 @@ import OtherPromptsStep from './OtherPromptsStep';
const STEP_ID = 'other'; const STEP_ID = 'other';
export default function useOtherPrompt(config, resource, i18n) { 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 { return {
step: getStep(config, i18n), step: getStep(config, i18n),
initialValues: getInitialValues(config, resource), initialValues: getInitialValues(config, resource),
validate,
isReady: true, isReady: true,
error: null, error: null,
}; };

View File

@@ -27,6 +27,7 @@ export default function useSurveyStep(config, resource, i18n) {
return { return {
step: getStep(config, survey, i18n), step: getStep(config, survey, i18n),
initialValues: getInitialValues(config, survey), initialValues: getInitialValues(config, survey),
validate: getValidate(config, survey, i18n),
survey, survey,
isReady: !isLoading && !!survey, isReady: !isLoading && !!survey,
error, error,
@@ -58,3 +59,22 @@ function getInitialValues(config, survey) {
}); });
return values; 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;
};
}