Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format "uri" is not throwing errors #40

Closed
asafyish opened this issue Aug 28, 2014 · 2 comments
Closed

Format "uri" is not throwing errors #40

asafyish opened this issue Aug 28, 2014 · 2 comments

Comments

@asafyish
Copy link

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
Copy link
Owner

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
Copy link
Owner

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.
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants