From a356414afee42c5073ce8eb31c9e55974c861527 Mon Sep 17 00:00:00 2001 From: Sergio Betanzos Date: Mon, 21 Mar 2022 15:41:33 +0100 Subject: [PATCH] M #~: Fix dynamic schema on stepper (#1864) (cherry picked from commit 98d8fd484080dc8dd31ceea6ed4cd30207c0032a) --- .../CreateForm/Steps/Inputs/schema.js | 16 ++++++++--- .../Forms/Provision/CreateForm/Steps/index.js | 10 +++---- .../src/client/constants/userInput.js | 27 ++++++++++++++++--- src/fireedge/src/client/utils/schema.js | 25 ++++++++--------- 4 files changed, 52 insertions(+), 26 deletions(-) diff --git a/src/fireedge/src/client/components/Forms/Provision/CreateForm/Steps/Inputs/schema.js b/src/fireedge/src/client/components/Forms/Provision/CreateForm/Steps/Inputs/schema.js index ea87f087d2..9645493b6f 100644 --- a/src/fireedge/src/client/components/Forms/Provision/CreateForm/Steps/Inputs/schema.js +++ b/src/fireedge/src/client/components/Forms/Provision/CreateForm/Steps/Inputs/schema.js @@ -13,10 +13,14 @@ * See the License for the specific language governing permissions and * * limitations under the License. * * ------------------------------------------------------------------------- */ -/* eslint-disable jsdoc/require-jsdoc */ -import * as yup from 'yup' -import { getValidationFromFields, schemaUserInput } from 'client/utils' +import { object, ObjectSchema } from 'yup' +import { Field, getValidationFromFields, schemaUserInput } from 'client/utils' +import { UserInputOneProvisionObject } from 'client/constants' +/** + * @param {UserInputOneProvisionObject[]} inputs - Inputs + * @returns {Field[]} Inputs in Field format + */ export const FORM_FIELDS = (inputs) => inputs?.map( ({ @@ -44,5 +48,9 @@ export const FORM_FIELDS = (inputs) => } ) +/** + * @param {UserInputOneProvisionObject[]} inputs - Inputs + * @returns {ObjectSchema} Inputs step schema + */ export const STEP_FORM_SCHEMA = (inputs) => - yup.object(getValidationFromFields(FORM_FIELDS(inputs))) + object(getValidationFromFields(FORM_FIELDS(inputs))) diff --git a/src/fireedge/src/client/components/Forms/Provision/CreateForm/Steps/index.js b/src/fireedge/src/client/components/Forms/Provision/CreateForm/Steps/index.js index d54835054c..2b14814954 100644 --- a/src/fireedge/src/client/components/Forms/Provision/CreateForm/Steps/index.js +++ b/src/fireedge/src/client/components/Forms/Provision/CreateForm/Steps/index.js @@ -42,12 +42,10 @@ const Steps = createSteps([Template, Provider, BasicConfiguration, Inputs], { }) } - const resolvedInputs = provisionTemplateSelected?.inputs?.map((input) => { - const value = inputs[input?.name] - const ensuredValue = Array.isArray(value) ? value.join(';') : value - - return { ...input, value: ensuredValue } - }) + const resolvedInputs = provisionTemplateSelected?.inputs?.map((input) => ({ + ...input, + value: `${inputs[input?.name]}`, + })) return { ...provisionTemplateSelected, diff --git a/src/fireedge/src/client/constants/userInput.js b/src/fireedge/src/client/constants/userInput.js index 01fb5e4665..a2a8d906a8 100644 --- a/src/fireedge/src/client/constants/userInput.js +++ b/src/fireedge/src/client/constants/userInput.js @@ -32,17 +32,36 @@ */ /** - * @typedef {object} UserInputObject + * @typedef UserInputObject * @property {boolean} mandatory - If `true`, the input will be required * @property {UserInputType} type - Input type * @property {string} name - Name of input * @property {string} [description] - Description of input - * @property {number|string} [min] - Minimum value of range type input - * @property {number|string} [max] - Maximum value of range type input - * @property {string[]} [options] - Options available for the input + * @property {number|string} [min] - Minimum value. + * Valid for types: `range` or `range-float` + * @property {number|string} [max] - Maximum value. + * Valid for types: `range` or `range-float` + * @property {string|string[]} [options] - Available options. + * Valid for types: `list`, `list-multiple` or `array` * @property {number|string|string[]} [default] - Default value for the input */ +/** + * User input used on provision templates for OneProvision. + * + * @typedef UserInputOneProvisionObject + * @property {string} name - Name of input + * @property {string} description - Description of input + * @property {UserInputType} type - Type of input + * @property {number|string} [min_value] - Minimum value. + * Valid for types: `range` or `range-float` + * @property {number|string} [max_value] - Maximum value + * Valid for types: `range` or `range-float` + * @property {string|string[]} [options] - Available options. + * Valid for types: `list`, `list-multiple` or `array` + * @property {number|string|string[]} default - Default value for the input + */ + /** @enum {UserInputType} User input types */ export const USER_INPUT_TYPES = { text: 'text', diff --git a/src/fireedge/src/client/utils/schema.js b/src/fireedge/src/client/utils/schema.js index a91da72b3e..5eeddfa22e 100644 --- a/src/fireedge/src/client/utils/schema.js +++ b/src/fireedge/src/client/utils/schema.js @@ -233,15 +233,13 @@ const parseUserInputValue = (value) => { // ---------------------------------------------------------- /** - * Get input schema for the user input defined in OpenNebula resource. + * Get field properties to represent an user input defined by OpenNebula. * * @param {UserInputObject} userInput - User input from OpenNebula document - * @param {number|string|string[]} [userInput.default] - Default value for the input * @returns {Field} Field properties */ export const schemaUserInput = ({ mandatory, - name, type, options, default: defaultValue, @@ -413,23 +411,26 @@ export const createSteps = const stepCallbacks = typeof steps === 'function' ? steps(stepProps) : steps const performedSteps = stepCallbacks.map((step) => step(stepProps)) - const schemas = {} - for (const { id, resolver } of performedSteps) { - const schema = typeof resolver === 'function' ? resolver() : resolver + // Generate the schema in the last instance + const generateSchema = () => { + const schemas = {} + for (const { id, resolver } of performedSteps) { + const schema = typeof resolver === 'function' ? resolver() : resolver - schemas[id] = schema + schemas[id] = schema + } + + return object(schemas) } - const allResolver = object(schemas) - const defaultValues = initialValues - ? transformInitialValue(initialValues, allResolver) - : allResolver.default() + ? transformInitialValue(initialValues, generateSchema()) + : generateSchema().default() return { steps: performedSteps, defaultValues, - resolver: () => allResolver, + resolver: generateSchema, ...extraParams, } }