darobin / web-schema

The meeting of JSON Schema and HTML form validation
14 stars 0 forks source link

Validation extensibility #9

Open SVasilev opened 8 years ago

SVasilev commented 8 years ago

Hello, I am trying to extend the module's functionality by adding a custom types. For example I have this schema:

{ "type": "uri" }

So your module is supposed to call an internal function with name validateUri. But since the module doesn't have it I can add it with this code:

var JsonValidator = require('web-schema').WebSchema;
var jv = new JsonValidator();

jv.validateUri = function(obj, schema, errors) {
  return this.validateString(obj, schema, errors) && myUriValidation(obj, schema, errors);
};

Can you document that this is the way of extending the module because otherwise if you change the implementation in some future versions this may not work. Or if this is not the proper way, can you help me out and figure a different solution? Thanks in advance, Best regards, Stefan

darobin commented 8 years ago

I'd be happy to look at a pull request if you wish to make support for that explicit!

darobin commented 8 years ago

Maybe adding a registration method to avoid having to assign to the object would be best.

SVasilev commented 8 years ago

Yep, that would be the solution. Unfortunately, I found that somebody has already done it. Since I am running short on time, the faster way is to try their module. There is a function addFormat, which does the trick for me. Thanks for the response. I really liked the js code that you have written for the module. It seems that you have many other interesting projects. I might take a look at them if I have more time. :)