Comparing version 0.4.0 to 0.5.0
# Changelog | ||
## 📦 [0.5.0](https://www.npmjs.com/package/v8r/v/0.5.0) - 2021-01-13 | ||
* Allow validation against a local schema | ||
* Move cache file to OS temp dir | ||
## 📦 [0.4.0](https://www.npmjs.com/package/v8r/v/0.4.0) - 2020-12-30 | ||
@@ -4,0 +9,0 @@ |
{ | ||
"name": "v8r", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"description": "A command-line JSON and YAML validator that's on your wavelength", | ||
"scripts": { | ||
"test": "nyc --reporter=text mocha \"src/**/*.spec.js\"", | ||
"test": "V8R_CACHE_NAME=v8r-test nyc --reporter=text mocha \"src/**/*.spec.js\"", | ||
"lint": "eslint \"src/**/*.js\"", | ||
"coverage": "nyc report --reporter=text-lcov > coverage.lcov", | ||
"prettier": "prettier --write \"**/*.js\"", | ||
"prettier:check": "prettier --check \"**/*.js\"" | ||
"prettier:check": "prettier --check \"**/*.js\"", | ||
"v8r": "src/index.js" | ||
}, | ||
@@ -27,3 +28,4 @@ "bin": { | ||
"got": "^11.8.0", | ||
"js-yaml": "^3.14.0", | ||
"is-url": "^1.2.4", | ||
"js-yaml": "^4.0.0", | ||
"minimatch": "^3.0.4", | ||
@@ -30,0 +32,0 @@ "yargs": "^16.1.0" |
@@ -100,2 +100,2 @@ # v8r | ||
💡 Nope. There are other better tools for this. If you want to validate against a local schema, I recommend [ajv-cli](https://github.com/ajv-validator/ajv-cli/). | ||
💡 Yes. The `--schema` flag can be either a path to a local file or a URL. |
@@ -6,9 +6,15 @@ "use strict"; | ||
const fs = require("fs"); | ||
const isUrl = require("is-url"); | ||
const minimatch = require("minimatch"); | ||
const os = require("os"); | ||
const path = require("path"); | ||
const yaml = require("js-yaml"); | ||
const yargs = require("yargs/yargs"); | ||
const { hideBin } = require("yargs/helpers"); | ||
const { cachedFetch } = require("./cache.js"); | ||
const logging = require("./logging.js"); | ||
const SCHEMASTORE_CATALOG_URL = | ||
"https://www.schemastore.org/api/json/catalog.json"; | ||
const CACHE_DIR = path.join(os.tmpdir(), "flat-cache"); | ||
@@ -70,3 +76,3 @@ async function getSchemaUrlForFilename(filename, cache, ttl) { | ||
case ".yaml": | ||
return yaml.safeLoad(contents); | ||
return yaml.load(contents); | ||
default: | ||
@@ -81,5 +87,12 @@ throw new Error(`❌ Unsupported format ${format}`); | ||
function Cli(settings) { | ||
const cache = settings.cache || flatCache.load("v8r"); | ||
function getCache() { | ||
if (process.env.V8R_CACHE_NAME) { | ||
return flatCache.load(process.env.V8R_CACHE_NAME); | ||
} | ||
return flatCache.load("v8r", CACHE_DIR); | ||
} | ||
function Validator() { | ||
const cache = getCache(); | ||
return async function (args) { | ||
@@ -93,5 +106,7 @@ const filename = args.filename; | ||
); | ||
const schemaUrl = | ||
const schemaLocation = | ||
args.schema || (await getSchemaUrlForFilename(filename, cache, ttl)); | ||
const schema = await cachedFetch(schemaUrl, cache, ttl); | ||
const schema = isUrl(schemaLocation) | ||
? await cachedFetch(schemaLocation, cache, ttl) | ||
: JSON.parse(fs.readFileSync(schemaLocation, "utf8").toString()); | ||
if ( | ||
@@ -103,3 +118,5 @@ "$schema" in schema && | ||
} | ||
console.log(`Validating ${filename} against schema from ${schemaUrl} ...`); | ||
console.log( | ||
`Validating ${filename} against schema from ${schemaLocation} ...` | ||
); | ||
@@ -119,2 +136,57 @@ const resolver = function (url) { | ||
module.exports = { Cli }; | ||
async function cli(args) { | ||
logging.init(args.verbose); | ||
try { | ||
const validate = new Validator(); | ||
const valid = await validate(args); | ||
if (valid) { | ||
return 0; | ||
} | ||
return 99; | ||
} catch (e) { | ||
console.error(e.message); | ||
if (args.ignoreErrors) { | ||
return 0; | ||
} | ||
return 1; | ||
} finally { | ||
logging.cleanup(); | ||
} | ||
} | ||
function parseArgs(argv) { | ||
return yargs(hideBin(argv)) | ||
.command( | ||
"$0 <filename>", | ||
"Validate a local json/yaml file against a schema", | ||
(yargs) => { | ||
yargs.positional("filename", { describe: "Local file to validate" }); | ||
} | ||
) | ||
.option("verbose", { | ||
alias: "v", | ||
type: "boolean", | ||
description: "Run with verbose logging. Can be stacked e.g: -vv -vvv", | ||
}) | ||
.count("verbose") | ||
.option("schema", { | ||
alias: "s", | ||
type: "string", | ||
describe: | ||
"Local path or URL of schema to validate file against. If not supplied, we will attempt to find an appropriate schema on schemastore.org using the filename", | ||
}) | ||
.option("ignore-errors", { | ||
type: "boolean", | ||
default: false, | ||
describe: | ||
"Exit with code 0 even if an error was encountered. Passing this flag means a non-zero exit code is only issued if validation could be completed successfully and the file was invalid", | ||
}) | ||
.option("cache-ttl", { | ||
type: "number", | ||
default: 600, | ||
describe: | ||
"Remove cached HTTP responses older than <cache-ttl> seconds old. Passing 0 clears and disables cache completely", | ||
}).argv; | ||
} | ||
module.exports = { cli, parseArgs }; |
@@ -5,58 +5,7 @@ #!/usr/bin/env node | ||
const yargs = require("yargs/yargs"); | ||
const { hideBin } = require("yargs/helpers"); | ||
const { Cli } = require("./cli.js"); | ||
const logging = require("./logging.js"); | ||
const { cli, parseArgs } = require("./cli.js"); | ||
const args = yargs(hideBin(process.argv)) | ||
.command( | ||
"$0 <filename>", | ||
"Validate a local json/yaml file against a schema", | ||
(yargs) => { | ||
yargs.positional("filename", { describe: "Local file to validate" }); | ||
} | ||
) | ||
.option("verbose", { | ||
alias: "v", | ||
type: "boolean", | ||
description: "Run with verbose logging. Can be stacked e.g: -vv -vvv", | ||
}) | ||
.count("verbose") | ||
.option("schema", { | ||
alias: "s", | ||
type: "string", | ||
describe: | ||
"URL of schema to validate file against. If not supplied, we will attempt to find an appropriate schema on schemastore.org using the filename", | ||
}) | ||
.option("ignore-errors", { | ||
type: "boolean", | ||
default: false, | ||
describe: | ||
"Exit with code 0 even if an error was encountered. Passing this flag means a non-zero exit code is only issued if validation could be completed successfully and the file was invalid", | ||
}) | ||
.option("cache-ttl", { | ||
type: "number", | ||
default: 600, | ||
describe: | ||
"Remove cached HTTP responses older than <cache-ttl> seconds old. Passing 0 clears and disables cache completely", | ||
}).argv; | ||
(async () => { | ||
logging.init(args.verbose); | ||
try { | ||
const cli = new Cli({}); | ||
const valid = await cli(args); | ||
if (valid) { | ||
process.exit(0); | ||
} | ||
process.exit(99); | ||
} catch (e) { | ||
console.error(e.message); | ||
if (args.ignoreErrors) { | ||
process.exit(0); | ||
} | ||
process.exit(1); | ||
} finally { | ||
logging.cleanup(); | ||
} | ||
const exitCode = await cli(parseArgs(process.argv)); | ||
process.exit(exitCode); | ||
})(); |
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
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
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
14869
255
7
3
+ Addedis-url@^1.2.4
+ Addedargparse@2.0.1(transitive)
+ Addedis-url@1.2.4(transitive)
+ Addedjs-yaml@4.1.0(transitive)
- Removedargparse@1.0.10(transitive)
- Removedesprima@4.0.1(transitive)
- Removedjs-yaml@3.14.1(transitive)
- Removedsprintf-js@1.0.3(transitive)
Updatedjs-yaml@^4.0.0