awesome-ajv-errors
Advanced tools
Comparing version
import { codeFrameColumns } from '@babel/code-frame'; | ||
import { getLocation } from 'jsonpos'; | ||
export const printCode = (message, parsedJson, { dataPath, markIdentifier, linesAbove = 5, linesBelow = 3, colors, }) => { | ||
const { start, end } = getLocation(parsedJson, { dataPath, markIdentifier }); | ||
export const printCode = (message, parsedJson, { path, markIdentifier, linesAbove = 5, linesBelow = 3, colors, }) => { | ||
const { start, end } = getLocation(parsedJson, { path, markIdentifier }); | ||
if (!start) | ||
return `{The path ${dataPath} cannot be found in json}`; | ||
return `{The path ${path} cannot be found in json}`; | ||
return codeFrameColumns(parsedJson.jsonString, { start, end }, { | ||
@@ -8,0 +8,0 @@ highlightCode: colors, |
import type { LocationOptions, ParsedJson } from 'jsonpos'; | ||
export interface CodeOptions extends LocationOptions { | ||
export interface CodeLocationOptions extends Pick<LocationOptions, 'markIdentifier'>, Required<Pick<LocationOptions, 'path'>> { | ||
} | ||
export interface CodeOptions extends CodeLocationOptions { | ||
linesAbove?: number; | ||
@@ -4,0 +6,0 @@ linesBelow?: number; |
@@ -7,6 +7,5 @@ import { ValidationError } from "./types.js"; | ||
export interface DataPath { | ||
orig: string; | ||
dotOnly: string; | ||
path: Array<DataPathItem>; | ||
simplePath: Array<string>; | ||
} | ||
export declare function parseDataPath(error: ValidationError): DataPath; |
@@ -0,20 +1,20 @@ | ||
import { parsePath } from 'jsonpos'; | ||
export function parseDataPath(error) { | ||
var _a; | ||
// Ajv 6 and 7 differ. In 6 the dataPath separator is `.`, in 7 it's `/`. | ||
// Since Ajv 8, it's called "instancePath" | ||
const value = (_a = error.dataPath) !== null && _a !== void 0 ? _a : error.instancePath; | ||
if (value === '') | ||
return { orig: value, dotOnly: '.', path: [] }; | ||
// Ajv 6 and 7 differ. In 6 the path separator is `.`, in 7 it's `/`. | ||
const sep = value.charAt(0); | ||
const path = [].concat(...value | ||
.split(sep) | ||
.map(entry => entry | ||
.split(/\[|\]/g) | ||
.map((key, index) => ({ | ||
key, | ||
type: index === 0 ? 'string' : 'number' | ||
})))) | ||
var _a, _b; | ||
if (!((_a = error.dataPath) !== null && _a !== void 0 ? _a : error.instancePath)) | ||
return { path: [], simplePath: [] }; | ||
const value = !!error.instancePath | ||
? parsePath({ pointerPath: error.instancePath }) | ||
: ((_b = error.dataPath) === null || _b === void 0 ? void 0 : _b.charAt(0)) === '.' | ||
? parsePath({ dotPath: error.dataPath }) | ||
: parsePath({ pointerPath: error.dataPath }); | ||
const path = value | ||
.map((entry) => typeof entry === 'number' | ||
? ({ key: `${entry}`, type: 'number' }) | ||
: ({ key: entry, type: 'string' })) | ||
.filter(val => val.key !== ''); | ||
const dotOnly = '.' + path.map(({ key }) => key).join('.'); | ||
return { orig: value, dotOnly, path }; | ||
const simplePath = path.map(({ key }) => key); | ||
return { path, simplePath }; | ||
} |
import { PrettifyContext } from './types.js'; | ||
export declare function getValueByPath(context: PrettifyContext, path?: string): any; | ||
export declare function getValueByPath(context: PrettifyContext, path?: string[]): any; |
import * as pointer from 'jsonpointer'; | ||
export function getValueByPath(context, path = context.dataPath.dotOnly) { | ||
export function getValueByPath(context, path = context.dataPath.simplePath) { | ||
if (context.data === null || typeof context.data !== 'object') | ||
return context.data; | ||
return pointer.get(context.data, path.replace(/\./g, '/')); | ||
return pointer.get(context.data, encodeJsonPointerPath(path)); | ||
} | ||
function encodeJsonPointerPath(path) { | ||
return '/' + | ||
path | ||
.map(segment => encodeJsonPointerSegment(segment)) | ||
.join('/'); | ||
} | ||
function encodeJsonPointerSegment(segment) { | ||
return segment.replace(/~/g, '~0').replace(/\//g, '~1'); | ||
} |
@@ -15,3 +15,3 @@ import { getTypedContext } from "../../types.js"; | ||
style.pathDescription(`"${additionalProperty}"`), context.parsedJson, { | ||
dataPath: dataPath.dotOnly + "." + additionalProperty, | ||
path: [...dataPath.simplePath, additionalProperty], | ||
markIdentifier: true, | ||
@@ -18,0 +18,0 @@ }); |
@@ -32,4 +32,4 @@ import { getTypedValue, getTypedContext, } from "../../types.js"; | ||
' (as ' + style.pathDescription(suggestion.type) + | ||
') perhaps?'), context.parsedJson, { dataPath: dataPath.dotOnly, markIdentifier: false }); | ||
') perhaps?'), context.parsedJson, { path: dataPath.simplePath, markIdentifier: false }); | ||
return { title, codeFrame }; | ||
} |
@@ -27,4 +27,4 @@ import { getTypedContext } from "../../types.js"; | ||
style.title(plural(' more item')); | ||
const codeFrame = printCode(shortMessage, context.parsedJson, { dataPath: dataPath.dotOnly, markIdentifier: false }); | ||
const codeFrame = printCode(shortMessage, context.parsedJson, { path: dataPath.simplePath, markIdentifier: false }); | ||
return { title, codeFrame }; | ||
} |
@@ -20,4 +20,4 @@ import { getTypedContext } from "../../types.js"; | ||
validStatement; | ||
const codeFrame = printCode('Ensure this is ' + validStatement, context.parsedJson, { dataPath: dataPath.dotOnly, markIdentifier: false }); | ||
const codeFrame = printCode('Ensure this is ' + validStatement, context.parsedJson, { path: dataPath.simplePath, markIdentifier: false }); | ||
return { title, codeFrame }; | ||
} |
@@ -22,4 +22,4 @@ import { getTypedValue, getTypedContext, } from "../../types.js"; | ||
(!suggestionResult.best ? '' : | ||
' e.g. ' + formatBestSuggestion(suggestionResult, context)))), context.parsedJson, { dataPath: dataPath.dotOnly, markIdentifier: false }); | ||
' e.g. ' + formatBestSuggestion(suggestionResult, context)))), context.parsedJson, { path: dataPath.simplePath, markIdentifier: false }); | ||
return { title, codeFrame }; | ||
} |
@@ -22,3 +22,3 @@ import { getTypedContext } from "../../types.js"; | ||
? 'valid ' + link(linkInfo.title, linkInfo.url) | ||
: 'a valid ' + style.pathDescription(format)), context.parsedJson, { dataPath: dataPath.dotOnly, markIdentifier: false }); | ||
: 'a valid ' + style.pathDescription(format)), context.parsedJson, { path: dataPath.simplePath, markIdentifier: false }); | ||
return { title, codeFrame }; | ||
@@ -25,0 +25,0 @@ } |
@@ -16,4 +16,4 @@ import { getTypedContext } from "../../types.js"; | ||
style.expr(failingKeyword) + | ||
' clause is satisfied', context.parsedJson, { dataPath: dataPath.dotOnly, markIdentifier: false }); | ||
' clause is satisfied', context.parsedJson, { path: dataPath.simplePath, markIdentifier: false }); | ||
return { title, codeFrame }; | ||
} |
@@ -17,4 +17,4 @@ import { getTypedContext } from "../../types.js"; | ||
style.title(')'); | ||
const codeFrame = printCode('Ensure this is a ' + validStatement, context.parsedJson, { dataPath: dataPath.dotOnly, markIdentifier: false }); | ||
const codeFrame = printCode('Ensure this is a ' + validStatement, context.parsedJson, { path: dataPath.simplePath, markIdentifier: false }); | ||
return { title, codeFrame }; | ||
} |
@@ -9,4 +9,4 @@ import { getTypedContext } from "../../types.js"; | ||
style.regex(pattern); | ||
const codeFrame = printCode('Ensure this matches the regex pattern', context.parsedJson, { dataPath: dataPath.dotOnly, markIdentifier: false }); | ||
const codeFrame = printCode('Ensure this matches the regex pattern', context.parsedJson, { path: dataPath.simplePath, markIdentifier: false }); | ||
return { title, codeFrame }; | ||
} |
@@ -10,4 +10,4 @@ import { getTypedContext } from "../../types.js"; | ||
const codeFrame = printCode('add missing property ' + | ||
style.pathDescription(missingProperty), context.parsedJson, { dataPath: dataPath.dotOnly, markIdentifier: false }); | ||
style.pathDescription(missingProperty), context.parsedJson, { path: dataPath.simplePath, markIdentifier: false }); | ||
return { title, codeFrame }; | ||
} |
@@ -17,4 +17,4 @@ import { getTypedContext } from "../../types.js"; | ||
validStatement; | ||
const codeFrame = printCode('Ensure this is ' + validStatement, context.parsedJson, { dataPath: dataPath.dotOnly, markIdentifier: false }); | ||
const codeFrame = printCode('Ensure this is ' + validStatement, context.parsedJson, { path: dataPath.simplePath, markIdentifier: false }); | ||
return { title, codeFrame }; | ||
} |
@@ -19,4 +19,4 @@ import { suggestAnotherType, getTypedContext, } from "../../types.js"; | ||
? ' with e.g. ' + suggestion | ||
: ''), context.parsedJson, { dataPath: dataPath.dotOnly, markIdentifier: false }); | ||
: ''), context.parsedJson, { path: dataPath.simplePath, markIdentifier: false }); | ||
return { title, codeFrame }; | ||
} |
@@ -6,3 +6,3 @@ import { getTypedContext } from "../../types.js"; | ||
const [a, b] = [i, j].sort(); | ||
const valueA = getValueByPath(context, dataPath.dotOnly + `.${a}`); | ||
const valueA = getValueByPath(context, [...dataPath.simplePath, `${a}`]); | ||
const [prePath, pathExpr, postPath] = pathDescription(context, 'array'); | ||
@@ -20,4 +20,4 @@ const title = style.title(`The ${prePath}`) + | ||
' or ' + | ||
style.number(`${b}`), context.parsedJson, { dataPath: dataPath.dotOnly, markIdentifier: false }); | ||
style.number(`${b}`), context.parsedJson, { path: dataPath.simplePath, markIdentifier: false }); | ||
return { title, codeFrame }; | ||
} |
@@ -8,4 +8,4 @@ export function prettify(context) { | ||
style.title(`${postPath} ${message}`); | ||
const codeFrame = printCode(message, context.parsedJson, { dataPath: dataPath.dotOnly, markIdentifier: false }); | ||
const codeFrame = printCode(message, context.parsedJson, { path: dataPath.simplePath, markIdentifier: false }); | ||
return { title, codeFrame }; | ||
} |
{ | ||
"name": "awesome-ajv-errors", | ||
"version": "4.0.1", | ||
"version": "4.1.0", | ||
"description": "Prettified AJV errors", | ||
@@ -61,6 +61,6 @@ "author": "Gustaf Räntilä", | ||
"dependencies": { | ||
"@babel/code-frame": "^7.16.0", | ||
"@babel/code-frame": "^7.16.7", | ||
"chalk": "^4.1.2", | ||
"jsonpointer": "^5.0.0", | ||
"jsonpos": "^2.0.2", | ||
"jsonpos": "^3.0.1", | ||
"leven": "^4.0.0", | ||
@@ -67,0 +67,0 @@ "terminal-link": "^3.0.0" |
71517
0.82%1278
0.79%+ Added
- Removed
Updated
Updated