Skip to content

Commit

Permalink
fix: only allow a array as value in variable option (#474)
Browse files Browse the repository at this point in the history
* fix: only allow a array as value in variable option

* fix: fix tests

---------

Co-authored-by: Jordy Quak <[email protected]>
  • Loading branch information
jrquak and Jordy Quak authored Nov 13, 2024
1 parent 66f7e0b commit 7705f02
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 6 deletions.
52 changes: 52 additions & 0 deletions __tests__/validations/component.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,32 @@ test('Success when the optionRefs of the prefabs are valid', (t: Context): void
t.notThrows(() => validatePrefabs(prefabs, {}));
});

test('Throws error when value is an empty string in variable option', (t: Context): void => {
const prefabs: Prefab[] = [
{
name: 'Test Component',
icon: 'PencilIcon',
category: 'LAYOUT',
structure: [
{
name: 'option',
options: [
{
value: '',
label: 'label',
key: 'key',
type: 'VARIABLE',
},
],
descendants: [],
},
],
},
];

t.throws(() => validatePrefabs(prefabs, {}));
});

test('Does not throw when wrapper option has a optionRef', (t: Context): void => {
const prefabs = [
{
Expand Down Expand Up @@ -1123,3 +1149,29 @@ test('Does not throw when wrapper option has a optionRef', (t: Context): void =>

t.notThrows(() => validatePrefabs(prefabs, {}));
});

test('Success when value is an array with empty string in variable option', (t: Context): void => {
const prefabs: Prefab[] = [
{
name: 'Component Name',
icon: 'TitleIcon',
category: 'CONTENT',
structure: [
{
name: 'something',
options: [
{
value: [''],
label: 'something',
key: 'something',
type: 'VARIABLE',
},
],
descendants: [],
},
],
},
];

t.notThrows(() => validatePrefabs(prefabs, {}));
});
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ export interface PrefabComponentOptionBase {
}

export interface ValueDefault {
value: string | ValueConfig;
value: string | string[] | ValueConfig;
ref?: { id?: string };
}

Expand Down
16 changes: 11 additions & 5 deletions src/validations/prefab/componentOption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,18 +138,24 @@ const OptionRefInheritObject = Joi.object({
useKey: Joi.string(),
});

const optionValueSchema = Joi.when('ref', {
is: Joi.object({ value: Joi.exist() }).exist(),
then: Joi.forbidden(),
otherwise: Joi.any(),
}).when('type', {
is: 'VARIABLE',
then: Joi.array().items(Joi.string().allow('')).required(),
otherwise: Joi.any(),
});

export const optionSchema = Joi.object({
label: Joi.string().required(),
key: Joi.string().required(),
type: Joi.string()
.valid(...OPTIONS)
.required(),
configuration: optionConfigurationSchema,
value: Joi.when('ref', {
is: Joi.object({ value: Joi.exist() }).exist(),
then: Joi.forbidden(),
otherwise: Joi.any(),
}),
value: optionValueSchema,
showInAddChild: Joi.boolean(),
showInReconfigure: Joi.boolean(),
ref: refSchema,
Expand Down

0 comments on commit 7705f02

Please sign in to comment.