Comparing version 0.3.0 to 0.4.0
# Changelog | ||
## 📦 [0.4.0](https://www.npmjs.com/package/v8r/v/0.4.0) - 2020-12-30 | ||
* Resolve external references in schemas | ||
## 📦 [0.3.0](https://www.npmjs.com/package/v8r/v/0.3.0) - 2020-12-29 | ||
@@ -4,0 +8,0 @@ |
{ | ||
"name": "v8r", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "A command-line JSON and YAML validator that's on your wavelength", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -96,3 +96,3 @@ # v8r | ||
💡 No. There are some with [known issues](TODO:issue-link) | ||
💡 No. There are some with [known issues](https://github.com/chris48s/v8r/issues/18) | ||
@@ -99,0 +99,0 @@ ### ❓ Can `v8r` validate against a local schema? |
"use strict"; | ||
const got = require("got"); | ||
const callCounter = {}; | ||
const callLimit = 10; | ||
@@ -18,3 +20,24 @@ function expire(cache, ttl) { | ||
function limitDepth(url) { | ||
/* | ||
It is possible to create cyclic dependencies with external references | ||
in JSON schema. Ajv doesn't detect this when resolving external references, | ||
so we keep a count of how many times we've called the same URL. | ||
If we are calling the same URL over and over we've probably hit a circular | ||
external reference and we need to break the loop. | ||
*/ | ||
if (url in callCounter) { | ||
callCounter[url]++; | ||
} else { | ||
callCounter[url] = 1; | ||
} | ||
if (callCounter[url] > callLimit) { | ||
throw new Error( | ||
`❌ Called ${url} ${callLimit} times. Possible circular reference.` | ||
); | ||
} | ||
} | ||
async function cachedFetch(url, cache, ttl) { | ||
limitDepth(url); | ||
expire(cache, ttl); | ||
@@ -21,0 +44,0 @@ const cachedResponse = cache.getKey(url); |
@@ -47,7 +47,7 @@ "use strict"; | ||
function validate(data, schema) { | ||
const ajv = new Ajv({ schemaId: "auto" }); | ||
async function validate(data, schema, resolver) { | ||
const ajv = new Ajv({ schemaId: "auto", loadSchema: resolver }); | ||
ajv.addMetaSchema(require("ajv/lib/refs/json-schema-draft-04.json")); | ||
ajv.addMetaSchema(require("ajv/lib/refs/json-schema-draft-06.json")); | ||
const validate = ajv.compile(schema); | ||
const validate = await ajv.compileAsync(schema); | ||
const valid = validate(data); | ||
@@ -94,5 +94,14 @@ if (!valid) { | ||
const schema = await cachedFetch(schemaUrl, cache, ttl); | ||
if ( | ||
"$schema" in schema && | ||
schema.$schema.includes("json-schema.org/draft/2019-09/schema") | ||
) { | ||
throw new Error(`❌ Unsupported JSON schema version ${schema.$schema}`); | ||
} | ||
console.log(`Validating ${filename} against schema from ${schemaUrl} ...`); | ||
const valid = validate(data, schema); | ||
const resolver = function (url) { | ||
return cachedFetch(url, cache, ttl); | ||
}; | ||
const valid = await validate(data, schema, resolver); | ||
if (valid) { | ||
@@ -99,0 +108,0 @@ console.log(`✅ ${filename} is valid`); |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
14150
236
0