ajv-formats
Advanced tools
Comparing version 1.3.2 to 1.4.0
15
idn.js
@@ -1,11 +0,4 @@ | ||
const draft2019formats = require('./formats'); | ||
const idnFormat = {}; | ||
const idnFormatKeywords = ['idn-email', 'iri', 'iri-reference', 'idn-hostname']; | ||
idnFormatKeywords.forEach(keyword => { | ||
idnFormat[keyword] = draft2019formats[keyword]; | ||
}); | ||
module.exports = idnFormat; | ||
module.exports["idn-email"] = require('./formats/idn-email'); | ||
module.exports["iri"] = require('./formats/iri'); | ||
module.exports["iri-reference"] = require('./formats/iri-reference'); | ||
module.exports["idn-hostname"] = require('./formats/idn-hostname'); |
21
index.js
const formats = require('./formats'); | ||
module.exports = ajv => { | ||
Object.keys(formats).forEach(key => { | ||
ajv.addFormat(key, formats[key]); | ||
}); | ||
module.exports = (ajv, options = {}) => { | ||
const allFormats = Object.keys(formats); | ||
let formatsToInstall = allFormats; | ||
if (options.formats) { | ||
if (!Array.isArray(options.formats)) | ||
throw new Error('options.formats must be an array'); | ||
formatsToInstall = options.formats; | ||
} | ||
allFormats | ||
.filter(format => formatsToInstall.includes(format)) | ||
.forEach(key => { | ||
ajv.addFormat(key, formats[key]); | ||
}); | ||
return ajv; | ||
}; |
@@ -277,2 +277,12 @@ const assert = require('assert'); | ||
}); | ||
it('it should be possible to specify formats to install', function() { | ||
const ajv = new Ajv(); | ||
apply(ajv, { formats: ['idn-email', 'iri'] }); | ||
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.3.2", | ||
"version": "1.4.0", | ||
"description": "Plugin for AJV that adds support for some of draft2019 formats.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -33,3 +33,3 @@ # ajv-formats | ||
const ajv = new Ajv(); | ||
apply(ajv); | ||
apply(ajv); // returns ajv instance, allowing chaining | ||
@@ -43,2 +43,14 @@ let schema = { | ||
The `apply` function also accepts a second optional parameter to specify which | ||
formats to add to the `ajv` instance. | ||
```js | ||
const Ajv = require('ajv'); | ||
const apply = require('ajv-formats'); | ||
const ajv = new Ajv(); | ||
// Install only the idn-email and iri formats | ||
apply(ajv, { formats: ['idn-email', 'iri'] }); | ||
``` | ||
The module also provides an alternate entrypoint `ajv-formats/formats` that | ||
@@ -61,3 +73,4 @@ works with the `ajv` constructor to add the formats to new instances. | ||
Note the approach below only works for formats that don't contain a hypen `-` in | ||
the name. | ||
the name. This approach may yield smaller packed bundles since it allows | ||
tree-shaking to remove unwanted validators and related dependencies. | ||
@@ -97,3 +110,3 @@ ```js | ||
Basically, any URL-safe string is a valid IRI syntactically. I struggled to find | ||
[negative test cases](https://github.com/luzlab/ajv-formats/blob/master/index.test.js#L232) | ||
[negative test cases](https://github.com/luzlab/ajv-formats/blob/master/index.test.js#L240) | ||
when writing the unit tests for IRI-references. Consider: | ||
@@ -100,0 +113,0 @@ |
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
17140
327
128