1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-21 14:50:08 +03:00

M #~: Fix dynamic schema on stepper (#1864)

(cherry picked from commit 98d8fd484080dc8dd31ceea6ed4cd30207c0032a)
This commit is contained in:
Sergio Betanzos 2022-03-21 15:41:33 +01:00 committed by Tino Vazquez
parent cbc7d099a6
commit a356414afe
No known key found for this signature in database
GPG Key ID: 14201E424D02047E
4 changed files with 52 additions and 26 deletions

View File

@ -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)))

View File

@ -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,

View File

@ -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',

View File

@ -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,
}
}