@readme/better-ajv-errors
Advanced tools
Comparing version 1.0.1 to 1.1.0
@@ -13,41 +13,52 @@ "use strict"; | ||
var _2 = _interopRequireDefault(require("ajv/dist/2020")); | ||
var _testHelpers = require("../test-helpers"); | ||
var _ = _interopRequireDefault(require("..")); | ||
var _3 = _interopRequireDefault(require("..")); | ||
var _lib = _interopRequireDefault(require("../../lib")); | ||
var _openapiSchemas = require("@apidevtools/openapi-schemas"); | ||
describe('Main', function () { | ||
it('should output error with reconstructed codeframe', /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee() { | ||
var _yield$getSchemaAndDa, _yield$getSchemaAndDa2, schema, data, ajv, validate, valid, res; | ||
it.each([['should output error with reconstructed codeframe', true], ['should output error with reconstructed codeframe [without colors]', false]])('%s', /*#__PURE__*/function () { | ||
var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(_, colorize) { | ||
var _yield$getSchemaAndDa, _yield$getSchemaAndDa2, schema, data, ajv, validate, valid, res; | ||
return _regenerator["default"].wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
_context.next = 2; | ||
return (0, _testHelpers.getSchemaAndData)('default', __dirname); | ||
return _regenerator["default"].wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
_context.next = 2; | ||
return (0, _testHelpers.getSchemaAndData)('default', __dirname); | ||
case 2: | ||
_yield$getSchemaAndDa = _context.sent; | ||
_yield$getSchemaAndDa2 = (0, _slicedToArray2["default"])(_yield$getSchemaAndDa, 2); | ||
schema = _yield$getSchemaAndDa2[0]; | ||
data = _yield$getSchemaAndDa2[1]; | ||
ajv = new _ajv["default"](); | ||
validate = ajv.compile(schema); | ||
valid = validate(data); | ||
expect(valid).toBe(false); | ||
res = (0, _["default"])(schema, data, validate.errors, { | ||
format: 'cli', | ||
indent: 2 | ||
}); | ||
expect(res).toMatchSnapshot(); | ||
case 2: | ||
_yield$getSchemaAndDa = _context.sent; | ||
_yield$getSchemaAndDa2 = (0, _slicedToArray2["default"])(_yield$getSchemaAndDa, 2); | ||
schema = _yield$getSchemaAndDa2[0]; | ||
data = _yield$getSchemaAndDa2[1]; | ||
ajv = new _ajv["default"](); | ||
validate = ajv.compile(schema); | ||
valid = validate(data); | ||
expect(valid).toBe(false); | ||
res = (0, _3["default"])(schema, data, validate.errors, { | ||
colorize: colorize, | ||
format: 'cli', | ||
indent: 2 | ||
}); | ||
expect(res).toMatchSnapshot(); | ||
case 12: | ||
case "end": | ||
return _context.stop(); | ||
case 12: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
} | ||
}, _callee); | ||
}))); | ||
}, _callee); | ||
})); | ||
return function (_x, _x2) { | ||
return _ref.apply(this, arguments); | ||
}; | ||
}()); | ||
it('should output error with codeframe', /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2() { | ||
@@ -73,3 +84,3 @@ var _yield$getSchemaAndDa3, _yield$getSchemaAndDa4, schema, data, json, ajv, validate, valid, res; | ||
expect(valid).toBe(false); | ||
res = (0, _["default"])(schema, data, validate.errors, { | ||
res = (0, _3["default"])(schema, data, validate.errors, { | ||
format: 'cli', | ||
@@ -118,2 +129,42 @@ json: json | ||
}); | ||
describe('complex schema examples', function () { | ||
it('should output an error on an invalid OpenAPI 3.1 definition', /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4() { | ||
var schema, _yield$getSchemaAndDa7, _yield$getSchemaAndDa8, data, ajv, validate, valid, res; | ||
return _regenerator["default"].wrap(function _callee4$(_context4) { | ||
while (1) { | ||
switch (_context4.prev = _context4.next) { | ||
case 0: | ||
schema = _openapiSchemas.openapi.v31; | ||
_context4.next = 3; | ||
return (0, _testHelpers.getSchemaAndData)('openapi-3.1', __dirname); | ||
case 3: | ||
_yield$getSchemaAndDa7 = _context4.sent; | ||
_yield$getSchemaAndDa8 = (0, _slicedToArray2["default"])(_yield$getSchemaAndDa7, 2); | ||
data = _yield$getSchemaAndDa8[1]; | ||
// Need to load the 2020 dist because it supports draft-7, which we'll need for an 3.1.0 | ||
// OpenAPI definition. | ||
ajv = new _2["default"]({ | ||
allErrors: true, | ||
strict: false, | ||
validateFormats: false | ||
}); | ||
validate = ajv.compile(schema); | ||
valid = validate(data); | ||
expect(valid).toBe(false); | ||
res = (0, _3["default"])(schema, data, validate.errors, { | ||
indent: 2, | ||
format: 'cli' | ||
}); | ||
expect(res).toMatchSnapshot(); | ||
case 12: | ||
case "end": | ||
return _context4.stop(); | ||
} | ||
} | ||
}, _callee4); | ||
}))); | ||
}); | ||
}); |
@@ -16,3 +16,5 @@ "use strict"; | ||
var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; | ||
var _options$format = options.format, | ||
var _options$colorize = options.colorize, | ||
colorize = _options$colorize === void 0 ? true : _options$colorize, | ||
_options$format = options.format, | ||
format = _options$format === void 0 ? 'cli' : _options$format, | ||
@@ -37,2 +39,3 @@ _options$indent = options.indent, | ||
var customErrors = (0, _helpers["default"])(errors, { | ||
colorize: colorize, | ||
data: data, | ||
@@ -39,0 +42,0 @@ schema: schema, |
@@ -50,3 +50,3 @@ "use strict"; | ||
}))); | ||
it('prints correctly for enum prop', function () { | ||
it.each([['prints correctly for enum prop', true], ['prints correctly for enum prop [without colors]', false]])('%s', function (_, colorize) { | ||
var error = new _enum["default"]({ | ||
@@ -61,2 +61,3 @@ keyword: 'enum', | ||
}, { | ||
colorize: colorize, | ||
data: data, | ||
@@ -69,3 +70,3 @@ schema: schema, | ||
}); | ||
it('prints correctly for no levenshtein match', function () { | ||
it.each([['prints correctly for no levenshtein match', true], ['prints correctly for no levenshtein match [without colors]', false]])('%s', function (_, colorize) { | ||
var error = new _enum["default"]({ | ||
@@ -80,2 +81,3 @@ keyword: 'enum', | ||
}, { | ||
colorize: colorize, | ||
data: data, | ||
@@ -88,3 +90,3 @@ schema: schema, | ||
}); | ||
it('prints correctly for empty value', function () { | ||
it.each([['prints correctly for empty value', true], ['prints correctly for empty value [without colors]', false]])('%s', function (_, colorize) { | ||
var error = new _enum["default"]({ | ||
@@ -99,2 +101,3 @@ keyword: 'enum', | ||
}, { | ||
colorize: colorize, | ||
data: data, | ||
@@ -142,3 +145,3 @@ schema: schema, | ||
}))); | ||
it('prints correctly for enum prop', function () { | ||
it.each([['prints correctly for enum prop', true], ['prints correctly for enum prop [without colors]', false]])('%s', function (_, colorize) { | ||
var error = new _enum["default"]({ | ||
@@ -153,2 +156,3 @@ keyword: 'enum', | ||
}, { | ||
colorize: colorize, | ||
data: data, | ||
@@ -161,3 +165,3 @@ schema: schema, | ||
}); | ||
it('prints correctly for no levenshtein match', function () { | ||
it.each([['prints correctly for no levenshtein match', true], ['prints correctly for no levenshtein match [without colors]', false]])('%s', function (_, colorize) { | ||
var error = new _enum["default"]({ | ||
@@ -172,2 +176,3 @@ keyword: 'enum', | ||
}, { | ||
colorize: colorize, | ||
data: data, | ||
@@ -180,3 +185,3 @@ schema: schema, | ||
}); | ||
it('prints correctly for empty value', function () { | ||
it.each([['prints correctly for empty value', true], ['prints correctly for empty value [without colors]', false]])('%s', function (_, colorize) { | ||
var error = new _enum["default"]({ | ||
@@ -191,2 +196,3 @@ keyword: 'enum', | ||
}, { | ||
colorize: colorize, | ||
data: data, | ||
@@ -193,0 +199,0 @@ schema: schema, |
@@ -18,44 +18,51 @@ "use strict"; | ||
describe('Required', function () { | ||
it('prints correctly for missing required prop', /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee() { | ||
var _yield$getSchemaAndDa, _yield$getSchemaAndDa2, schema, data, jsonRaw, jsonAst, error; | ||
it.each([['prints correctly for missing required prop', true], ['prints correctly for missing required prop[without colors]', false]])('%s', /*#__PURE__*/function () { | ||
var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(_, colorize) { | ||
var _yield$getSchemaAndDa, _yield$getSchemaAndDa2, schema, data, jsonRaw, jsonAst, error; | ||
return _regenerator["default"].wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
_context.next = 2; | ||
return (0, _testHelpers.getSchemaAndData)('required', __dirname); | ||
return _regenerator["default"].wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
_context.next = 2; | ||
return (0, _testHelpers.getSchemaAndData)('required', __dirname); | ||
case 2: | ||
_yield$getSchemaAndDa = _context.sent; | ||
_yield$getSchemaAndDa2 = (0, _slicedToArray2["default"])(_yield$getSchemaAndDa, 2); | ||
schema = _yield$getSchemaAndDa2[0]; | ||
data = _yield$getSchemaAndDa2[1]; | ||
jsonRaw = JSON.stringify(data, null, 2); | ||
jsonAst = (0, _jsonToAst["default"])(jsonRaw, { | ||
loc: true | ||
}); | ||
error = new _required["default"]({ | ||
keyword: 'required', | ||
dataPath: '/nested', | ||
schemaPath: '#/required', | ||
params: { | ||
missingProperty: 'id' | ||
}, | ||
message: "should have required property 'id'" | ||
}, { | ||
data: data, | ||
schema: schema, | ||
jsonRaw: jsonRaw, | ||
jsonAst: jsonAst | ||
}); | ||
expect(error.print()).toMatchSnapshot(); | ||
case 2: | ||
_yield$getSchemaAndDa = _context.sent; | ||
_yield$getSchemaAndDa2 = (0, _slicedToArray2["default"])(_yield$getSchemaAndDa, 2); | ||
schema = _yield$getSchemaAndDa2[0]; | ||
data = _yield$getSchemaAndDa2[1]; | ||
jsonRaw = JSON.stringify(data, null, 2); | ||
jsonAst = (0, _jsonToAst["default"])(jsonRaw, { | ||
loc: true | ||
}); | ||
error = new _required["default"]({ | ||
keyword: 'required', | ||
dataPath: '/nested', | ||
schemaPath: '#/required', | ||
params: { | ||
missingProperty: 'id' | ||
}, | ||
message: "should have required property 'id'" | ||
}, { | ||
colorize: colorize, | ||
data: data, | ||
schema: schema, | ||
jsonRaw: jsonRaw, | ||
jsonAst: jsonAst | ||
}); | ||
expect(error.print()).toMatchSnapshot(); | ||
case 10: | ||
case "end": | ||
return _context.stop(); | ||
case 10: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
} | ||
}, _callee); | ||
}))); | ||
}, _callee); | ||
})); | ||
return function (_x, _x2) { | ||
return _ref.apply(this, arguments); | ||
}; | ||
}()); | ||
}); |
@@ -24,4 +24,2 @@ "use strict"; | ||
var _chalk = _interopRequireDefault(require("chalk")); | ||
var _base = _interopRequireDefault(require("./base")); | ||
@@ -65,4 +63,5 @@ | ||
params = _this$options.params; | ||
var output = [(0, _chalk["default"])(_templateObject || (_templateObject = (0, _taggedTemplateLiteral2["default"])(["{red {bold ADDTIONAL PROPERTY} ", "}\n"], ["{red {bold ADDTIONAL PROPERTY} ", "}\\n"])), message)]; | ||
return output.concat(this.getCodeFrame((0, _chalk["default"])(_templateObject2 || (_templateObject2 = (0, _taggedTemplateLiteral2["default"])(["\uD83D\uDE32 {magentaBright ", "} is not expected to be here!"])), params.additionalProperty), "".concat(this.instancePath, "/").concat(params.additionalProperty))); | ||
var chalk = this.getChalk(); | ||
var output = [chalk(_templateObject || (_templateObject = (0, _taggedTemplateLiteral2["default"])(["{red {bold ADDTIONAL PROPERTY} ", "}\n"], ["{red {bold ADDTIONAL PROPERTY} ", "}\\n"])), message)]; | ||
return output.concat(this.getCodeFrame(chalk(_templateObject2 || (_templateObject2 = (0, _taggedTemplateLiteral2["default"])(["\uD83D\uDE32 {magentaBright ", "} is not expected to be here!"])), params.additionalProperty), "".concat(this.instancePath, "/").concat(params.additionalProperty))); | ||
} | ||
@@ -69,0 +68,0 @@ }, { |
@@ -14,2 +14,4 @@ "use strict"; | ||
var _chalk = _interopRequireDefault(require("chalk")); | ||
var _codeFrame = require("@babel/code-frame"); | ||
@@ -26,2 +28,3 @@ | ||
var _ref = arguments.length > 1 ? arguments[1] : undefined, | ||
colorize = _ref.colorize, | ||
data = _ref.data, | ||
@@ -34,2 +37,3 @@ schema = _ref.schema, | ||
this.options = options; | ||
this.colorize = !!(!!colorize || colorize === undefined); | ||
this.data = data; | ||
@@ -42,2 +46,9 @@ this.schema = schema; | ||
(0, _createClass2["default"])(BaseValidationError, [{ | ||
key: "getChalk", | ||
value: function getChalk() { | ||
return this.colorize ? _chalk["default"] : new _chalk["default"].Instance({ | ||
level: 0 | ||
}); | ||
} | ||
}, { | ||
key: "getLocation", | ||
@@ -69,3 +80,3 @@ value: function getLocation() { | ||
return (0, _codeFrame.codeFrameColumns)(this.jsonRaw, this.getLocation(dataPath), { | ||
highlightCode: true, | ||
highlightCode: this.colorize, | ||
message: message | ||
@@ -72,0 +83,0 @@ }); |
@@ -24,4 +24,2 @@ "use strict"; | ||
var _chalk = _interopRequireDefault(require("chalk")); | ||
var _base = _interopRequireDefault(require("./base")); | ||
@@ -65,4 +63,5 @@ | ||
message = _this$options.message; | ||
var output = [(0, _chalk["default"])(_templateObject || (_templateObject = (0, _taggedTemplateLiteral2["default"])(["{red {bold ", "} ", "}\n"], ["{red {bold ", "} ", "}\\n"])), keyword.toUpperCase(), message)]; | ||
return output.concat(this.getCodeFrame((0, _chalk["default"])(_templateObject2 || (_templateObject2 = (0, _taggedTemplateLiteral2["default"])(["\uD83D\uDC48\uD83C\uDFFD {magentaBright ", "} ", ""])), keyword, message))); | ||
var chalk = this.getChalk(); | ||
var output = [chalk(_templateObject || (_templateObject = (0, _taggedTemplateLiteral2["default"])(["{red {bold ", "} ", "}\n"], ["{red {bold ", "} ", "}\\n"])), keyword.toUpperCase(), message)]; | ||
return output.concat(this.getCodeFrame(chalk(_templateObject2 || (_templateObject2 = (0, _taggedTemplateLiteral2["default"])(["\uD83D\uDC48\uD83C\uDFFD {magentaBright ", "} ", ""])), keyword, message))); | ||
} | ||
@@ -69,0 +68,0 @@ }, { |
@@ -24,4 +24,2 @@ "use strict"; | ||
var _chalk = _interopRequireDefault(require("chalk")); | ||
var _leven = _interopRequireDefault(require("leven")); | ||
@@ -68,5 +66,6 @@ | ||
allowedValues = _this$options.params.allowedValues; | ||
var chalk = this.getChalk(); | ||
var bestMatch = this.findBestMatch(); | ||
var output = [(0, _chalk["default"])(_templateObject || (_templateObject = (0, _taggedTemplateLiteral2["default"])(["{red {bold ENUM} ", "}"])), message), (0, _chalk["default"])(_templateObject2 || (_templateObject2 = (0, _taggedTemplateLiteral2["default"])(["{red (", ")}\n"], ["{red (", ")}\\n"])), allowedValues.join(', '))]; | ||
return output.concat(this.getCodeFrame(bestMatch !== null ? (0, _chalk["default"])(_templateObject3 || (_templateObject3 = (0, _taggedTemplateLiteral2["default"])(["\uD83D\uDC48\uD83C\uDFFD Did you mean {magentaBright ", "} here?"])), bestMatch) : (0, _chalk["default"])(_templateObject4 || (_templateObject4 = (0, _taggedTemplateLiteral2["default"])(["\uD83D\uDC48\uD83C\uDFFD Unexpected value, should be equal to one of the allowed values"]))))); | ||
var output = [chalk(_templateObject || (_templateObject = (0, _taggedTemplateLiteral2["default"])(["{red {bold ENUM} ", "}"])), message), chalk(_templateObject2 || (_templateObject2 = (0, _taggedTemplateLiteral2["default"])(["{red (", ")}\n"], ["{red (", ")}\\n"])), allowedValues.join(', '))]; | ||
return output.concat(this.getCodeFrame(bestMatch !== null ? chalk(_templateObject3 || (_templateObject3 = (0, _taggedTemplateLiteral2["default"])(["\uD83D\uDC48\uD83C\uDFFD Did you mean {magentaBright ", "} here?"])), bestMatch) : chalk(_templateObject4 || (_templateObject4 = (0, _taggedTemplateLiteral2["default"])(["\uD83D\uDC48\uD83C\uDFFD Unexpected value, should be equal to one of the allowed values"]))))); | ||
} | ||
@@ -73,0 +72,0 @@ }, { |
@@ -26,4 +26,2 @@ "use strict"; | ||
var _chalk = _interopRequireDefault(require("chalk")); | ||
var _base = _interopRequireDefault(require("./base")); | ||
@@ -78,4 +76,5 @@ | ||
params = _this$options.params; | ||
var output = [(0, _chalk["default"])(_templateObject || (_templateObject = (0, _taggedTemplateLiteral2["default"])(["{red {bold REQUIRED} ", "}\n"], ["{red {bold REQUIRED} ", "}\\n"])), message)]; | ||
return output.concat(this.getCodeFrame((0, _chalk["default"])(_templateObject2 || (_templateObject2 = (0, _taggedTemplateLiteral2["default"])(["\u2639\uFE0F {magentaBright ", "} is missing here!"])), params.missingProperty))); | ||
var chalk = this.getChalk(); | ||
var output = [chalk(_templateObject || (_templateObject = (0, _taggedTemplateLiteral2["default"])(["{red {bold REQUIRED} ", "}\n"], ["{red {bold REQUIRED} ", "}\\n"])), message)]; | ||
return output.concat(this.getCodeFrame(chalk(_templateObject2 || (_templateObject2 = (0, _taggedTemplateLiteral2["default"])(["\u2639\uFE0F {magentaBright ", "} is missing here!"])), params.missingProperty))); | ||
} | ||
@@ -82,0 +81,0 @@ }, { |
{ | ||
"name": "@readme/better-ajv-errors", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "JSON Schema validation for Human", | ||
@@ -33,2 +33,3 @@ "license": "Apache-2.0", | ||
"prebuild": "rm -rf lib", | ||
"prepack": "npm run build", | ||
"prepare": "husky install", | ||
@@ -35,0 +36,0 @@ "prerelease": "npm run build", |
@@ -20,2 +20,3 @@ <h1 align="center"> | ||
- This helps eliminate long blocks of `^^^^^^` pointers. | ||
- Addition of a `colorize` option for disabling colorization in `format: cli` output. | ||
- Up-to-date dependencies. | ||
@@ -85,2 +86,9 @@ | ||
##### colorize | ||
Type: `boolean` | ||
Default: `true` | ||
When disabled, if you are outputting `cli` formatting it will be without colorized or styled content. | ||
##### format | ||
@@ -87,0 +95,0 @@ |
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
84284
1687
131