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

jest-schema-matcher

Package Overview
Dependencies
Maintainers
1
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jest-schema-matcher - npm Package Compare versions

Comparing version 1.3.0 to 1.3.1

2

dist/index.d.ts

@@ -1,1 +0,1 @@

export { toMatchSchema } from './to-match-schema';
export { default } from './to-match-schema';
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var to_match_schema_1 = require("./to-match-schema");
Object.defineProperty(exports, "toMatchSchema", { enumerable: true, get: function () { return to_match_schema_1.toMatchSchema; } });
Object.defineProperty(exports, "default", { enumerable: true, get: function () { return to_match_schema_1.default; } });
//# sourceMappingURL=index.js.map

@@ -1,2 +0,2 @@

export declare function toMatchSchema(object: any, schemaName: any): {
export default function toMatchSchema(object: any, schemaName: string): {
message: () => string;

@@ -3,0 +3,0 @@ pass: boolean;

"use strict";
// Import Ajv from 'ajv';
// Import prettyFormat from 'pretty-format';
var __importDefault = (this && this.__importDefault) || function (mod) {

@@ -8,3 +6,4 @@ return (mod && mod.__esModule) ? mod : { "default": mod };

Object.defineProperty(exports, "__esModule", { value: true });
exports.toMatchSchema = void 0;
const ajv_1 = __importDefault(require("ajv"));
const jest_matcher_utils_1 = require("jest-matcher-utils");
const schematized_1 = require("schematized");

@@ -14,4 +13,4 @@ const chalk_1 = __importDefault(require("chalk"));

const fs_1 = __importDefault(require("fs"));
const jest_matcher_utils_1 = require("jest-matcher-utils");
const path_1 = __importDefault(require("path"));
const pretty_format_1 = __importDefault(require("pretty-format"));
const { SHOULD_UPDATE_SCHEMAS } = global.TEST_DATA;

@@ -23,8 +22,13 @@ function toMatchSchema(object, schemaName) {

if (schema) {
const schemaChanges = checkForSchemaChanges(schemaName, schemaPath, schema, object);
if (schemaChanges) {
failureMessages.unshift(schemaChanges);
const schemaDiffCheck = checkForSchemaChanges(schemaName, schemaPath, schema, object);
if (schemaDiffCheck.updated) {
schema = readSchema(schemaPath);
}
// Const validationFailures = validateAgainstSchema(schema, object)
// if (validationFailures) failureMessages.unshift(validationFailures)
if (schemaDiffCheck.failures) {
failureMessages.unshift(...schemaDiffCheck.failures);
}
const validationFailures = validateAgainstSchema(schema, object);
if (validationFailures) {
failureMessages.unshift(...validationFailures);
}
}

@@ -46,5 +50,3 @@ if (!schema) {

return {
message: () => {
return failureMessages.join('\n\n');
},
message: () => failureMessages.join('\n'),
pass,

@@ -54,3 +56,3 @@ schema

}
exports.toMatchSchema = toMatchSchema;
exports.default = toMatchSchema;
function writeSchema(path, schema) {

@@ -70,12 +72,6 @@ const s = typeof schema === 'string' ? schema : JSON.stringify(schema, null, 2);

function checkIfFileExists(path) {
try {
fs_1.default.existsSync(path);
return true;
}
catch {
return false;
}
return fs_1.default.existsSync(path);
}
function findSchema(testPath, schemaName) {
const testDir = testPath.match(/.*\/(?=.+ts)/)[0];
const testDir = /.*\/(?=.+ts)/.exec(testPath)[0];
const schemaDir = path_1.default.resolve(testDir, 'schemas');

@@ -96,14 +92,27 @@ const dirExists = checkIfFileExists(schemaDir);

writeSchema(schemaPath, newSchema);
return {
changes: true,
updated: true,
failures: []
};
}
if (!SHOULD_UPDATE_SCHEMAS) {
return [
chalk_1.default.bold.red('JSON Schema Trainer: ') +
chalk_1.default.yellow('Schema change is recommended:') +
'\n\n' +
schemaDiff.text +
'\n\n'
];
return {
changes: true,
updated: false,
failures: [
chalk_1.default.bold.red('JSON Schema Trainer: ') +
chalk_1.default.yellow('Schema change is recommended:') +
'\n\n' +
schemaDiff.text +
'\n\n'
]
};
}
}
return undefined;
return {
changes: false,
updated: false,
failures: []
};
}

@@ -126,37 +135,35 @@ function compareSchemas(schemaA, schemaB) {

}
// Function createSchemaValidator(schema) {
// const ajv = new Ajv({schemaId: 'auto', allErrors: true});
// return ajv.compile(schema);
// }
// Function validateAgainstSchema(schema, object) {
// const validator = createSchemaValidator(schema);
// const valid = validator(object);
// if (!valid) {
// return shapeValidationMessage(validator, object);
// }
// }
// function shapeValidationMessage(validator, _object) {
// const {errors} = validator;
// return errors.map(error => {
// const rejectedValue = prettyFormat(eval(`_object${error.objectPath}`));
// const allowedValues = error?.params.allowedValues;
// return (
// chalk.bold.yellow(`${error.keyword.toUpperCase()}`) +
// ' violation: ' +
// chalk.yellow(`received${error.objectPath} ${error.message}.`) +
// '\n\n' +
// 'Rejected value: ' +
// printReceived(rejectedValue) +
// '\n\n' +
// (
// allowedValues ?
// chalk.dim('Allowed values: ') +
// chalk.dim.green(prettyFormat(allowedValues)) +
// '\n\n' :
// ''
// ) +
// chalk.dim('Schema rule: ' + printExpected(error.schemaPath))
// );
// });
// }
function createSchemaValidator(schema) {
const ajv = new ajv_1.default({ schemaId: 'auto', allErrors: true });
return ajv.compile(schema);
}
function validateAgainstSchema(schema, object) {
const validator = createSchemaValidator(schema);
const valid = validator(object);
if (!valid) {
return shapeValidationMessage(validator, object);
}
return null;
}
function shapeValidationMessage(validator, _object) {
const { errors } = validator;
return errors.map(error => {
const rejectedValue = pretty_format_1.default(eval(`_object${error.dataPath}`));
const violationDetails = error.params;
return (chalk_1.default.bold.yellow(error.keyword.toUpperCase()) +
' violation: ' +
chalk_1.default.yellow(`received ${error.dataPath} ${error.message}.`) +
'\n\n' +
'Rejected value: ' +
jest_matcher_utils_1.printReceived(rejectedValue) +
'\n\n' +
(violationDetails ?
'Violations details: ' +
chalk_1.default.red(pretty_format_1.default(violationDetails)) +
'\n\n' :
'') +
chalk_1.default.dim('Schema rule: ' + jest_matcher_utils_1.printExpected(error.schemaPath)) +
'\n\n');
});
}
//# sourceMappingURL=to-match-schema.js.map
{
"name": "jest-schema-matcher",
"version": "1.3.0",
"version": "1.3.1",
"license": "MIT",

@@ -19,7 +19,22 @@ "files": [

},
"xo": {
"extends": [
"plugin:jest/recommended"
],
"rules": {
"@typescript-eslint/restrict-plus-operands": 0,
"@typescript-eslint/restrict-template-expressions": 0,
"no-eval": 0
}
},
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
},
"dependencies": {
"ajv": "^6.12.2",
"chalk": "^4.1.0",
"jest-matcher-utils": "^26.0.1",
"pretty-format": "^26.0.1",
"jest-matcher-utils": "^26.1.0",
"pretty-format": "^26.1.0",
"schematized": "^1.5.1",

@@ -29,34 +44,16 @@ "variable-diff": "^2.0.1"

"devDependencies": {
"@types/jest": "^26.0.0",
"@types/node": "^14.0.13",
"@types/jest": "^26.0.3",
"@types/node": "^14.0.14",
"commitizen": "^4.1.2",
"cz-conventional-changelog": "^3.2.0",
"eslint-plugin-jest": "^23.16.0",
"eslint-plugin-prettier": "^3.1.4",
"jest": "^26.0.1",
"eslint-plugin-jest": "^23.17.1",
"jest": "^26.1.0",
"nyc": "^15.1.0",
"prettier": "^2.0.5",
"rimraf": "^3.0.2",
"semantic-release": "^17.0.8",
"semantic-release": "^17.1.1",
"ts-jest": "^26.1.1",
"ts-node": "^8.10.2",
"typescript": "^3.9.5",
"xo": "^0.32.0"
},
"xo": {
"extends": [
"plugin:jest/recommended"
],
"prettier": true,
"rules": {
"@typescript-eslint/restrict-plus-operands": 0,
"@typescript-eslint/restrict-template-expressions": 0,
"no-eval": 0
}
},
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
},
"keywords": [

@@ -63,0 +60,0 @@ "jest",

Sorry, the diff of this file is not supported yet

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