Skip to content

Commit c5c0784

Browse files
committed
Fix tests and rename initialValues
1 parent 91d8654 commit c5c0784

File tree

4 files changed

+40
-40
lines changed

4 files changed

+40
-40
lines changed

src/draft2019-09/methods/getData.test.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,8 +1700,8 @@ describe("getData (2019)", () => {
17001700
assert.deepEqual(res, { valid: "stays", invalid: "not removed" });
17011701
});
17021702
});
1703-
describe("defaultTemplateOptions.initialValues", () => {
1704-
it("should omit properties without default values when 'initialValues:false' even if required", () => {
1703+
describe("defaultTemplateOptions.useTypeDefaults", () => {
1704+
it("should omit properties without default values when 'useTypeDefaults:false' even if required", () => {
17051705
const node = compileSchema({
17061706
$schema: "draft-2019-09",
17071707
type: "object",
@@ -1713,13 +1713,13 @@ describe("getData (2019)", () => {
17131713
}
17141714
});
17151715
const res = node.getData(undefined, {
1716-
extendDefaults: false, initialValues: false
1716+
extendDefaults: false, useTypeDefaults: false
17171717
});
17181718

17191719
assert.deepEqual(res, { name: "John Doe", country: "USA" });
17201720
});
17211721

1722-
it("should omit properties without default values when 'initialValues:false' even if required in nested", () => {
1722+
it("should omit properties without default values when 'useTypeDefaults:false' even if required in nested", () => {
17231723
const node = compileSchema({
17241724
$schema: "draft-2019-09",
17251725
type: "object",
@@ -1742,7 +1742,7 @@ describe("getData (2019)", () => {
17421742
active: { type: "boolean", default: true }
17431743
}
17441744
});
1745-
const res = node.getData({}, { addOptionalProps: true, initialValues: false});
1745+
const res = node.getData({}, { addOptionalProps: true, useTypeDefaults: false});
17461746

17471747
assert.deepEqual(JSON.stringify(res), JSON.stringify({
17481748
user: {
@@ -1755,26 +1755,26 @@ describe("getData (2019)", () => {
17551755
}));
17561756
});
17571757

1758-
it("should handle type string with default value and 'initialValues:false'", () => {
1758+
it("should handle type string with default value and 'useTypeDefaults:false'", () => {
17591759
const node = compileSchema({
17601760
type: "string",
17611761
default: "default value"
17621762
});
17631763
const res = node.getData(undefined, {
1764-
initialValues: false
1764+
useTypeDefaults: false
17651765
});
17661766
assert.deepEqual(res, "default value");
17671767
});
17681768

1769-
it("should handle type string without default value and 'initialValues:false'", () => {
1769+
it("should handle type string without default value and 'useTypeDefaults:false'", () => {
17701770
const node = compileSchema({
17711771
type: "string",
17721772
});
1773-
const res = node.getData(undefined, {initialValues: false});
1773+
const res = node.getData(undefined, {useTypeDefaults: false});
17741774
assert.deepEqual(res, undefined);
17751775
});
17761776

1777-
it("should handle array without default value and 'initialValues:false'", () => {
1777+
it("should handle array without default value and 'useTypeDefaults:false'", () => {
17781778
const node = compileSchema({
17791779
$schema: "draft-2019-09",
17801780
type: "object",
@@ -1786,7 +1786,7 @@ describe("getData (2019)", () => {
17861786
}
17871787
}
17881788
});
1789-
const res = node.getData(undefined, {initialValues: false});
1789+
const res = node.getData(undefined, {useTypeDefaults: false});
17901790
assert.deepEqual(res, { title: [] });
17911791
});
17921792
});

src/draft2019-09/methods/getData.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export type TemplateOptions = {
2424
/**
2525
* Set to false to not use type specific initial values.Defaults to true
2626
*/
27-
initialValues?: boolean;
27+
useTypeDefaults?: boolean;
2828
/**
2929
* Limits how often a $ref should be followed before aborting. Prevents infinite data-structure.
3030
* Defaults to 1
@@ -177,11 +177,11 @@ export function getData(node: SchemaNode, data?: unknown, opts?: TemplateOptions
177177
}
178178

179179
const TYPE: Record<string, (node: SchemaNode, data: unknown, opts: TemplateOptions) => unknown> = {
180-
null: (node, data, opts) => getDefault(node, data, null, opts.initialValues),
181-
string: (node, data,opts) => getDefault(node, data, "", opts.initialValues),
182-
number: (node, data,opts) => getDefault(node, data, 0, opts.initialValues),
183-
integer: (node, data,opts) => getDefault(node, data, 0, opts.initialValues),
184-
boolean: (node, data,opts) => getDefault(node, data, false, opts.initialValues),
180+
null: (node, data, opts) => getDefault(node, data, null, opts.useTypeDefaults),
181+
string: (node, data,opts) => getDefault(node, data, "", opts.useTypeDefaults),
182+
number: (node, data,opts) => getDefault(node, data, 0, opts.useTypeDefaults),
183+
integer: (node, data,opts) => getDefault(node, data, 0, opts.useTypeDefaults),
184+
boolean: (node, data,opts) => getDefault(node, data, false, opts.useTypeDefaults),
185185
// object: (draft, schema, data: Record<string, unknown> | undefined, pointer: JsonPointer, opts: TemplateOptions) => {
186186
object: (node, data, opts) => {
187187
const schema = node.schema;
@@ -198,7 +198,7 @@ const TYPE: Record<string, (node: SchemaNode, data: unknown, opts: TemplateOptio
198198
// Omit adding a property if it is not required or optional props should be added
199199
if (value != null || isRequired || opts.addOptionalProps) {
200200
const propertyValue = propertyNode.getData(value, opts);
201-
if(propertyValue !== undefined){
201+
if (propertyValue !== undefined || opts.useTypeDefaults !== false) {
202202
d[propertyName] = propertyValue;
203203
}
204204
}
@@ -335,14 +335,14 @@ const TYPE: Record<string, (node: SchemaNode, data: unknown, opts: TemplateOptio
335335
}
336336
};
337337

338-
function getDefault({ schema }: SchemaNode, templateValue: any, initValue: any, initialValues: boolean) {
338+
function getDefault({ schema }: SchemaNode, templateValue: any, initValue: any, useTypeDefaults: boolean) {
339339
if (templateValue !== undefined) {
340340
return convertValue(schema.type, templateValue);
341341
} else if (schema.const) {
342342
return schema.const;
343343
} else if (schema.default === undefined && Array.isArray(schema.enum)) {
344344
return schema.enum[0];
345-
} else if (schema.default === undefined && initialValues !== false) {
345+
} else if (schema.default === undefined && useTypeDefaults !== false) {
346346
return initValue;
347347
}
348348
return schema.default;

src/methods/getData.test.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,8 +1656,8 @@ describe("getData", () => {
16561656
assert.deepEqual(res, { valid: "stays", invalid: "not removed" });
16571657
});
16581658
});
1659-
describe("defaultTemplateOptions.initialValues", () => {
1660-
it("should omit properties without default values when 'initialValues:false' even if required", () => {
1659+
describe("defaultTemplateOptions.useTypeDefaults", () => {
1660+
it("should omit properties without default values when 'useTypeDefaults:false' even if required", () => {
16611661
const node = compileSchema({
16621662
type: "object",
16631663
required: ["name", "age", "country"],
@@ -1668,13 +1668,13 @@ describe("getData", () => {
16681668
}
16691669
});
16701670
const res = node.getData(undefined, {
1671-
extendDefaults: false, initialValues: false
1671+
extendDefaults: false, useTypeDefaults: false
16721672
});
16731673

16741674
assert.deepEqual(res, { name: "John Doe", country: "USA" });
16751675
});
16761676

1677-
it("should omit properties without default values when 'initialValues:false' even if required in nested", () => {
1677+
it("should omit properties without default values when 'useTypeDefaults:false' even if required in nested", () => {
16781678
const node = compileSchema({
16791679
type: "object",
16801680
properties: {
@@ -1696,7 +1696,7 @@ describe("getData", () => {
16961696
active: { type: "boolean", default: true }
16971697
}
16981698
});
1699-
const res = node.getData({}, { addOptionalProps: true, initialValues: false});
1699+
const res = node.getData({}, { addOptionalProps: true, useTypeDefaults: false});
17001700

17011701
assert.deepEqual(JSON.stringify(res), JSON.stringify({
17021702
user: {
@@ -1709,26 +1709,26 @@ describe("getData", () => {
17091709
}));
17101710
});
17111711

1712-
it("should handle type string with default value and 'initialValues:false'", () => {
1712+
it("should handle type string with default value and 'useTypeDefaults:false'", () => {
17131713
const node = compileSchema({
17141714
type: "string",
17151715
default: "default value"
17161716
});
17171717
const res = node.getData(undefined, {
1718-
initialValues: false
1718+
useTypeDefaults: false
17191719
});
17201720
assert.deepEqual(res, "default value");
17211721
});
17221722

1723-
it("should handle type string without default value and 'initialValues:false'", () => {
1723+
it("should handle type string without default value and 'useTypeDefaults:false'", () => {
17241724
const node = compileSchema({
17251725
type: "string",
17261726
});
1727-
const res = node.getData(undefined, {initialValues: false});
1727+
const res = node.getData(undefined, {useTypeDefaults: false});
17281728
assert.deepEqual(res, undefined);
17291729
});
17301730

1731-
it("should handle array without default value and 'initialValues:false'", () => {
1731+
it("should handle array without default value and 'useTypeDefaults:false'", () => {
17321732
const node = compileSchema({
17331733
type: "object",
17341734
required: ["title"],
@@ -1739,7 +1739,7 @@ describe("getData", () => {
17391739
}
17401740
}
17411741
});
1742-
const res = node.getData(undefined, {initialValues: false});
1742+
const res = node.getData(undefined, {useTypeDefaults: false});
17431743
assert.deepEqual(res, { title: [] });
17441744
});
17451745
});

src/methods/getData.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export type TemplateOptions = {
2424
/**
2525
* Set to false to not use type specific initial values.Defaults to true
2626
*/
27-
initialValues?: boolean;
27+
useTypeDefaults?: boolean;
2828
/**
2929
* Limits how often a $ref should be followed before aborting. Prevents infinite data-structure.
3030
* Defaults to 1
@@ -168,11 +168,11 @@ export function getData(node: SchemaNode, data?: unknown, opts?: TemplateOptions
168168
}
169169

170170
const TYPE: Record<string, (node: SchemaNode, data: unknown, opts: TemplateOptions) => unknown> = {
171-
null: (node, data, opts) => getDefault(node, data, null, opts.initialValues),
172-
string: (node, data,opts) => getDefault(node, data, "", opts.initialValues),
173-
number: (node, data,opts) => getDefault(node, data, 0, opts.initialValues),
174-
integer: (node, data,opts) => getDefault(node, data, 0, opts.initialValues),
175-
boolean: (node, data,opts) => getDefault(node, data, false, opts.initialValues),
171+
null: (node, data, opts) => getDefault(node, data, null, opts.useTypeDefaults),
172+
string: (node, data,opts) => getDefault(node, data, "", opts.useTypeDefaults),
173+
number: (node, data,opts) => getDefault(node, data, 0, opts.useTypeDefaults),
174+
integer: (node, data,opts) => getDefault(node, data, 0, opts.useTypeDefaults),
175+
boolean: (node, data,opts) => getDefault(node, data, false, opts.useTypeDefaults),
176176
// object: (draft, schema, data: Record<string, unknown> | undefined, pointer: JsonPointer, opts: TemplateOptions) => {
177177
object: (node, data, opts) => {
178178
const schema = node.schema;
@@ -189,7 +189,7 @@ const TYPE: Record<string, (node: SchemaNode, data: unknown, opts: TemplateOptio
189189
// Omit adding a property if it is not required or optional props should be added
190190
if (value != null || isRequired || opts.addOptionalProps) {
191191
const propertyValue = propertyNode.getData(value, opts);
192-
if(propertyValue !== undefined){
192+
if(propertyValue !== undefined || opts.useTypeDefaults !== false) {
193193
d[propertyName] = propertyValue;
194194
}
195195
}
@@ -331,14 +331,14 @@ const TYPE: Record<string, (node: SchemaNode, data: unknown, opts: TemplateOptio
331331
}
332332
};
333333

334-
function getDefault({ schema }: SchemaNode, templateValue: any, initValue: any, initialValues: boolean) {
334+
function getDefault({ schema }: SchemaNode, templateValue: any, initValue: any, useTypeDefaults: boolean) {
335335
if (templateValue !== undefined) {
336336
return convertValue(schema.type, templateValue);
337337
} else if (schema.const) {
338338
return schema.const;
339339
} else if (schema.default === undefined && Array.isArray(schema.enum)) {
340340
return schema.enum[0];
341-
} else if (schema.default === undefined && initialValues !== false) {
341+
} else if (schema.default === undefined && useTypeDefaults !== false) {
342342
return initValue;
343343
}
344344
return schema.default;

0 commit comments

Comments
 (0)