Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ function computeDefaults(
defaults = schema.default;
}

switch (schema.type) {
switch (getSchemaType(schema)) {
// We need to recur for object schema inner default values.
case "object":
return Object.keys(schema.properties || {}).reduce((acc, key) => {
Expand Down
30 changes: 30 additions & 0 deletions test/anyOf_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,36 @@ describe("anyOf", () => {
expect(comp.state.formData).eql({ foo: "defaultbar" });
});

it("should assign a default value and set defaults on option change with 'type': 'object' missing", () => {
const { comp, node } = createFormComponent({
schema: {
type: "object",
anyOf: [
{
properties: {
foo: { type: "string", default: "defaultfoo" },
},
},
{
properties: {
foo: { type: "string", default: "defaultbar" },
},
},
],
},
});

expect(comp.state.formData).eql({ foo: "defaultfoo" });

const $select = node.querySelector("select");

Simulate.change($select, {
target: { value: $select.options[1].value },
});

expect(comp.state.formData).eql({ foo: "defaultbar" });
});

it("should render a custom widget", () => {
const schema = {
type: "object",
Expand Down
30 changes: 30 additions & 0 deletions test/oneOf_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,36 @@ describe("oneOf", () => {
expect(comp.state.formData).eql({ foo: "defaultbar" });
});

it("should assign a default value and set defaults on option change with 'type': 'object' missing", () => {
const { comp, node } = createFormComponent({
schema: {
type: "object",
oneOf: [
{
properties: {
foo: { type: "string", default: "defaultfoo" },
},
},
{
properties: {
foo: { type: "string", default: "defaultbar" },
},
},
],
},
});

expect(comp.state.formData).eql({ foo: "defaultfoo" });

const $select = node.querySelector("select");

Simulate.change($select, {
target: { value: $select.options[1].value },
});

expect(comp.state.formData).eql({ foo: "defaultbar" });
});

it("should render a custom widget", () => {
const schema = {
type: "object",
Expand Down
17 changes: 17 additions & 0 deletions test/utils_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,23 @@ describe("utils", () => {
});
});

it("should populate defaults for oneOf when 'type': 'object' is missing", () => {
const schema = {
type: "object",
oneOf: [
{
properties: { name: { type: "string", default: "a" } },
},
{
properties: { id: { type: "number", default: 13 } },
},
],
};
expect(getDefaultFormState(schema, {})).eql({
name: "a",
});
});

it("should populate nested default values for oneOf", () => {
const schema = {
type: "object",
Expand Down