acornejo/jjv

uri format doesn't handle hostnames without a dot

Closed this issue · 4 comments

Example

var env = require('jjv')();

var schema = { type: 'string', format: 'uri' };

console.log(env.validate(schema, 'http://localhost/'));
console.log(env.validate(schema, 'http://localhost:8000/'));
console.log(env.validate(schema, 'http://example.org:8000/'));

Output

{ validation: { format: true } }
{ validation: { format: true } }
null

Looks like it's suppose to conform to rfc3986, which would be pretty difficult to do via regex.

I'm just going to add a custom format which ignores this check and thus fixes my problem.

@silas, are you sure the localhost example is supposed to be a valid URI?

You can probably use the 127.0.0.1 address to achieve the intended effect.

As far as I understand the localhost name translation is controlled by the /etc/hosts file, and therefore it cannot be assumed that it will be available in all devices. For example, android phones do not translate localhost to the loopback address.

You can also edit the hosts file to disable the resolution to localhost or rename it to localcomputer or anything else you want. So what does this mean for URI's? Should we accept any string as URI because you can add it to a hosts file?

If you feel localhost should be an accepted URI, open up a ticket on the json-shema specification. Specifically see here https://github.com/json-schema/JSON-Schema-Test-Suite

I will gladly modify JJV to pass any tests required by the JSON-schema-test-suite, accepting localhost as a valid URI is currently not one of them.

PS: Let me add that I believe that in "good" operating systems and computers it is reasonable to expect that localhost is translated to the loopback address, and I feel it is bad practice not to do so, probably even breaking some existing standards. However, given that json-schema is for the most part used to validate user input, I am not sure that accepting almost any string as a URI would be very useful to JJV users.

Like I said, I don't really care because I don't expect jjv to implement the URI grammar.

That said, according to the JSON Schema spec a "string instance is valid against this attribute if it is a valid URI, according to [RFC3986]." ref.

If we look at RFC3986 we'll see that localhost is certainly valid (even explicitly mentioned in a couple of places), as is localcomputer and any other single label domain. It even goes on to mention the specification "does not mandate a particular registered name lookup technology and therefore does not restrict the syntax of reg-name beyond what is necessary for interoperability."

Anyway, it's easy for people to customize and that's enough for me!

Thanks again for making JJV, it's been super useful.

Thanks Silas, I had missed the reference to RFC3986 in the json schema specification.

I see if I can make some adjustments to accommodate it without making the code too slow/complicated. As you said, its easy enough to add custom formats (or even to replace formatting), but I would like to support the full json-schema spec out of the box.