natesilva / jayschema

[Unmaintained] - A comprehensive JSON Schema validator for Node.js
BSD 3-Clause "New" or "Revised" License
217 stars 22 forks source link

Format "uri" is not throwing errors #40

Closed asafyish closed 10 years ago

asafyish commented 10 years ago

This should throw an error, but it doesn't:

var JaySchema = require('jayschema');
var js = new JaySchema();
var instance ={ "url": "invalid"};
var schema = {
    "type": "object",
    "properties": {
        "url": {
            "type": "string",
            "format": "uri"
        }
    }
}

js.validate(instance, schema, function(errs) {
    if (errs) {
        console.error(errs);
    }
    else {
        console.log('async validation OK!');
    }
});
natesilva commented 10 years ago

The uri format in JaySchema is very generous in what it allows. In your example, invalid is a valid relative URI. (Imagine an HTML tag, <a href="pagename">, which is valid.)

This should probably be fixed, so the uri format expects an absolute URI – which I think is what you’re looking for. But it needs testing to ensure it doesn’t break stuff. In particular, in the Core/Validation Meta-Schema, the id property is specified to use the uri format. That needs special handling, for example, with this example in the JSON Schema documentation.

I don’t have time to study this right now, but I will keep the issue open. And of course, if someone else wants to do it, pull requests are welcome.

natesilva commented 10 years ago

Hi @asafyish. As of version 0.3.0, JaySchema can do this for you using a custom format validator. If the built-in uri format isn’t to your liking you can now override it:

var js = new JaySchema();

// This overrides the built-in `uri` format; you could also use your own
// format name (like `my-uri`).
js.addFormat('uri', function(value) {
  // Do your own URI validation here, and return null if the value is valid,
  // or, if the value is not valid, return a string describing the problem.
});