Skip to content

Commit 1e1b7f6

Browse files
authored
fix(try-it-out): pass Parameter validation messages around in props for OAS3 (#4162)
* default to empty `ImmutableMap` when grabbing op metadata * pass `errors` into JsonSchema components * Account for Immutable data structure in JavaScriptonSchema... ...and create empty Lists instead of Maps by default. * Pass ImmutableList through to JsonSchema child components
1 parent 2f23698 commit 1e1b7f6

File tree

3 files changed

+25
-18
lines changed

3 files changed

+25
-18
lines changed

src/core/components/parameter-row.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ export default class ParameterRow extends Component {
158158
<Markdown source={
159159
"<i>Available values</i>: " + paramItemsEnum.map(function(item) {
160160
return item
161-
}).toArray().join(", ")}/>
161+
}).toArray().join(", ")}/>
162162
: null
163163
}
164164

@@ -181,6 +181,7 @@ export default class ParameterRow extends Component {
181181
required={ required }
182182
description={param.get("description") ? `${param.get("name")} - ${param.get("description")}` : `${param.get("name")}`}
183183
onChange={ this.onChangeWrapper }
184+
errors={ param.get("errors") }
184185
schema={ isOAS3 && isOAS3() ? param.get("schema") : param }/>
185186
}
186187

src/core/json-schema-components.js

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { PureComponent, Component } from "react"
22
import PropTypes from "prop-types"
33
import { List, fromJS } from "immutable"
4+
import ImPropTypes from "react-immutable-proptypes"
45
//import "less/json-schema-form"
56

67
const noop = ()=> {}
@@ -11,6 +12,7 @@ const JsonSchemaPropShape = {
1112
keyName: PropTypes.any,
1213
fn: PropTypes.object.isRequired,
1314
schema: PropTypes.object,
15+
errors: ImPropTypes.list,
1416
required: PropTypes.bool,
1517
description: PropTypes.any
1618
}
@@ -20,7 +22,8 @@ const JsonSchemaDefaultProps = {
2022
onChange: noop,
2123
schema: {},
2224
keyName: "",
23-
required: false
25+
required: false,
26+
errors: List()
2427
}
2528

