GithubHelp home page GithubHelp logo

Comments (8)

epoberezkin avatar epoberezkin commented on May 13, 2024

Thanks, I will have a look

from ajv.

epoberezkin avatar epoberezkin commented on May 13, 2024

There are two issues here.

  1. The string isn't a valid resource address (e.g. http://example.com) according to JSON-schema standard, that is a bit contradictory about what a valid URI is, but the test suite they supply mandates the presence of '//' (you can check formats file). For your kind of URN/URI you can either add a custom format or redefine URI format with ajv.addFormat('uri', /some_pattern/).
  2. ajv.errorsText() without parameters will return errors as a string only if you used ajv.validate() rather than a compiled function (see examples in readme). Using compiled function is recommended as it is a bit faster. In this case you can access errors either via valid.errors (an array) or via ajv.errorsText(valid.errors) (a string).

from ajv.

epoberezkin avatar epoberezkin commented on May 13, 2024

@nichtich with the latest change from @johanlelan URIs need to have at lease a single "/" character (was two slashes). Please feel free to reopen if my suggestions above are not working for you. Thanks

from ajv.

nichtich avatar nichtich commented on May 13, 2024

Thanks for pointing to the sources and the second issue. My original bug report, however remains:

json-schema-validation spec. section 7.3.6. defines validation of the uri format as following:

A string instance is valid against this attribute if it is a valid URI, according to RFC3986

RFC 3986 lists some examples of URIs, several of them not containing a /. The specification contains a detailed grammar. Any regular expression for testing URI should allow at least this formal language. Sure ajv does not need to implemented all of its complexity but use a simplified regular expression.

from ajv.

epoberezkin avatar epoberezkin commented on May 13, 2024

👍 It would be really great if you could suggest changes to the regular expressions ajv uses (in PR or just here).

from ajv.

johanlelan avatar johanlelan commented on May 13, 2024

I found this regular expression for generics URI on Regular Expression Cookbook (https://github.com/shihyu/Regular_Expressions/blob/master/O'Reilly-Regular%20Expressions%20Cookbook(2nd%20Edition).pdf)

^([a-z][a-z0-9+\-.]*:(\/\/([a-z0-9\-._~%!$&'()*+,;=]+@)?([a-z0-9\-._~%]+|\[[a-f0-9:.]+\]|\[v[a-f0-9][a-z0-9\-._~%!$&'()*+,;=:]+\])(:[0-9]+)?(\/[a-z0-9\-._~%!$&'()*+,;=:@]+)*\/?|(\/?[a-z0-9\-._~%!$&'()*+,;=:@]+(\/[a-z0-9\-._~%!$&'()*+,;=:@]+)*\/?)?)|([a-z0-9\-._~%!$&'()*+,;=@]+(\/[a-z0-9\-._~%!$&'()*+,;=:@]+)*\/?|(\/[a-z0-9\-._~%!$&'()*+,;=:@]+)+\/?))(\?[a-z0-9\-._~%!$&'()*+,;=:@\/?]*)?(#[a-zA-Z0-9\-._~%!$&'()*+,;=:@\/?]*)?$

It validates your urn @nichtich !
It validates the relatives URLs !
And of course, classics URLs :)

There is just one improvement to think about: case sensitive URIs.
For me URI are case insensitive but in real life, it depends on web server implementation.
So if you want to manage case sensitive URIs, juste replace a-z by a-zA-Z in all occurences.

Hope that helps!

from ajv.

epoberezkin avatar epoberezkin commented on May 13, 2024

Cool, thanks!. Can be just /.../i

from ajv.

epoberezkin avatar epoberezkin commented on May 13, 2024

The problem with that regex is that it also validates URI fragments (like 'abc'), and it shouldn't... It will fail the test. I'll see how it can be changed so URI fragments don't match.

from ajv.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.