Skip to content

Commit b3f427d

Browse files
committed
docs: add description on useTypeDefaults-option
1 parent ef5f74c commit b3f427d

File tree

2 files changed

+102
-16
lines changed

2 files changed

+102
-16
lines changed

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,50 @@ console.log(data); // { valid: "stays", invalid: "removed" }
539539

540540
</details>
541541

542+
<details><summary>Option: useTypeDefaults (default: true)</summary>
543+
544+
With `useTypeDefaults:true`, `getData` will return initial values for all primitives (non-objects/arrays) that do not have a default-property set:
545+
546+
```ts
547+
const data = compileSchema({
548+
required: ["valid"],
549+
properties: { valid: { type: "string" } }
550+
}).getData(null, { useTypeDefaults: true });
551+
console.log(data); // { valid: "" }
552+
```
553+
554+
Setting `useTypeDefaults:false` will _not_ remove data that is valid, but unspecified:
555+
556+
```ts
557+
const data = compileSchema({
558+
required: ["valid"],
559+
properties: { valid: { type: "string" } }
560+
}).getData(null, { useTypeDefaults: false });
561+
console.log(data); // {}
562+
```
563+
564+
_Note_ object and array-properties will still be added if required:
565+
566+
```ts
567+
const data = compileSchema({
568+
required: ["valid"],
569+
properties: { valid: { type: "object" } }
570+
}).getData(null, { useTypeDefaults: true });
571+
console.log(data); // { valid: {} }
572+
```
573+
574+
_Note_ enforced array-items will be `undefined` if required and `initialValues: false`
575+
576+
```ts
577+
const data = compileSchema({
578+
required: ["valid"],
579+
properties: { valid: { type: "array", items: { type: "string" }, minItems: 1 } }
580+
}).getData(null, { useTypeDefaults: false });
581+
console.log(data); // { valid: [undefined] }
582+
```
583+
584+
</details>
585+
542586
### getNode
543587

544588
`getNode` returns the JSON Schema from data location specified by a JSON Pointer. In many cases the JSON Schema can be retrieved without passing any data, but in situations where the schema is dynamic (for example in _oneOf_, _dependencies_, etc.), input-data is required or `getNode` will return a _JsonError_ as is done when the JSON Pointer path is invalid.

src/methods/getData.test.ts

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,6 +1657,44 @@ describe("getData", () => {
16571657
});
16581658
});
16591659
describe("defaultTemplateOptions.useTypeDefaults", () => {
1660+
it("should return initial value for missing default-properties", () => {
1661+
const data = compileSchema({
1662+
required: ["valid"],
1663+
properties: { valid: { type: "string" } }
1664+
}).getData(null, { useTypeDefaults: true });
1665+
1666+
assert.deepEqual(data, { valid: "" });
1667+
});
1668+
1669+
it("should omit initial value for missing default-properties", () => {
1670+
const data = compileSchema({
1671+
required: ["valid"],
1672+
properties: { valid: { type: "string" } }
1673+
}).getData(null, { useTypeDefaults: false });
1674+
1675+
assert.deepEqual(data, {});
1676+
});
1677+
1678+
// @todo: reevaluate this behaviour
1679+
it("should return initial object-value for missing default-properties, even if useTypeDefaults: false", () => {
1680+
const data = compileSchema({
1681+
required: ["valid"],
1682+
properties: { valid: { type: "string" } }
1683+
}).getData(null, { useTypeDefaults: false });
1684+
1685+
assert.deepEqual(data, {});
1686+
});
1687+
1688+
// @todo: reevaluate this behaviour
1689+
it("should return initial array-value for missing default-properties", () => {
1690+
const data = compileSchema({
1691+
required: ["valid"],
1692+
properties: { valid: { type: "array", items: { type: "string" }, minItems: 1 } }
1693+
}).getData(null, { useTypeDefaults: false });
1694+
1695+
assert.deepEqual(data, { valid: [undefined] });
1696+
});
1697+
16601698
it("should omit properties without default values when 'useTypeDefaults:false' even if required", () => {
16611699
const node = compileSchema({
16621700
type: "object",
@@ -1668,7 +1706,8 @@ describe("getData", () => {
16681706
}
16691707
});
16701708
const res = node.getData(undefined, {
1671-
extendDefaults: false, useTypeDefaults: false
1709+
extendDefaults: false,
1710+
useTypeDefaults: false
16721711
});
16731712

16741713
assert.deepEqual(res, { name: "John Doe", country: "USA" });
@@ -1696,20 +1735,23 @@ describe("getData", () => {
16961735
active: { type: "boolean", default: true }
16971736
}
16981737
});
1699-
const res = node.getData({}, { addOptionalProps: true, useTypeDefaults: false});
1738+
const res = node.getData({}, { addOptionalProps: true, useTypeDefaults: false });
17001739

1701-
assert.deepEqual(JSON.stringify(res), JSON.stringify({
1702-
user: {
1703-
username: "guest",
1704-
profile: {
1705-
theme: "light"
1706-
}
1707-
},
1708-
active: true
1709-
}));
1740+
assert.deepEqual(
1741+
JSON.stringify(res),
1742+
JSON.stringify({
1743+
user: {
1744+
username: "guest",
1745+
profile: {
1746+
theme: "light"
1747+
}
1748+
},
1749+
active: true
1750+
})
1751+
);
17101752
});
17111753

1712-
it("should handle type string with default value and 'useTypeDefaults:false'", () => {
1754+
it("should handle type string with default value and 'useTypeDefaults:false'", () => {
17131755
const node = compileSchema({
17141756
type: "string",
17151757
default: "default value"
@@ -1722,9 +1764,9 @@ describe("getData", () => {
17221764

17231765
it("should handle type string without default value and 'useTypeDefaults:false'", () => {
17241766
const node = compileSchema({
1725-
type: "string",
1767+
type: "string"
17261768
});
1727-
const res = node.getData(undefined, {useTypeDefaults: false});
1769+
const res = node.getData(undefined, { useTypeDefaults: false });
17281770
assert.deepEqual(res, undefined);
17291771
});
17301772

@@ -1733,13 +1775,13 @@ describe("getData", () => {
17331775
type: "object",
17341776
required: ["title"],
17351777
properties: {
1736-
title: {
1778+
title: {
17371779
type: "array",
17381780
items: { type: "string" }
17391781
}
17401782
}
17411783
});
1742-
const res = node.getData(undefined, {useTypeDefaults: false});
1784+
const res = node.getData(undefined, { useTypeDefaults: false });
17431785
assert.deepEqual(res, { title: [] });
17441786
});
17451787
});

0 commit comments

Comments
 (0)