ajv-formats
Advanced tools
Comparing version 1.0.1 to 1.2.0
@@ -6,2 +6,4 @@ const formatters = []; | ||
formatters.push(require('./idn-email')); | ||
formatters.push(require('./idn-hostname')) | ||
formatters.push(require('./iri-reference')) | ||
@@ -8,0 +10,0 @@ const formats = {}; |
@@ -1,14 +0,15 @@ | ||
const { parse } = require('uri-js'); | ||
const { validate } = require('isemail'); | ||
const schemes = require('schemes'); | ||
const { parse } = require("uri-js"); | ||
const { validate } = require("isemail"); | ||
const schemes = require("schemes"); | ||
module.exports.name = 'iri' | ||
module.exports.validate = (value) => { | ||
const uri = parse(value); | ||
if (uri.scheme === 'mailto' && uri.to.every(validate)) { | ||
module.exports.name = "iri"; | ||
module.exports.validate = value => { | ||
const iri = parse(value); | ||
if (iri.scheme === "mailto" && iri.to.every(validate)) { | ||
return true; | ||
}; if (schemes.allByName[uri.scheme] && uri.reference === 'absolute') { | ||
} | ||
if (iri.reference === "absolute" && schemes.allByName[iri.scheme]) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
}; |
@@ -13,2 +13,4 @@ const assert = require("assert"); | ||
assert.ok(ajv._formats["idn-email"]); | ||
assert.ok(ajv._formats["idn-hostname"]); | ||
assert.ok(ajv._formats["iri-reference"]); | ||
}); | ||
@@ -21,2 +23,4 @@ | ||
assert.ok(ajv._formats["idn-email"]); | ||
assert.ok(ajv._formats["idn-hostname"]); | ||
assert.ok(ajv._formats["iri-reference"]); | ||
}); | ||
@@ -55,2 +59,7 @@ | ||
assert.ok(!validate("invalidScheme://example.com")); // an invalid scheme | ||
assert.ok(!validate("this:that")); | ||
// These are IRI-References not IRI | ||
assert.ok(!validate("#someelement")); | ||
assert.ok(!validate("afile.svg#anelement")); | ||
}); | ||
@@ -110,2 +119,145 @@ | ||
}); | ||
it("accept valid uuids", function() { | ||
const ajv = new Ajv(); | ||
apply(ajv); | ||
const schema = { | ||
type: "string", | ||
format: "uuid" | ||
}; | ||
const validate = ajv.compile(schema); | ||
// examples from https://www.uuidgenerator.net/version4 | ||
assert.ok(validate("90e89155-4c0d-4942-a804-a610ccb76b1b")); | ||
assert.ok(validate("55e9b1aa-cff4-43fe-8c66-bdd190720360")); | ||
// examples from https://www.uuidgenerator.net/version1 | ||
assert.ok(validate("e1a4973e-395b-11ea-a137-2e728ce88125")); | ||
}); | ||
it("reject invalid uuids", function() { | ||
const ajv = new Ajv(); | ||
apply(ajv); | ||
const schema = { | ||
type: "string", | ||
format: "uuid" | ||
}; | ||
var validate = ajv.compile(schema); | ||
assert.ok(!validate("90e89155-4c0d-4942-a804-a610ccb76b1b1")); | ||
assert.ok(!validate("55e9b1aa-cff4-43fe-8c66-bdg190720360")); | ||
}); | ||
it("accept valid international domains", function() { | ||
const ajv = new Ajv(); | ||
apply(ajv); | ||
const schema = { | ||
type: "string", | ||
format: "idn-hostname" | ||
}; | ||
const validate = ajv.compile(schema); | ||
assert.ok(validate("google.com")); | ||
// example from https://en.wikipedia.org/wiki/Internationalized_domain_name#Example_of_IDNA_encoding | ||
assert.ok(validate("ジェーピーニック.jp")); | ||
assert.ok(validate("ουτοπία.δπθ.gr")); | ||
// example from https://unicode.org/faq/idn.html#11 | ||
assert.ok(validate("öbb.at")); | ||
}); | ||
it("reject invalid international domains", function() { | ||
const ajv = new Ajv(); | ||
apply(ajv); | ||
const schema = { | ||
type: "string", | ||
format: "idn-hostname" | ||
}; | ||
var validate = ajv.compile(schema); | ||
// bad tld | ||
assert.ok(!validate("example.unknown")); | ||
// a URL, not a hostname | ||
assert.ok(!validate("http://google.com")); | ||
}); | ||
it("accept valid IRI-reference", function() { | ||
const ajv = new Ajv(); | ||
apply(ajv); | ||
const schema = { | ||
type: "string", | ||
format: "iri-reference" | ||
}; | ||
const validate = ajv.compile(schema); | ||
assert.ok(validate('https://tools.ietf.org/html/rfc3986#section-4.2')); | ||
// examples from https://dev.w3.org/SVG/profiles/1.2T/publish/diff/linking.html#IRIforms | ||
assert.ok(validate("#someelement")); | ||
assert.ok(validate("afile.svg#anelement")); | ||
assert.ok(validate("afile.svg")); | ||
assert.ok(validate("somecontainer#fragment")); | ||
// from http://homepage.divms.uiowa.edu/~rus/Courses/WebPro/uri.pdf | ||
assert.ok(validate("http://example.org/absolute/URI/with/absolute/path/to/resource.txt")); | ||
assert.ok(validate("//example.org/scheme-relative/URI/with/absolute/path/to/resource.txt")); | ||
assert.ok(validate("/relative/URI/with/absolute/path/to/resource.txt")); | ||
assert.ok(validate("relative/path/to/resource.txt")); | ||
assert.ok(validate("../../../resource.txt")); | ||
assert.ok(validate("./resource.txt#frag01")); | ||
assert.ok(validate("resource.txt")); | ||
assert.ok(validate("#frag01")); | ||
// https://tools.ietf.org/html/rfc3986#section-4.2 | ||
assert.ok(validate("//network/test")); | ||
assert.ok(validate("./this:that")); | ||
assert.ok(validate("./path")); // relative-path reference | ||
assert.ok(validate("/path")); // absolute-path reference | ||
}); | ||
it("reject invalid IRI-reference", function() { | ||
const ajv = new Ajv(); | ||
apply(ajv); | ||
const schema = { | ||
type: "string", | ||
format: "iri-reference" | ||
}; | ||
var validate = ajv.compile(schema); | ||
// https://tools.ietf.org/html/rfc3986#section-4.2 | ||
assert.ok(!validate("this:that")); | ||
}); | ||
it("draft07 should not include draft2019 formats", function() { | ||
const draft07 = require('./draft07'); | ||
assert.ok(!draft07.duration); | ||
}); | ||
it("draft07 should include the correct formats", function() { | ||
const draft07 = require('./draft07'); | ||
assert.ok(draft07["idn-hostname"]); | ||
assert.ok(draft07["idn-email"]); | ||
assert.ok(draft07["iri"]); | ||
assert.ok(draft07["iri-reference"]); | ||
}); | ||
it("add the draft2017 types to ajv as options to Ajv instances", function () { | ||
const draft07 = require('./draft07'); | ||
const ajv = new Ajv({ formats : draft07}); | ||
assert.ok(!ajv._formats.duration); | ||
assert.ok(ajv._formats.iri); | ||
assert.ok(ajv._formats["idn-email"]); | ||
assert.ok(ajv._formats["idn-hostname"]); | ||
assert.ok(ajv._formats["iri-reference"]); | ||
}); | ||
}); | ||
{ | ||
"name": "ajv-formats", | ||
"version": "1.0.1", | ||
"version": "1.2.0", | ||
"description": "Plugin for AJV that adds support for some of draft2019 formats.", | ||
@@ -15,3 +15,5 @@ "main": "index.js", | ||
"isemail": "^3.2.0", | ||
"punycode": "^2.1.1", | ||
"schemes": "^1.1.1", | ||
"tldjs": "^2.3.1", | ||
"uri-js": "^4.2.2" | ||
@@ -18,0 +20,0 @@ }, |
# ajv-formats | ||
Plugin for AJV that adds support for **some** of draft2019 formats. | ||
Plugin for AJV that adds support for additional international formats and formats | ||
added in draft2019. | ||
Currently, the `iri`, `idn-email`, and `duration` formats are supported. | ||
Currently, `iri`, `iri-reference`, `idn-email`, `idn-hostname`, and `duration` formats | ||
(added in draft 2019) are supported. | ||
@@ -33,3 +35,3 @@ ## Installation | ||
const Ajv = require('ajv'); | ||
const formats = require('ajv-draft2019-formats/formats'); | ||
const formats = require('ajv-formats/formats'); | ||
const ajv = new Ajv({formats}); | ||
@@ -44,2 +46,13 @@ | ||
## Draft07 | ||
The library also provides a `draft07` export to load only the formats relevant to | ||
draft07. | ||
``` | ||
const Ajv = require('ajv'); | ||
const formats = require('ajv-formats/draft07'); | ||
const ajv = new Ajv({formats}); | ||
``` | ||
## Formats | ||
@@ -49,5 +62,11 @@ | ||
Scheme are checked against the list of IANA schemes for a valid scheme and path only. | ||
For 'mailto' schemes, all of the `to:` addresses are validated. | ||
The string is parsed with 'uri-js' and the scheme is checked against the list of known IANA schemes. | ||
If it's a 'mailto' schemes, all of the `to:` addresses are validated, otherwise we check there IRI | ||
includes a path and is an absolute reference. | ||
### iri-reference | ||
All valid IRIs are valid. Fragments must have a valid path and of type "relative", "same-document" | ||
or "uri". If there is a scheme, it must be valid. | ||
### idn-email | ||
@@ -57,4 +76,8 @@ | ||
### idn-hostname | ||
The hostname is converted to ascii with punycode and checked for a valid tld. | ||
### duration | ||
The string is checked against a regex. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14051
12
292
80
6
+ Addedpunycode@^2.1.1
+ Addedtldjs@^2.3.1
+ Addedpunycode@1.4.1(transitive)
+ Addedtldjs@2.3.1(transitive)