@@ -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
@@ -346,7 +346,7 @@ textmode.create = function (container, options = {}) {
346346 this . errorTable = new ErrorTable ( {
347347 errorTableVisible : this . mode === 'text' ,
348348 onToggleVisibility : function ( ) {
349- me . validate ( )
349+ me . _validateAndCatch ( )
350350 } ,
351351 onFocusLine : function ( line ) {
352352 me . isFocused = true
@@ -434,7 +434,11 @@ textmode._onChange = function () {
434434 }
435435
436436 // enable/disable undo/redo buttons
437- setTimeout ( ( ) => this . _updateHistoryButtons ( ) )
437+ setTimeout ( ( ) => {
438+ if ( this . _updateHistoryButtons ) {
439+ this . _updateHistoryButtons ( )
440+ }
441+ } )
438442
439443 // validate JSON schema (if configured)
440444 this . _debouncedValidate ( )
@@ -822,7 +826,11 @@ textmode._setText = function (jsonText, clearHistory) {
822826 } )
823827 }
824828
825- setTimeout ( ( ) => this . _updateHistoryButtons ( ) )
829+ setTimeout ( ( ) => {
830+ if ( this . _updateHistoryButtons ) {
831+ this . _updateHistoryButtons ( )
832+ }
833+ } )
826834 }
827835
828836 // validate JSON schema
@@ -877,22 +885,22 @@ textmode.validate = function () {
877885 this . validationSequence = ( this . validationSequence || 0 ) + 1
878886 const me = this
879887 const seq = this . validationSequence
880- validateCustom ( json , this . options . onValidate )
888+ return validateCustom ( json , this . options . onValidate )
881889 . then ( customValidationErrors => {
882890 // only apply when there was no other validation started whilst resolving async results
883891 if ( seq === me . validationSequence ) {
884892 const errors = schemaErrors . concat ( parseErrors ) . concat ( customValidationErrors )
885893 me . _renderErrors ( errors )
886- if ( typeof this . options . onValidationError === 'function' ) {
887- if ( isValidationErrorChanged ( errors , this . lastSchemaErrors ) ) {
888- this . options . onValidationError . call ( this , errors )
889- }
890- this . lastSchemaErrors = errors
894+ if (
895+ typeof this . options . onValidationError === 'function' &&
896+ isValidationErrorChanged ( errors , this . lastSchemaErrors )
897+ ) {
898+ this . options . onValidationError . call ( this , errors )
891899 }
900+ this . lastSchemaErrors = errors
892901 }
893- } )
894- . catch ( err => {
895- console . error ( 'Custom validation function did throw an error' , err )
902+
903+ return this . lastSchemaErrors
896904 } )
897905 } catch ( err ) {
898906 if ( this . getText ( ) ) {
@@ -911,15 +919,24 @@ textmode.validate = function () {
911919
912920 this . _renderErrors ( parseErrors )
913921
914- if ( typeof this . options . onValidationError === 'function' ) {
915- if ( isValidationErrorChanged ( parseErrors , this . lastSchemaErrors ) ) {
916- this . options . onValidationError . call ( this , parseErrors )
917- }
918- this . lastSchemaErrors = parseErrors
922+ if (
923+ typeof this . options . onValidationError === 'function' &&
924+ isValidationErrorChanged ( parseErrors , this . lastSchemaErrors )
925+ ) {
926+ this . options . onValidationError . call ( this , parseErrors )
919927 }
928+ this . lastSchemaErrors = parseErrors
929+
930+ return Promise . resolve ( this . lastSchemaErrors )
920931 }
921932}
922933
934+ textmode . _validateAndCatch = function ( ) {
935+ this . validate ( ) . catch ( err => {
936+ console . error ( 'Error running validation:' , err )
937+ } )
938+ }
939+
923940textmode . _renderErrors = function ( errors ) {
924941 const jsonText = this . getText ( )
925942 const errorPaths = [ ]
0 commit comments