Skip to content

Commit

Permalink
feat: support json option to get accurate line/column listings
Browse files Browse the repository at this point in the history
  • Loading branch information
ext authored and torifat committed Jan 2, 2021
1 parent 8ee8c5c commit 4e6e4c7
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 20 deletions.
4 changes: 4 additions & 0 deletions .changeset/small-kings-pump/changes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"releases": [{ "name": "better-ajv-errors", "type": "minor" }],
"dependents": []
}
1 change: 1 addition & 0 deletions .changeset/small-kings-pump/changes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support json option to get accurate line/column listings
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,14 @@ Or, use `js` if you are planning to use this with some API. Your output will loo
Type: `number` `null`
Default: `null`

If you have an unindented JSON payload and you want the error output indented
If you have an unindented JSON payload and you want the error output indented.

This option have no effect when using the `json` option.

##### json

Type: `string` `null`
Default: `null`

Raw JSON payload used when formatting codeframe.
Gives accurate line and column listings.
15 changes: 0 additions & 15 deletions src/__fixtures__/data.json

This file was deleted.

5 changes: 5 additions & 0 deletions src/__fixtures__/default/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "doc",
"version": 1,
"content": [{ "type": "paragarph" }]
}
File renamed without changes.
26 changes: 26 additions & 0 deletions src/__tests__/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Main should output error with codeframe 1`] = `
"ENUM should be equal to one of the allowed values
(paragraph, bulletList, mediaSingle, codeBlock, orderedList, heading, panel, blockquote, rule, mediaGroup, applicationCard, decisionList, taskList, table, extension, bodiedExtension, blockCard, layoutSection)
  2 |  \\"type\\": \\"doc\\",
  3 |  \\"version\\": 1,
> 4 |  \\"content\\": [{ \\"type\\": \\"paragarph\\" }]
  |  ^^^^^^^^^^^ 👈🏽 Did you mean paragraph here?
  5 | }
  6 | "
`;
exports[`Main should output error with reconstructed codeframe 1`] = `
"ENUM should be equal to one of the allowed values
(paragraph, bulletList, mediaSingle, codeBlock, orderedList, heading, panel, blockquote, rule, mediaGroup, applicationCard, decisionList, taskList, table, extension, bodiedExtension, blockCard, layoutSection)
  4 |  \\"content\\": [
  5 |  {
> 6 |  \\"type\\": \\"paragarph\\"
  |  ^^^^^^^^^^^ 👈🏽 Did you mean paragraph here?
  7 |  }
  8 |  ]
  9 | }"
`;
33 changes: 33 additions & 0 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import Ajv from 'ajv';
import { getSchemaAndData } from '../test-helpers';
import betterAjvErrors from '../';

describe('Main', () => {
it('should output error with reconstructed codeframe', async () => {
const [schema, data] = await getSchemaAndData('default', __dirname);
const ajv = new Ajv({ jsonPointers: true });
const validate = ajv.compile(schema);
const valid = validate(data);
expect(valid).toBeFalsy();

const res = betterAjvErrors(schema, data, validate.errors, {
format: 'cli',
indent: 2,
});
expect(res).toMatchSnapshot();
});

it('should output error with codeframe', async () => {
const [schema, data, json] = await getSchemaAndData('default', __dirname);
const ajv = new Ajv({ jsonPointers: true });
const validate = ajv.compile(schema);
const valid = validate(data);
expect(valid).toBeFalsy();

const res = betterAjvErrors(schema, data, validate.errors, {
format: 'cli',
json,
});
expect(res).toMatchSnapshot();
});
});
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import parse from 'json-to-ast';
import prettify from './helpers';

export default (schema, data, errors, options = {}) => {
const { format = 'cli', indent = null } = options;
const { format = 'cli', indent = null, json = null } = options;

const jsonRaw = JSON.stringify(data, null, indent);
const jsonRaw = json || JSON.stringify(data, null, indent);
const jsonAst = parse(jsonRaw, { loc: true });

const customErrorToText = error => error.print().join('\n');
Expand Down
5 changes: 3 additions & 2 deletions src/test-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ export async function getSchemaAndData(name, dirPath) {
const schemaPath = await getFixturePath(dirPath, name, 'schema.json');
const schema = JSON.parse(readFileSync(schemaPath, 'utf8'));
const dataPath = await getFixturePath(dirPath, name, 'data.json');
const data = JSON.parse(readFileSync(dataPath, 'utf8'));
const json = readFileSync(dataPath, 'utf8');
const data = JSON.parse(json);

return [schema, data];
return [schema, data, json];
}
3 changes: 3 additions & 0 deletions typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ declare namespace betterAjvErrors {
export interface IInputOptions {
format?: 'cli' | 'js';
indent?: number | null;

/** Raw JSON used when highlighting error location */
json?: string | null;
}

export interface IOutputError {
Expand Down

0 comments on commit 4e6e4c7

Please sign in to comment.