Skip to content

Commit

Permalink
Add custom_validators option to set instance-specific custom validato…
Browse files Browse the repository at this point in the history
…rs. Fixes #503
  • Loading branch information
jdorn committed Sep 27, 2015
1 parent 635adc1 commit 0d7b2c8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
8 changes: 7 additions & 1 deletion src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ JSONEditor.prototype = {
// Fetch all external refs via ajax
this._loadExternalRefs(this.schema, function() {
self._getDefinitions(self.schema);
self.validator = new JSONEditor.Validator(self);

// Validator options
var validator_options = {};
if(self.options.custom_validators) {
validator_options.custom_validators = self.options.custom_validators;
}
self.validator = new JSONEditor.Validator(self,null,validator_options);

// Create the root editor
var editor_class = self.getEditorClass(self.schema);
Expand Down
8 changes: 7 additions & 1 deletion src/editors/multiple.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,12 @@ JSONEditor.defaults.editors.multiple = JSONEditor.AbstractEditor.extend({

this.editor_holder = document.createElement('div');
container.appendChild(this.editor_holder);


var validator_options = {};
if(self.jsoneditor.options.custom_validators) {
validator_options.custom_validators = self.jsoneditor.options.custom_validators;
}

this.switcher_options = this.theme.getSwitcherOptions(this.switcher);
$each(this.types,function(i,type) {
Expand All @@ -200,7 +206,7 @@ JSONEditor.defaults.editors.multiple = JSONEditor.AbstractEditor.extend({
}
}

self.validators[i] = new JSONEditor.Validator(self.jsoneditor,schema);
self.validators[i] = new JSONEditor.Validator(self.jsoneditor,schema,validator_options);
});

this.switchEditor(0);
Expand Down
12 changes: 9 additions & 3 deletions src/validator.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
JSONEditor.Validator = Class.extend({
init: function(jsoneditor,schema) {
init: function(jsoneditor,schema,options) {
this.jsoneditor = jsoneditor;
this.schema = schema || this.jsoneditor.schema;
this.options = {};
this.options = options || {};
this.translate = this.jsoneditor.translate || JSONEditor.defaults.translate;
},
validate: function(value) {
Expand Down Expand Up @@ -508,10 +508,16 @@ JSONEditor.Validator = Class.extend({
}
}

// Custom type validation
// Custom type validation (global)
$each(JSONEditor.defaults.custom_validators,function(i,validator) {
errors = errors.concat(validator.call(self,schema,value,path));
});
// Custom type validation (instance specific)
if(this.options.custom_validators) {
$each(this.options.custom_validators,function(i,validator) {
errors = errors.concat(validator.call(self,schema,value,path));
});
}

return errors;
},
Expand Down

0 comments on commit 0d7b2c8

Please sign in to comment.