awesome-ajv-errors
Advanced tools
Comparing version 3.0.0 to 4.0.0
@@ -8,2 +8,2 @@ import type { LocationOptions, ParsedJson } from 'jsonpos'; | ||
export declare type PrintCode = (message: string | undefined, parsedJson: ParsedJson, options: CodeOptions) => string; | ||
export declare function makePrintCode(colors: boolean | undefined, printCode: PrintCode): PrintCode; | ||
export declare function makePrintCode(enabled: boolean, colors: boolean | undefined, printCode: PrintCode): PrintCode; |
@@ -1,3 +0,5 @@ | ||
export function makePrintCode(colors, printCode) { | ||
return (message, parsedJson, options) => printCode(message, parsedJson, { colors, ...options }); | ||
export function makePrintCode(enabled, colors, printCode) { | ||
return (message, parsedJson, options) => !enabled | ||
? '' | ||
: printCode(message, parsedJson, { colors, ...options }); | ||
} |
import { managerOptions } from './style/style-plain.js'; | ||
import { printCode } from './code/impl-none.js'; | ||
import { makePrettify } from './prettification.js'; | ||
export const prettify = makePrettify(managerOptions, printCode); | ||
export const prettify = makePrettify(managerOptions, printCode, 'browser'); |
import { managerOptions } from './style/style-ansi.js'; | ||
import { printCode } from './code/impl-babel.js'; | ||
import { makePrettify } from './prettification.js'; | ||
export const prettify = makePrettify(managerOptions, printCode); | ||
export const prettify = makePrettify(managerOptions, printCode, 'node'); |
@@ -11,3 +11,3 @@ import { managerOptions } from './style/style-plain.js'; | ||
]); | ||
return makePrettify(style.managerOptions, code.printCode); | ||
return makePrettify(style.managerOptions, code.printCode, 'browser'); | ||
})(); | ||
@@ -14,0 +14,0 @@ export const prettifyTryStyled = async (a, b) => { |
@@ -11,2 +11,17 @@ import { ValidateFunction } from "ajv"; | ||
colors?: boolean; | ||
/** | ||
* Include (if possible) the location of the error using a pretty-printed | ||
* code-frame. | ||
* | ||
* Defaults to `true` | ||
*/ | ||
location?: boolean; | ||
/** | ||
* When pretty-printing (if `location` is enabled), print big numbers | ||
* before each error if there are multiple errors. | ||
* | ||
* Defaults to `true` in Node.js (if location is enabled) and `false` | ||
* otherwise. | ||
*/ | ||
bigNumbers?: boolean; | ||
} | ||
@@ -17,2 +32,4 @@ export interface Prettify<Ret> { | ||
} | ||
export declare function makePrettify(managerOptions: ManagerOptions, printCode: PrintCode): Prettify<string>; | ||
declare type Environment = 'node' | 'browser'; | ||
export declare function makePrettify(managerOptions: ManagerOptions, printCode: PrintCode, environment: Environment): Prettify<string>; | ||
export {}; |
@@ -9,3 +9,3 @@ import { getAstByObject } from "jsonpos"; | ||
import { handlers } from "./prettifications/index.js"; | ||
export function makePrettify(managerOptions, printCode) { | ||
export function makePrettify(managerOptions, printCode, environment) { | ||
const styleManager = makeManager(managerOptions); | ||
@@ -25,3 +25,6 @@ const styleManagerPlain = makeManager(plainOptions); | ||
styleManager, | ||
printCode: makePrintCode(styleManager.support, printCode), | ||
printCode, | ||
location: opts === null || opts === void 0 ? void 0 : opts.location, | ||
bigNumbers: opts === null || opts === void 0 ? void 0 : opts.bigNumbers, | ||
environment, | ||
}); | ||
@@ -34,3 +37,6 @@ } | ||
styleManager, | ||
printCode: makePrintCode(styleManager.support, printCode), | ||
printCode, | ||
location: undefined, | ||
bigNumbers: undefined, | ||
environment, | ||
}); | ||
@@ -40,4 +46,13 @@ } | ||
} | ||
function _prettify(opts) { | ||
const { styleManager, printCode } = opts; | ||
function initOptionsWithDefaults(options) { | ||
var _a, _b; | ||
const location = (_a = options.location) !== null && _a !== void 0 ? _a : (options.environment === 'node'); | ||
const bigNumbers = location && | ||
((_b = options.bigNumbers) !== null && _b !== void 0 ? _b : (options.environment === 'node')); | ||
const printCode = makePrintCode(location, options.styleManager.support, options.printCode); | ||
return { ...options, location, bigNumbers, printCode }; | ||
} | ||
function _prettify(_opts) { | ||
const opts = initOptionsWithDefaults(_opts); | ||
const { styleManager, printCode, bigNumbers } = opts; | ||
const errors = mergeTypeErrors(ensureArray(opts.errors)); | ||
@@ -60,3 +75,3 @@ if (errors.length === 0) | ||
const errorLines = prettifyOne(context).split("\n"); | ||
if (errors.length === 1) | ||
if (!bigNumbers || errors.length === 1) | ||
return errorLines.join("\n"); | ||
@@ -63,0 +78,0 @@ return preparedText.printAsPrefix(index + 1, errorLines, { separator: ' ' }) |
{ | ||
"name": "awesome-ajv-errors", | ||
"version": "3.0.0", | ||
"version": "4.0.0", | ||
"description": "Prettified AJV errors", | ||
@@ -11,22 +11,4 @@ "author": "Gustaf Räntilä", | ||
"homepage": "https://github.com/grantila/awesome-ajv-errors#readme", | ||
"main": "./dist/index-node.js", | ||
"browser": "./dist/index-browser.js", | ||
"exports": { | ||
".": "./dist/index-node.js", | ||
"./node": "./dist/index-node.js", | ||
"./browser": "./dist/index-browser.js", | ||
"./try-styled": "./dist/index-try-styled.js" | ||
}, | ||
"typesVersions": { | ||
"*": { | ||
"node": [ | ||
"./dist/index-node.d.ts" | ||
], | ||
"browser": [ | ||
"./dist/index-browser.d.ts" | ||
], | ||
"try-styled": [ | ||
"./dist/index-try-styled.d.ts" | ||
] | ||
} | ||
}, | ||
"types": "./dist/index-node.d.ts", | ||
@@ -33,0 +15,0 @@ "type": "module", |
@@ -31,3 +31,10 @@ [![npm version][npm-image]][npm-url] | ||
* Both of these will fallback to non-colored non-codeframe output if e.g. loading `@babel/code-frame` failed. This will likely be entirely resolved once Babel 8 is released; then awesome output will by default work in browsers too. | ||
* Since version 4; | ||
* package.json exports field is not support well by e.g. Jest, so v4 reverts the v3 exports. | ||
* Until the exports field get better support, the official way to import will now be from: | ||
* `awesome-ajv-errors` for auto-detecting node vs browser | ||
* `awesome-ajv-errors/dist/index-node.js` or `awesome-ajv-errors/dist/index-browser.js` for explicit importing depending on environment | ||
* `awesome-ajv-errors/dist/index-try-styled.js` for dynamic trying to load color support (e.g. in browsers) | ||
# Examples | ||
@@ -644,2 +651,23 @@ | ||
## Configure styling | ||
Instead of auto-detecting based on the platform (Node.js or a browser), you can turn on/off colors, location printing (the json-snippet of the error) and whether to print big ascii numbers to the left of each error, if there are more than one error. | ||
With the options object containing `data` provided to `prettify` you can include `colors`, `location` and `bigNumbers` as booleans, to override the defaults. | ||
Turning colors explicitly on will only enable colors if it's detected to be supported by the platform, but turning them off will always output non-colored text. | ||
Turning location on will also only enable the location printing if *colors* are detected to be supported by the underlying platform (this is a limitation in the current `@babel/code-frame` and will likely be resolved in Babel 8). | ||
`bigNumbers` will only be enabled if location printing is enabled, but can be explicitly turned off. | ||
Example: | ||
```ts | ||
const colors = false; | ||
const location = false; | ||
const explanation = prettify( validate, { data, colors, location } ); | ||
``` | ||
[npm-image]: https://img.shields.io/npm/v/awesome-ajv-errors.svg | ||
@@ -646,0 +674,0 @@ [npm-url]: https://npmjs.org/package/awesome-ajv-errors |
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
70938
1268
683