Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

v8r

Package Overview
Dependencies
Maintainers
1
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

v8r - npm Package Compare versions

Comparing version 0.3.0 to 0.4.0

4

CHANGELOG.md
# 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 @@

2

package.json
{
"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`);

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc