Skip to content

Commit 4c30cf2

Browse files
committed
feat: add support for 'additionalItems' keyword
1 parent 16a05fb commit 4c30cf2

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed

src/components/Examples/examples.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,79 @@
11
export default [
2+
{
3+
title: "additionalItems set to false",
4+
type: "array",
5+
items: [
6+
{
7+
type: "number",
8+
},
9+
{
10+
type: "string",
11+
},
12+
{
13+
type: "string",
14+
enum: ["Street", "Avenue", "Boulevard"],
15+
},
16+
{
17+
type: "string",
18+
enum: ["NW", "NE", "SW", "SE"],
19+
},
20+
],
21+
additionalItems: false,
22+
},
23+
{
24+
title: "additionalItems set to true",
25+
type: "array",
26+
items: [
27+
{
28+
type: "number",
29+
},
30+
{
31+
type: "string",
32+
},
33+
{
34+
type: "string",
35+
enum: ["Street", "Avenue", "Boulevard"],
36+
},
37+
{
38+
type: "string",
39+
enum: ["NW", "NE", "SW", "SE"],
40+
},
41+
],
42+
additionalItems: true,
43+
},
44+
{
45+
title: "additionalItems set to schema",
46+
type: "array",
47+
items: [
48+
{
49+
type: "number",
50+
},
51+
{
52+
type: "string",
53+
},
54+
{
55+
type: "string",
56+
enum: ["Street", "Avenue", "Boulevard"],
57+
},
58+
{
59+
type: "string",
60+
enum: ["NW", "NE", "SW", "SE"],
61+
},
62+
],
63+
additionalItems: {
64+
type: "number",
65+
},
66+
},
67+
{
68+
title: "additionalItems with object items",
69+
type: "array",
70+
items: {
71+
type: "number",
72+
},
73+
additionalItems: {
74+
type: "number",
75+
},
76+
},
277
{
378
title: "with a $comment",
479
description: "this is the description",

src/components/Instance/ArrayRestraints/index.svelte

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,24 @@
1010
{#if uniqueItems}
1111
<li>unique</li>
1212
{/if}
13+
14+
{#if show_additionalItems && isBoolean(additionalItems)}
15+
<li>additional items {additionalItems ? '' : 'not'} allowed</li>
16+
{/if}
1317
</ul>
1418

1519
<Items {items} href={`${href}/items`} />
1620

21+
{#if show_additionalItems && isPlainObject(additionalItems)}
22+
<BoxSegment legend="additional items">
23+
<Instance definition={additionalItems} href={`${href}/additionalItems`} />
24+
</BoxSegment>
25+
{/if}
26+
1727
<script>
28+
const isBoolean = require("lodash/isBoolean");
29+
const isPlainObject = require("lodash/isPlainObject");
30+
1831
export let href = "";
1932
2033
export let maxItems;
@@ -23,8 +36,14 @@
2336
export let maxContains;
2437
export let minContains;
2538
export let items;
39+
export let additionalItems;
40+
41+
let show_additionalItems;
42+
$: show_additionalItems = !isPlainObject(items);
2643
2744
import Items from "../Items/index.svelte";
45+
const Instance = require("../index.svelte").default;
46+
import BoxSegment from "../BoxSegment.svelte";
2847
</script>
2948

3049
<style>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const tap = require("../../../tests/svelte_loader");
2+
import { render } from "@testing-library/svelte";
3+
4+
const ArrayRestraints = require("./index.svelte");
5+
6+
tap.test("additionalItems", async (t) => {
7+
t.test("additionalItems, true", async (t) => {
8+
const { getByText } = render(ArrayRestraints, {
9+
additionalItems: true,
10+
});
11+
12+
t.ok(getByText("additional items allowed"));
13+
});
14+
15+
t.test("additionalItems, false", async (t) => {
16+
const { getByText } = render(ArrayRestraints, {
17+
items: [{ type: "string" }],
18+
additionalItems: false,
19+
});
20+
21+
t.ok(getByText("additional items not allowed"));
22+
});
23+
24+
t.test("additionalItems, boolean w/ items as object", async (t) => {
25+
const { queryByText } = render(ArrayRestraints, {
26+
items: {
27+
type: "string",
28+
},
29+
additionalItems: false,
30+
});
31+
32+
t.ok(!queryByText("additional items not allowed"), "not present");
33+
});
34+
35+
t.test("additionalItems, schema", async (t) => {
36+
const { getByText } = render(ArrayRestraints, {
37+
items: [{ type: "string" }],
38+
additionalItems: { type: "number" },
39+
});
40+
41+
t.ok(getByText("additional items"));
42+
});
43+
});

0 commit comments

Comments
 (0)