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

schema-watcher

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

schema-watcher - npm Package Compare versions

Comparing version 1.0.2 to 1.0.3

yarn-error.log

30

args.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.args = void 0;
var argparse_1 = require("argparse");
var path_1 = require("path");
var parser = new argparse_1.ArgumentParser({
version: "0.0.1",
addHelp: true,
description: "Watch and validate JSON / YAML files"
// version: "0.0.1",
// addHelp: true,
description: "Watch and validate JSON / YAML files",
});
parser.addArgument(["-f", "--file"], {
parser.add_argument("-f", "--file", {
help: "The data file to watch and validate",
required: true
required: true,
});
parser.addArgument(["-s", "--schema"], {
parser.add_argument("-s", "--schema", {
help: "Path to a schema file",
required: true
required: true,
});
parser.addArgument(["-q", "--query"], {
help: "JMESpath query to the schema if nested within a larger document"
parser.add_argument("-q", "--query", {
help: "JMESpath query to the schema if nested within a larger document",
});
parser.addArgument(["-d"], {
action: "storeTrue",
parser.add_argument("-d", {
action: "store_true",
help: "De-reference the Schema object (i.e. expand the '$ref' attributes).\n This may be required when the schema is nested within a larger\n document, but may increase memory usage and slow down validation.",
metavar: "dereference"
});
exports.args = parser.parseArgs();
exports.args.file = path_1.resolve(path_1.join(process.cwd(), exports.args.file));
exports.args.schema = path_1.resolve(path_1.join(process.cwd(), exports.args.schema));
exports.args = parser.parse_args();
exports.args.file = (0, path_1.resolve)(exports.args.file); // join(process.cwd(), args.file)
exports.args.schema = (0, path_1.resolve)(exports.args.schema); // join(process.cwd(), args.schema)
exports.default = exports.args;

@@ -5,40 +5,39 @@ import { ArgumentParser } from "argparse";

const parser = new ArgumentParser({
version: "0.0.1",
addHelp: true,
description: "Watch and validate JSON / YAML files"
// version: "0.0.1",
// addHelp: true,
description: "Watch and validate JSON / YAML files",
});
parser.addArgument(["-f", "--file"], {
help: "The data file to watch and validate",
required: true
parser.add_argument("-f", "--file", {
help: "The data file to watch and validate",
required: true,
});
parser.addArgument(["-s", "--schema"], {
help: "Path to a schema file",
required: true
parser.add_argument("-s", "--schema", {
help: "Path to a schema file",
required: true,
});
parser.addArgument(["-q", "--query"], {
help: "JMESpath query to the schema if nested within a larger document"
parser.add_argument("-q", "--query", {
help: "JMESpath query to the schema if nested within a larger document",
});
parser.addArgument(["-d"], {
action: "storeTrue",
help: `De-reference the Schema object (i.e. expand the '$ref' attributes).
parser.add_argument("-d", {
action: "store_true",
help: `De-reference the Schema object (i.e. expand the '$ref' attributes).
This may be required when the schema is nested within a larger
document, but may increase memory usage and slow down validation.`,
metavar: "dereference"
});
interface Args {
file: string;
schema: string;
query?: string;
d?: boolean;
file: string;
schema: string;
query?: string;
d?: boolean;
}
export const args: Args = parser.parseArgs();
args.file = resolve(join(process.cwd(), args.file));
args.schema = resolve(join(process.cwd(), args.schema));
export const args: Args = parser.parse_args();
args.file = resolve(args.file); // join(process.cwd(), args.file)
args.schema = resolve(args.schema); // join(process.cwd(), args.schema)
export default args;

@@ -7,10 +7,16 @@ "use strict";

var args_1 = __importDefault(require("./args"));
var json_schema_ref_parser_1 = __importDefault(require("json-schema-ref-parser"));
var json_schema_ref_parser_1 = __importDefault(require("@apidevtools/json-schema-ref-parser"));
var chokidar_1 = __importDefault(require("chokidar"));
var chalk_1 = __importDefault(require("chalk"));
var ajv_1 = __importDefault(require("ajv"));
var ajv_formats_1 = __importDefault(require("ajv-formats"));
var ajv_keywords_1 = __importDefault(require("ajv-keywords"));
var fs_1 = require("fs");
var jmespath_1 = __importDefault(require("jmespath"));
var js_yaml_1 = __importDefault(require("js-yaml"));
var AJV = new ajv_1.default({ allErrors: true });
json_schema_ref_parser_1.default.dereference = json_schema_ref_parser_1.default.dereference.bind(json_schema_ref_parser_1.default);
json_schema_ref_parser_1.default.resolve = json_schema_ref_parser_1.default.resolve.bind(json_schema_ref_parser_1.default);
var AJV = new ajv_1.default({ allErrors: true, $data: true });
(0, ajv_formats_1.default)(AJV);
(0, ajv_keywords_1.default)(AJV);
chokidar_1.default.watch(args_1.default.file).on("change", validate_file);

@@ -22,10 +28,10 @@ chokidar_1.default.watch(args_1.default.schema).on("change", validate_file);

var raw;
console.log(chalk_1.default.inverse("Data file") + ": " + args_1.default.file);
console.log(chalk_1.default.inverse("Schema file") + ": " + args_1.default.schema);
console.log("".concat(chalk_1.default.inverse("Data file"), ": ").concat(args_1.default.file));
console.log("".concat(chalk_1.default.inverse("Schema file"), ": ").concat(args_1.default.schema));
if (args_1.default.query) {
console.log(chalk_1.default.inverse("Schema path") + ": " + args_1.default.query);
console.log("".concat(chalk_1.default.inverse("Schema path"), ": ").concat(args_1.default.query));
}
console.log(chalk_1.default.bgBlue.black("Last run at " + new Date().toLocaleString()));
console.log(chalk_1.default.bgBlue.black("Last run at ".concat(new Date().toLocaleString())));
try {
raw = fs_1.readFileSync(args_1.default.file, "utf-8");
raw = (0, fs_1.readFileSync)(args_1.default.file, "utf-8");
}

@@ -38,3 +44,3 @@ catch (err) {

try {
data = js_yaml_1.default.safeLoad(raw);
data = js_yaml_1.default.load(raw);
}

@@ -72,7 +78,25 @@ catch (err) {

catch (err) {
console.log("JMESpath '" + query + "' does not exist in the provided schema:");
console.error(err);
return;
if (/^[-a-zA-Z]*$/.test(query)) {
try {
schema = jmespath_1.default.search(schema, "\"".concat(query, "\""));
}
catch (_a) {
console.log("Failed with error: JMESpath '".concat(query, "' does not exist in the provided schema"));
console.log("this usually occures when there are special characters (e.g. \"-\") in the query string. Quote the string where appropriate.");
console.error(err);
return;
}
}
else {
console.log("Failed with error: JMESpath '".concat(query, "' does not exist in the provided schema"));
console.log("this usually occures when there are special characters (e.g. \"-\") in the query string. Quote the string where appropriate.");
console.error(err);
return;
}
}
}
if (schema === null) {
console.log("null schema: check your query string");
return;
}
// VALIDATE THE REQUEST BODY SCHEMA

@@ -89,7 +113,7 @@ var schema_is_valid = true;

if (args_1.default.query && !args_1.default.d) {
console.log(chalk_1.default.bgGreen("This may occure when using json references in combinatoin with the query parameter. Try setting the '-d' flag and re-starting"));
console.log(chalk_1.default.bgGreen("This may occure when using json references in combination with the query parameter. Try setting the '-d' flag and re-starting"));
}
AJV.errors &&
AJV.errors.map(function (e) {
return console.log(chalk_1.default.bgBlue(e.dataPath) + ": " + chalk_1.default.inverse(e.message));
return console.log("- ".concat(chalk_1.default.bgBlue(e.instancePath), ": ").concat(chalk_1.default.inverse(e.message)));
});

@@ -103,10 +127,10 @@ return;

catch (err) {
console.log(chalk_1.default.bgRed.black("Invalid schema"));
console.log(chalk_1.default.bgRed.black("Invalid data+schema"));
console.log("Error:", err.message);
if (args_1.default.query && !args_1.default.d) {
console.log(chalk_1.default.bgGreen.black("This may occure when using json references in combinatoin with the query parameter. Try setting the '-d' flag and re-starting"));
console.log(chalk_1.default.bgGreen.black("This may occure when using json references in combination with the query parameter. Try setting the '-d' flag and re-starting"));
}
AJV.errors &&
AJV.errors.map(function (e) {
return console.log(chalk_1.default.bgBlue(e.dataPath) + ": " + chalk_1.default.inverse(e.message));
return console.log("".concat(chalk_1.default.bgBlue(e.instancePath), ": ").concat(chalk_1.default.inverse(e.message)));
});

@@ -119,3 +143,3 @@ return;

AJV.errors.map(function (e) {
return console.log("'" + chalk_1.default.bgBlue(e.dataPath) + "': " + chalk_1.default.inverse(e.message));
return console.log("'".concat(chalk_1.default.bgBlue(e.instancePath), "': ").concat(chalk_1.default.inverse(e.message)));
});

@@ -122,0 +146,0 @@ }

import args from "./args";
import parser from "json-schema-ref-parser";
import $RefParser from "@apidevtools/json-schema-ref-parser";
import chokidar from "chokidar";
import chalk from "chalk";
import Ajv from "ajv";
import addFormats from "ajv-formats";
import addKeywords from "ajv-keywords";
import { readFileSync } from "fs";

@@ -10,4 +12,9 @@ import jq from "jmespath";

const AJV = new Ajv({ allErrors: true });
$RefParser.dereference = $RefParser.dereference.bind($RefParser);
$RefParser.resolve = $RefParser.resolve.bind($RefParser);
const AJV = new Ajv({ allErrors: true, $data: true });
addFormats(AJV);
addKeywords(AJV);
chokidar.watch(args.file).on("change", validate_file);

@@ -19,119 +26,145 @@ chokidar.watch(args.schema).on("change", validate_file);

function validate_file() {
console.clear();
let raw: string;
console.log(`${chalk.inverse("Data file")}: ${args.file}`);
console.log(`${chalk.inverse("Schema file")}: ${args.schema}`);
if (args.query) {
console.log(`${chalk.inverse("Schema path")}: ${args.query}`);
}
console.log(
chalk.bgBlue.black(`Last run at ${new Date().toLocaleString()}`)
console.clear();
let raw: string;
console.log(`${chalk.inverse("Data file")}: ${args.file}`);
console.log(`${chalk.inverse("Schema file")}: ${args.schema}`);
if (args.query) {
console.log(`${chalk.inverse("Schema path")}: ${args.query}`);
}
console.log(chalk.bgBlue.black(`Last run at ${new Date().toLocaleString()}`));
try {
raw = readFileSync(args.file, "utf-8");
} catch (err) {
console.log("Failed to read " + args.file);
return;
}
let data: any;
try {
data = yaml.load(raw);
} catch (err) {
console.log("Failed to parse " + args.file);
return;
}
if (args.d) {
$RefParser.dereference(
args.schema,
{},
(err: Error | null, schema: any) => {
if (err) {
console.log(chalk.bgRed.black(err.message));
console.log(err.stack);
return;
}
do_work(schema, data, args.query);
}
);
try {
raw = readFileSync(args.file, "utf-8");
} catch (err) {
console.log("Failed to read " + args.file);
} else {
$RefParser.bundle(args.schema, {}, (err: Error | null, schema: any) => {
if (err) {
console.log(chalk.bgRed.black(err.message));
console.log(err.stack);
return;
}
}
do_work(schema, data, args.query);
});
}
}
let data: any;
function do_work(schema: any, data: any, query?: string) {
if (query) {
try {
data = yaml.safeLoad(raw);
schema = jq.search(schema, query);
} catch (err) {
console.log("Failed to parse " + args.file);
if (/^[-a-zA-Z]*$/.test(query)) {
try {
schema = jq.search(schema, `"${query}"`);
} catch {
console.log(
`Failed with error: JMESpath '${query}' does not exist in the provided schema`
);
console.log(
`this usually occures when there are special characters (e.g. "-") in the query string. Quote the string where appropriate.`
);
console.error(err);
return;
}
} else {
console.log(
`Failed with error: JMESpath '${query}' does not exist in the provided schema`
);
console.log(
`this usually occures when there are special characters (e.g. "-") in the query string. Quote the string where appropriate.`
);
console.error(err);
return;
}
}
}
if (args.d) {
parser.dereference(args.schema, {}, (err, schema: any) => {
if (err) {
console.log(chalk.bgRed.black(err.message));
console.log(err.stack);
return;
}
do_work(schema, data, args.query);
});
} else {
parser.bundle(args.schema, {}, (err, schema: any) => {
if (err) {
console.log(chalk.bgRed.black(err.message));
console.log(err.stack);
return;
}
do_work(schema, data, args.query);
});
}
}
if (schema === null) {
console.log("null schema: check your query string");
return;
}
// VALIDATE THE REQUEST BODY SCHEMA
let schema_is_valid = true;
try {
schema_is_valid = AJV.validateSchema(schema) as boolean;
} catch (err) {
schema_is_valid = false;
}
if (!schema_is_valid) {
console.log(chalk.bgRed.black("Invalid schema"));
function do_work(schema: any, data: any, query?: string) {
if (query) {
try {
schema = jq.search(schema, query);
} catch (err) {
console.log(
`JMESpath '${query}' does not exist in the provided schema:`
);
console.error(err);
return;
}
if (args.query && !args.d) {
console.log(
chalk.bgGreen(
"This may occure when using json references in combination with the query parameter. Try setting the '-d' flag and re-starting"
)
);
}
AJV.errors &&
AJV.errors.map((e) =>
console.log(
`- ${chalk.bgBlue(e.instancePath)}: ${chalk.inverse(e.message)}`
)
);
return;
}
// VALIDATE THE REQUEST BODY SCHEMA
let schema_is_valid = true;
try {
schema_is_valid = AJV.validateSchema(schema);
} catch (err) {
schema_is_valid = false;
}
if (!schema_is_valid) {
console.log(chalk.bgRed.black("Invalid schema"));
if (args.query && !args.d) {
console.log(
chalk.bgGreen(
"This may occure when using json references in combinatoin with the query parameter. Try setting the '-d' flag and re-starting"
)
);
}
AJV.errors &&
AJV.errors.map(e =>
console.log(
`${chalk.bgBlue(e.dataPath)}: ${chalk.inverse(e.message)}`
)
);
return;
}
let data_is_valid = true;
try {
data_is_valid = AJV.validate(schema, data) as boolean;
} catch (err) {
console.log(chalk.bgRed.black("Invalid data+schema"));
console.log("Error:", (err as Error).message);
let data_is_valid = true;
try {
data_is_valid = AJV.validate(schema, data) as boolean;
} catch (err) {
console.log(chalk.bgRed.black("Invalid schema"));
console.log("Error:", err.message);
if (args.query && !args.d) {
console.log(
chalk.bgGreen.black(
"This may occure when using json references in combinatoin with the query parameter. Try setting the '-d' flag and re-starting"
)
);
}
AJV.errors &&
AJV.errors.map(e =>
console.log(
`${chalk.bgBlue(e.dataPath)}: ${chalk.inverse(e.message)}`
)
);
return;
if (args.query && !args.d) {
console.log(
chalk.bgGreen.black(
"This may occure when using json references in combination with the query parameter. Try setting the '-d' flag and re-starting"
)
);
}
AJV.errors &&
AJV.errors.map((e) =>
console.log(
`${chalk.bgBlue(e.instancePath)}: ${chalk.inverse(e.message)}`
)
);
return;
}
if (!data_is_valid) {
console.log(chalk.black.bgRed("Data does not follow the schema"));
AJV.errors &&
AJV.errors.map(e =>
console.log(
`'${chalk.bgBlue(e.dataPath)}': ${chalk.inverse(e.message)}`
)
);
} else {
console.log("Data matches the schema!");
}
if (!data_is_valid) {
console.log(chalk.black.bgRed("Data does not follow the schema"));
AJV.errors &&
AJV.errors.map((e) =>
console.log(
`'${chalk.bgBlue(e.instancePath)}': ${chalk.inverse(e.message)}`
)
);
} else {
console.log("Data matches the schema!");
}
}

@@ -138,0 +171,0 @@

{
"name": "schema-watcher",
"version": "1.0.2",
"description": "File Watcher + Schema Validator",
"main": "index.js",
"author": "Jason Thorpe <jathorpe@microsoft.com>",
"license": "MIT",
"dependencies": {
"ajv": "^6.11.0",
"argparse": "^1.0.10",
"chalk": "^3.0.0",
"chokidar": "^3.3.1",
"jmespath": "0.14.0",
"js-yaml": "^3.13.1",
"json-schema": "^0.2.5",
"json-schema-ref-parser": "^7.1.3"
},
"devDependencies": {
"@types/argparse": "^1.0.38",
"@types/jmespath": "^0.15.0",
"@types/js-yaml": "^3.12.2",
"@types/json-schema": "^7.0.4",
"@types/node": "^13.7.0",
"@types/node-emoji": "^1.8.1"
},
"bin": {
"swatch": "./bin/swatch"
}
"name": "schema-watcher",
"version": "1.0.3",
"description": "File Watcher + Schema Validator",
"main": "index.js",
"author": "Jason Thorpe <jathorpe@microsoft.com>",
"license": "MIT",
"dependencies": {
"ajv": "^8.11.0",
"ajv-formats": "^2.1.1",
"ajv-keywords": "^5.1.0",
"argparse": "^2.0.1",
"chalk": "^4.1.2",
"chokidar": "^3.3.1",
"jmespath": "0.14.0",
"js-yaml": "^4.1.0",
"json-schema": "^0.4.0",
"json-schema-ref-parser": "^9.0.9"
},
"devDependencies": {
"@types/argparse": "^2.0.10",
"@types/jmespath": "^0.14.0",
"@types/js-yaml": "^4.0.5",
"@types/json-schema": "^7.0.4",
"@types/node": "^18.0.3",
"@types/node-emoji": "^1.8.1",
"typescript": "^4.7.4"
},
"bin": {
"swatch": "./bin/swatch"
}
}

@@ -0,0 +0,0 @@ # schema-watcher (swatch)

@@ -0,0 +0,0 @@ {

Sorry, the diff of this file is not supported yet

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