Skip to content

Commit a51bf1e

Browse files
authored
fix(tio-params): disable empty values for required+enum booleans (#4615)
1 parent 690e274 commit a51bf1e

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

src/core/json-schema-components.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ export class JsonSchema_boolean extends Component {
201201

202202
onEnumChange = (val) => this.props.onChange(val)
203203
render() {
204-
let { getComponent, value, errors, schema } = this.props
204+
let { getComponent, value, errors, schema, required } = this.props
205205
errors = errors.toJS ? errors.toJS() : []
206206

207207
const Select = getComponent("Select")
@@ -210,7 +210,7 @@ export class JsonSchema_boolean extends Component {
210210
title={ errors.length ? errors : ""}
211211
value={ String(value) }
212212
allowedValues={ fromJS(schema.enum || ["true", "false"]) }
213-
allowEmptyValue={ true }
213+
allowEmptyValue={ !schema.enum || !required }
214214
onChange={ this.onEnumChange }/>)
215215
}
216216
}

test/components/json-schema-form.js

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,54 @@ describe("<JsonSchemaForm/>", function(){
8686
expect(wrapper.find("select option").eq(2).text()).toEqual("false")
8787
})
8888

89+
90+
it("should render the correct options for an enum boolean parameter", function(){
91+
92+
let props = {
93+
getComponent: getComponentStub,
94+
value: "",
95+
onChange: () => {},
96+
keyName: "",
97+
fn: {},
98+
schema: {
99+
type: "boolean",
100+
enum: ["true"]
101+
}
102+
}
103+
104+
let wrapper = render(<JsonSchemaForm {...props}/>)
105+
106+
expect(wrapper.find("select").length).toEqual(1)
107+
expect(wrapper.find("select option").length).toEqual(2)
108+
expect(wrapper.find("select option").eq(0).text()).toEqual("--")
109+
expect(wrapper.find("select option").eq(1).text()).toEqual("true")
110+
expect(wrapper.find("select option:checked").first().text()).toEqual("--")
111+
})
112+
113+
it("should render the correct options for a required boolean parameter", function(){
114+
115+
let props = {
116+
getComponent: getComponentStub,
117+
value: "",
118+
onChange: () => {},
119+
keyName: "",
120+
fn: {},
121+
schema: {
122+
type: "boolean",
123+
required: true
124+
}
125+
}
126+
127+
let wrapper = render(<JsonSchemaForm {...props}/>)
128+
129+
expect(wrapper.find("select").length).toEqual(1)
130+
expect(wrapper.find("select option").length).toEqual(3)
131+
expect(wrapper.find("select option").eq(0).text()).toEqual("--")
132+
expect(wrapper.find("select option").eq(1).text()).toEqual("true")
133+
expect(wrapper.find("select option").eq(2).text()).toEqual("false")
134+
expect(wrapper.find("select option:checked").first().text()).toEqual("--")
135+
})
136+
89137
it("should render the correct options for a required enum boolean parameter", function(){
90138

91139
let props = {
@@ -105,7 +153,8 @@ describe("<JsonSchemaForm/>", function(){
105153

106154
expect(wrapper.find("select").length).toEqual(1)
107155
expect(wrapper.find("select option").length).toEqual(1)
108-
expect(wrapper.find("select option").first().text()).toEqual("true")
156+
expect(wrapper.find("select option").eq(0).text()).toEqual("true")
157+
expect(wrapper.find("select option:checked").first().text()).toEqual("true")
109158
})
110159
})
111160
describe("objects", function() {

0 commit comments

Comments
 (0)