Skip to content

Commit ff65bb3

Browse files
committed
Merge remote-tracking branch 'origin/develop' into develop
2 parents 3f3b8b0 + a2213da commit ff65bb3

File tree

4 files changed

+63
-34
lines changed

4 files changed

+63
-34
lines changed

src/js/textmode.js

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ textmode.create = function (container, options = {}) {
9797
this.lastSchemaErrors = undefined
9898

9999
// create a debounced validate function
100-
this._debouncedValidate = debounce(this.validate.bind(this), this.DEBOUNCE_INTERVAL)
100+
this._debouncedValidate = debounce(this._validateAndCatch.bind(this), this.DEBOUNCE_INTERVAL)
101101

102102
this.width = container.clientWidth
103103
this.height = container.clientHeight
@@ -324,7 +324,7 @@ textmode.create = function (container, options = {}) {
324324
this.errorTable = new ErrorTable({
325325
errorTableVisible: this.mode === 'text',
326326
onToggleVisibility: function () {
327-
me.validate()
327+
me._validateAndCatch()
328328
},
329329
onFocusLine: function (line) {
330330
me.isFocused = true
@@ -863,22 +863,22 @@ textmode.validate = function () {
863863
this.validationSequence = (this.validationSequence || 0) + 1
864864
const me = this
865865
const seq = this.validationSequence
866-
validateCustom(json, this.options.onValidate)
866+
return validateCustom(json, this.options.onValidate)
867867
.then(customValidationErrors => {
868868
// only apply when there was no other validation started whilst resolving async results
869869
if (seq === me.validationSequence) {
870870
const errors = schemaErrors.concat(parseErrors).concat(customValidationErrors)
871871
me._renderErrors(errors)
872-
if (typeof this.options.onValidationError === 'function') {
873-
if (isValidationErrorChanged(errors, this.lastSchemaErrors)) {
874-
this.options.onValidationError.call(this, errors)
875-
}
876-
this.lastSchemaErrors = errors
872+
if (
873+
typeof this.options.onValidationError === 'function' &&
874+
isValidationErrorChanged(errors, this.lastSchemaErrors)
875+
) {
876+
this.options.onValidationError.call(this, errors)
877877
}
878+
this.lastSchemaErrors = errors
878879
}
879-
})
880-
.catch(err => {
881-
console.error('Custom validation function did throw an error', err)
880+
881+
return this.lastSchemaErrors
882882
})
883883
} catch (err) {
884884
if (this.getText()) {
@@ -897,15 +897,24 @@ textmode.validate = function () {
897897

898898
this._renderErrors(parseErrors)
899899

900-
if (typeof this.options.onValidationError === 'function') {
901-
if (isValidationErrorChanged(parseErrors, this.lastSchemaErrors)) {
902-
this.options.onValidationError.call(this, parseErrors)
903-
}
904-
this.lastSchemaErrors = parseErrors
900+
if (
901+
typeof this.options.onValidationError === 'function' &&
902+
isValidationErrorChanged(parseErrors, this.lastSchemaErrors)
903+
) {
904+
this.options.onValidationError.call(this, parseErrors)
905905
}
906+
this.lastSchemaErrors = parseErrors
907+
908+
return Promise.resolve(this.lastSchemaErrors)
906909
}
907910
}
908911

912+
textmode._validateAndCatch = function () {
913+
this.validate().catch(err => {
914+
console.error('Error running validation:', err)
915+
})
916+
}
917+
909918
textmode._renderErrors = function (errors) {
910919
const jsonText = this.getText()
911920
const errorPaths = []

src/js/treemode.js

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ treemode._setOptions = function (options) {
182182
this.setSchema(this.options.schema, this.options.schemaRefs)
183183

184184
// create a debounced validate function
185-
this._debouncedValidate = debounce(this.validate.bind(this), this.DEBOUNCE_INTERVAL)
185+
this._debouncedValidate = debounce(this._validateAndCatch.bind(this), this.DEBOUNCE_INTERVAL)
186186

187187
if (options.onSelectionChange) {
188188
this.onSelectionChange(options.onSelectionChange)
@@ -214,7 +214,7 @@ treemode.set = function (json) {
214214
this._setRoot(node)
215215

216216
// validate JSON schema (if configured)
217-
this.validate()
217+
this._validateAndCatch()
218218

219219
// expand
220220
const recurse = false
@@ -254,7 +254,7 @@ treemode.update = function (json) {
254254
this.onChangeDisabled = false
255255

256256
// validate JSON schema
257-
this.validate()
257+
this._validateAndCatch()
258258

259259
// update search result if any
260260
if (this.searchBox && !this.searchBox.isEmpty()) {
@@ -588,28 +588,35 @@ treemode.validate = function () {
588588
this.validationSequence++
589589
const me = this
590590
const seq = this.validationSequence
591-
this._validateCustom(json)
591+
return this._validateCustom(json)
592592
.then(customValidationErrors => {
593593
// only apply when there was no other validation started whilst resolving async results
594594
if (seq === me.validationSequence) {
595595
const errorNodes = [].concat(schemaErrors, customValidationErrors || [])
596596
me._renderValidationErrors(errorNodes)
597-
if (typeof this.options.onValidationError === 'function') {
598-
if (isValidationErrorChanged(errorNodes, this.lastSchemaErrors)) {
599-
this.options.onValidationError.call(this, errorNodes)
600-
}
601-
this.lastSchemaErrors = errorNodes
597+
if (
598+
typeof this.options.onValidationError === 'function' &&
599+
isValidationErrorChanged(errorNodes, this.lastSchemaErrors)
600+
) {
601+
this.options.onValidationError.call(this, errorNodes)
602602
}
603+
604+
this.lastSchemaErrors = errorNodes
603605
}
604-
})
605-
.catch(err => {
606-
console.error(err)
606+
607+
return this.lastSchemaErrors
607608
})
608609
} catch (err) {
609-
console.error(err)
610+
return Promise.reject(err)
610611
}
611612
}
612613

614+
treemode._validateAndCatch = function () {
615+
this.validate().catch(err => {
616+
console.error('Error running validation:', err)
617+
})
618+
}
619+
613620
treemode._renderValidationErrors = function (errorNodes) {
614621
// clear all current errors
615622
if (this.errorNodes) {

src/scss/jsoneditor/_editor.scss

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ div {
208208
margin: 0 4px 0 0;
209209
background-image: $jse-icons-url;
210210
background-position: -168px -48px;
211+
background-color: transparent;
211212
}
212213
}
213214
}
@@ -536,11 +537,13 @@ pre.jsoneditor-preview,
536537
.jsoneditor-schema-error {
537538
background-image: $jse-icons-url;
538539
background-position: -168px -48px;
540+
background-color: transparent;
539541
}
540542
&.parse-error {
541543
.jsoneditor-schema-error {
542544
background-image: $jse-icons-url;
543545
background-position: -25px 0px;
546+
background-color: transparent;
544547
}
545548
}
546549
}

test/test_schema.html

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,29 @@
7979
};
8080

8181
var options = {
82-
mode: 'tree',
82+
mode: 'code',
8383
modes: ['code', 'form', 'text', 'tree', 'view', 'preview'], // allowed modes
84+
schema: schema,
85+
schemaRefs: {"hobbies.json": hobbiesSchema},
8486
onError: function (err) {
85-
console.error(err);
87+
console.error('ERROR', err);
8688
},
87-
schema: schema,
88-
schemaRefs: {"hobbies.json": hobbiesSchema}
89+
onChange: async () => {
90+
const errors = await editor.validate()
91+
if (errors.length === 0) {
92+
console.log('validation errors: NONE')
93+
// do something, like persisting the JSON
94+
} else {
95+
// show error to the user or something
96+
console.log('validation errors', errors)
97+
}
98+
}
8999
};
90100

91101
var json = {
92102
"firstName": "Jos",
93103
"lastName": "de Jong",
94-
gender: null,
104+
"gender": null,
95105
"age": 34.2,
96106
"hobbies": [
97107
"programming",

0 commit comments

Comments
 (0)