2629
export class JsonSchemaForm extends Component {
@@ -29,15 +32,15 @@ export class JsonSchemaForm extends Component {
2932
static defaultProps = JsonSchemaDefaultProps
3033

3134
render() {
32-
let { schema, value, onChange, getComponent, fn } = this.props
35+
let { schema, errors, value, onChange, getComponent, fn } = this.props
3336

3437
if(schema.toJS)
3538
schema = schema.toJS()
3639

3740
let { type, format="" } = schema
3841

3942
let Comp = (format ? getComponent(`JsonSchema_${type}_${format}`) : getComponent(`JsonSchema_${type}`)) || getComponent("JsonSchema_string")
40-
return <Comp { ...this.props } fn={fn} getComponent={getComponent} value={value} onChange={onChange} schema={schema}/>
43+
return <Comp { ...this.props } errors={errors} fn={fn} getComponent={getComponent} value={value} onChange={onChange} schema={schema}/>
4144
}
4245

4346
}
@@ -51,14 +54,15 @@ export class JsonSchema_string extends Component {
5154
}
5255
onEnumChange = (val) => this.props.onChange(val)
5356
render() {
54-
let { getComponent, value, schema, required, description } = this.props
57+
let { getComponent, value, schema, errors, required, description } = this.props
5558
let enumValue = schema["enum"]
56-
let errors = schema.errors || []
59+
60+
errors = errors.toJS ? errors.toJS() : []
5761

5862
if ( enumValue ) {
5963
const Select = getComponent("Select")
6064
return (<Select className={ errors.length ? "invalid" : ""}
61-
title={ errors.length ? errors : ""}
65+
title={ errors.length ? errors : ""}
6266
allowedValues={ enumValue }
6367
value={ value }
6468
allowEmptyValue={ !required }
@@ -70,14 +74,14 @@ export class JsonSchema_string extends Component {
7074
if (schema["type"] === "file") {
7175
return (<Input type="file"
7276
className={ errors.length ? "invalid" : ""}
73-
title={ errors.length ? errors : ""}
77+
title={ errors.length ? errors : ""}
7478
onChange={ this.onChange }
7579
disabled={isDisabled}/>)
7680
}
7781
else {
7882
return (<Input type={ schema.format === "password" ? "password" : "text" }
7983
className={ errors.length ? "invalid" : ""}
80-
title={ errors.length ? errors : ""}
84+
title={ errors.length ? errors : ""}
8185
value={value}
8286
placeholder={description}
8387
onChange={ this.onChange }
@@ -131,9 +135,10 @@ export class JsonSchema_array extends PureComponent {
131135
}
132136

133137
render() {
134-
let { getComponent, required, schema, fn } = this.props
138+
let { getComponent, required, schema, errors, fn } = this.props
139+
140+
errors = errors.toJS ? errors.toJS() : []
135141

136-
let errors = schema.errors || []
137142
let itemSchema = fn.inferSchema(schema.items)
138143

139144
const JsonSchemaForm = getComponent("JsonSchemaForm")
@@ -145,7 +150,7 @@ export class JsonSchema_array extends PureComponent {
145150
if ( enumValue ) {
146151
const Select = getComponent("Select")
147152
return (<Select className={ errors.length ? "invalid" : ""}
148-
title={ errors.length ? errors : ""}
153+
title={ errors.length ? errors : ""}
149154
multiple={ true }
150155
value={ value }
151156
allowedValues={ enumValue }
@@ -160,7 +165,7 @@ export class JsonSchema_array extends PureComponent {
160165
let schema = Object.assign({}, itemSchema)
161166
if ( errors.length ) {
162167
let err = errors.filter((err) => err.index === i)
163-
if (err.length) schema.errors = [ err[0].error + i ]
168+
if (err.length) errors = [ err[0].error + i ]
164169
}
165170
return (
166171
<div key={i} className="json-schema-form-item">
@@ -182,12 +187,13 @@ export class JsonSchema_boolean extends Component {
182187

183188
onEnumChange = (val) => this.props.onChange(val)
184189
render() {
185-
let { getComponent, value, schema } = this.props
186-
let errors = schema.errors || []
190+
let { getComponent, value, errors, schema } = this.props
191+
errors = errors.toJS ? errors.toJS() : []
192+
187193
const Select = getComponent("Select")
188194

189195
return (<Select className={ errors.length ? "invalid" : ""}
190-
title={ errors.length ? errors : ""}
196+
title={ errors.length ? errors : ""}
191197
value={ String(value) }
192198
allowedValues={ fromJS(schema.enum || ["true", "false"]) }
193199
allowEmptyValue={ !this.props.required }

src/core/plugins/spec/reducers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export default {
5252
},
5353

5454
[VALIDATE_PARAMS]: ( state, { payload: { pathMethod, isOAS3 } } ) => {
55-
let meta = state.getIn( [ "meta", "paths", ...pathMethod ] )
55+
let meta = state.getIn( [ "meta", "paths", ...pathMethod ], fromJS({}) )
5656
let isXml = /xml/i.test(meta.get("consumes_value"))
5757

5858
return state.updateIn( [ "resolved", "paths", ...pathMethod, "parameters" ], fromJS([]), parameters => {
@@ -68,7 +68,7 @@ export default {
6868
return state.updateIn( [ "resolved", "paths", ...pathMethod, "parameters" ], fromJS([]), parameters => {
6969
return parameters.withMutations( parameters => {
7070
for ( let i = 0, len = parameters.count(); i < len; i++ ) {
71-
parameters.setIn([i, "errors"], fromJS({}))
71+
parameters.setIn([i, "errors"], fromJS([]))
7272
}
7373
})
7474
})

0 commit comments

Comments
 (0)