Some refactoring

This commit is contained in:
jos 2016-11-12 10:58:06 +01:00
parent 2bc4cf7dfe
commit 6120949acf
2 changed files with 28 additions and 13 deletions

View File

@ -87,14 +87,14 @@ export default class TextMode extends Component {
/** @protected */ /** @protected */
renderSchemaErrors () { renderSchemaErrors () {
// TODO: move the JSON Schema stuff into a separate Component? // TODO: move the JSON Schema stuff into a separate Component?
if (!this.state.compiledSchema) {
return null
}
try { try {
// TODO: only validate again when json is changed since last validation // TODO: only validate again when json is changed since last validation
const json = this.get(); // this can fail when there is no valid json const json = this.get(); // this can fail when there is no valid json
const valid = this.state.compiledSchema(json) const valid = this.state.compiledSchema
? this.state.compiledSchema(json)
: true
if (!valid) { if (!valid) {
const allErrors = this.state.compiledSchema.errors.map(enrichSchemaError) const allErrors = this.state.compiledSchema.errors.map(enrichSchemaError)
const limitedErrors = limitErrors(allErrors) const limitedErrors = limitErrors(allErrors)
@ -103,34 +103,44 @@ export default class TextMode extends Component {
h('tbody', {}, limitedErrors.map(TextMode.renderSchemaError)) h('tbody', {}, limitedErrors.map(TextMode.renderSchemaError))
) )
} }
else {
return null
}
} }
catch (err) { catch (err) {
// no valid JSON, don't validate // no valid JSON
// TODO: display errors in text mode somehow? shouldn't be too much in your face
// maybe a warning icon top right?
// return h('table', {class: 'jsoneditor-text-errors'},
// h('tbody', {}, TextMode.renderSchemaError(err))
// )
return null return null
} }
} }
/** /**
* Render a table row of a single JSON schema error * Render a table row of a single JSON schema error
* @param {Error | string} error * @param {Error | Object | string} error
* @return {JSX.Element} * @return {JSX.Element}
*/ */
static renderSchemaError (error) { static renderSchemaError (error) {
const icon = h('input', {type: 'button', class: 'jsoneditor-schema-error'}) const icon = h('input', {type: 'button', class: 'jsoneditor-schema-error'})
if (typeof error === 'string') { if (error && error.data && error.schema) {
return h('tr', {}, // this is an ajv error message
h('td', {}, icon),
h('td', {colSpan: 2}, h('pre', {}, error))
)
}
else {
return h('tr', {}, [ return h('tr', {}, [
h('td', {}, icon), h('td', {}, icon),
h('td', {}, error.dataPath), h('td', {}, error.dataPath),
h('td', {}, error.message) h('td', {}, error.message)
]) ])
} }
else {
// any other error message
return h('tr', {},
h('td', {}, icon),
h('td', {colSpan: 2}, h('code', {}, String(error)))
)
}
} }
/** /**

View File

@ -590,6 +590,11 @@ div.jsoneditor-code {
vertical-align: middle; vertical-align: middle;
} }
code {
display: block;
white-space: pre-wrap;
}
.jsoneditor-schema-error { .jsoneditor-schema-error {
user-select: none; user-select: none;
outline: none; outline: none;