Skip to content

Commit e0afad3

Browse files
committed
Fixed referenced tuples
1 parent bfd6144 commit e0afad3

File tree

3 files changed

+48
-22
lines changed

3 files changed

+48
-22
lines changed

__tests__/v2/JSchema.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,31 @@ describe('JSchema', () => {
949949
await fireEvent.click(result.getByRole('button', { name: 'Clear' }));
950950
expect(result.component.getArguments()).deep.eq({ arg_A: [[[undefined]]] });
951951
});
952+
953+
it('referenced tuple', async function () {
954+
const result = renderSchemaWithReferencedProperty({
955+
type: 'array',
956+
minItems: 1,
957+
maxItems: 1,
958+
items: [
959+
{
960+
type: 'array',
961+
minItems: 2,
962+
maxItems: 2,
963+
items: [{ type: 'string' }, { type: 'string' }]
964+
}
965+
]
966+
});
967+
968+
expect(result.component.getArguments()).deep.eq({ testProp: [] });
969+
expect(result.component.unsavedChanges).toEqual(false);
970+
await fireEvent.click(result.getByRole('button', { name: 'Add tuple' }));
971+
expect(result.component.unsavedChanges).toEqual(true);
972+
expect(result.component.getArguments()).deep.eq({ testProp: [[]] });
973+
await fireEvent.click(result.getByRole('button', { name: 'Add tuple' }));
974+
expect(result.component.getArguments()).deep.eq({ testProp: [[null, null]] });
975+
expect(result.getAllByRole('textbox').length).eq(2);
976+
});
952977
});
953978

954979
/**

src/lib/components/common/jschema/ArrayProperty.svelte

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
}
2424
// Create mandatory fields in case of minItems
2525
if (schemaProperty.isRequired()) {
26-
const minItems = getMinItems(schemaProperty.referenceSchema);
26+
const minItems = getMinItems();
2727
if (minItems !== null) {
2828
for (let i = count; i < minItems; i++) {
2929
addNestedProperty(undefined, false);
@@ -34,23 +34,18 @@
3434
});
3535
3636
/**
37-
* @param {object} referenceSchema
3837
* @returns {number|null}
3938
*/
40-
function getMinItems(referenceSchema) {
41-
if ('minItems' in referenceSchema) {
42-
return /** @type {number} */ (referenceSchema.minItems);
39+
function getMinItems() {
40+
if ('minItems' in schemaProperty) {
41+
return /** @type {number} */ (schemaProperty.minItems);
4342
}
4443
return null;
4544
}
4645
47-
/**
48-
* @param {object} referenceSchema
49-
* @returns {number|null}
50-
*/
51-
function getMaxItems(referenceSchema) {
52-
if ('maxItems' in referenceSchema) {
53-
return /** @type {number} */ (referenceSchema.maxItems);
46+
function getMaxItems() {
47+
if ('maxItems' in schemaProperty) {
48+
return /** @type {number} */ (schemaProperty.maxItems);
5449
}
5550
return null;
5651
}
@@ -97,17 +92,16 @@
9792
* @param {any[]} nestedProperties
9893
*/
9994
function canAddMoreItems(nestedProperties) {
100-
const maxItems = getMaxItems(schemaProperty.referenceSchema);
95+
const maxItems = getMaxItems();
10196
return maxItems === null || nestedProperties.length < maxItems;
10297
}
10398
10499
/**
105-
* @param {import('$lib/components/common/jschema/schema_management').SchemaProperty} schemaProperty
106100
* @returns {boolean} true if has fixed length (minItems === maxItems), false otherwise
107101
*/
108-
function isTuple(schemaProperty) {
109-
const minItems = getMinItems(schemaProperty.referenceSchema);
110-
const maxItems = getMaxItems(schemaProperty.referenceSchema);
102+
function isTuple() {
103+
const minItems = getMinItems();
104+
const maxItems = getMaxItems();
111105
return minItems !== null && maxItems !== null && minItems === maxItems;
112106
}
113107
@@ -116,13 +110,13 @@
116110
* @returns {boolean} true if the nested properties can be removed, false otherwise
117111
*/
118112
function showRemoveButton(schemaProperty) {
119-
if (isTuple(schemaProperty)) {
113+
if (isTuple()) {
120114
return false;
121115
}
122116
if (!schemaProperty.isRequired()) {
123117
return true;
124118
}
125-
const minItems = getMinItems(schemaProperty.referenceSchema);
119+
const minItems = getMinItems();
126120
if (minItems === null) {
127121
return true;
128122
}
@@ -132,7 +126,7 @@
132126
$: addNestedPropertyBtnDisabled = !canAddMoreItems(nestedProperties);
133127
134128
function addTuple() {
135-
const minItems = /** @type {number} */ (getMinItems(schemaProperty.referenceSchema));
129+
const minItems = /** @type {number} */ (getMinItems());
136130
if (
137131
!Array.isArray(schemaProperty.defaultValue) ||
138132
schemaProperty.defaultValue.length !== minItems
@@ -180,7 +174,7 @@
180174
>
181175
<div class="accordion-body p-1">
182176
<div class="d-flex justify-content-center p-2">
183-
{#if !isTuple(schemaProperty)}
177+
{#if !isTuple()}
184178
<button
185179
class="btn btn-primary"
186180
type="button"
@@ -229,7 +223,7 @@
229223
<PropertyDiscriminator schemaProperty={nestedProperty} />
230224
</div>
231225
<div class="align-self-right mt-2 me-2">
232-
{#if nestedProperties.length > 1 && !isTuple(schemaProperty)}
226+
{#if nestedProperties.length > 1 && !isTuple()}
233227
<button
234228
class="btn btn-light"
235229
on:click|preventDefault={() => moveUp(index)}

src/lib/components/common/jschema/schema_management.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,13 @@ export class SchemaProperty {
277277
this.hasCustomKeyValues = true;
278278
}
279279
}
280+
281+
if ('minItems' in propertySchema) {
282+
this.minItems = propertySchema.minItems;
283+
}
284+
if ('maxItems' in propertySchema) {
285+
this.maxItems = propertySchema.maxItems;
286+
}
280287
}
281288
}
282289

0 commit comments

Comments
 (0)