tdegrunt / jsonschema

JSON Schema validation
Other
1.83k stars 263 forks source link

Async custom validation #158

Open TimoRuetten opened 8 years ago

TimoRuetten commented 8 years ago

Is there any way to add a custom attribute with async validation ?

Something like this:

Validator.attributes.asyncFunction = function () {
  var self = this;
  someAsync(function(){
    self.validate(true);
  });
};
tdegrunt commented 8 years ago

Not that I'm aware of ... what would be the use-case?

TimoRuetten commented 8 years ago

Sorry if you read the first comment - I made a mistake by writing this @tdegrunt . I am using jsonschema for writing a package for form validation. In this package (its designed for meteorjs) I want to make it possible defining a custom function which is also async but will return a valid jsonschema error for better error handling. Its possible for me to do a "work around" but it would be more awesome if there is still some functionality - if not I will add a work around for my case.

awwright commented 8 years ago

The primary purpose of JSON Schema is to provide structural validation, only requiring simple computation to verify. This necessarily means no blocking or asynchronous calls.

Of course, JSON Schema can do other stuff, too, like extracting link relationships from an instance. The only thing is this would be outside the scope of the validate() function of most implementations. But there should be some mechanism so so that a JSON Schema keyword can pass back a value about the instance -- including a deferred value like a Promise, that your application could wait on.

Maybe more like this:

Validator.attributes.someKeyword = function (instance, schema) {
  var self = this;
  return self.instanceData('verifyReference', function(db, cb){
    db.get(instance.id, function(err, record){ cb(null, !!record); });
  });
};

So here, the keyword isn't blocking at all, but it's passing back a value that annotates something about the instance. In this case, an asynchronous function call that accepts a database connection to make the lookup with.

xfg commented 8 years ago

+1 how to do it?