Skip to content

Commit

Permalink
Handle primitive values in EnumValidationError
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip committed Sep 21, 2019
1 parent 8b3471e commit 038d07c
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 50 deletions.
4 changes: 4 additions & 0 deletions .changeset/moody-days-switch/changes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"releases": [{ "name": "better-ajv-errors", "type": "patch" }],
"dependents": []
}
1 change: 1 addition & 0 deletions .changeset/moody-days-switch/changes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Handle primitive values in EnumValidationError
1 change: 1 addition & 0 deletions src/validation-errors/__fixtures__/enum-string/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"baz"
4 changes: 4 additions & 0 deletions src/validation-errors/__fixtures__/enum-string/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type": "string",
"enum": ["foo", "bar"]
}
36 changes: 33 additions & 3 deletions src/validation-errors/__tests__/__snapshots__/enum.js.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,36 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Enum prints correctly for empty value 1`] = `
exports[`Enum when value is a primitive prints correctly for empty value 1`] = `
Array [
"ENUM should be equal to one of the allowed values",
"(foo, bar)
",
"> 1 | \\"baz\\"
  | ^^^^^ 👈🏽 Did you mean bar here?",
]
`;

exports[`Enum when value is a primitive prints correctly for enum prop 1`] = `
Array [
"ENUM should be equal to one of the allowed values",
"(foo, bar)
",
"> 1 | \\"baz\\"
  | ^^^^^ 👈🏽 Did you mean bar here?",
]
`;

exports[`Enum when value is a primitive prints correctly for no levenshtein match 1`] = `
Array [
"ENUM should be equal to one of the allowed values",
"(one, two)
",
"> 1 | \\"baz\\"
  | ^^^^^ 👈🏽 Unexpected value, should be equal to one of the allowed values",
]
`;

exports[`Enum when value is an object prints correctly for empty value 1`] = `
Array [
"ENUM should be equal to one of the allowed values",
"(foo, bar)
Expand All @@ -12,7 +42,7 @@ Array [
]
`;
exports[`Enum prints correctly for enum prop 1`] = `
exports[`Enum when value is an object prints correctly for enum prop 1`] = `
Array [
"ENUM should be equal to one of the allowed values",
"(foo, bar)
Expand All @@ -24,7 +54,7 @@ Array [
]
`;
exports[`Enum prints correctly for no levenshtein match 1`] = `
exports[`Enum when value is an object prints correctly for no levenshtein match 1`] = `
Array [
"ENUM should be equal to one of the allowed values",
"(one, two)
Expand Down
154 changes: 108 additions & 46 deletions src/validation-errors/__tests__/enum.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,117 @@ import { getSchemaAndData } from '../../test-helpers';
import EnumValidationError from '../enum';

describe('Enum', () => {
let schema, data, jsonRaw, jsonAst;
beforeAll(async () => {
[schema, data] = await getSchemaAndData('enum', __dirname);
jsonRaw = JSON.stringify(data, null, 2);
jsonAst = parse(jsonRaw, { loc: true });
});
describe('when value is an object', () => {
let schema, data, jsonRaw, jsonAst;
beforeAll(async () => {
[schema, data] = await getSchemaAndData('enum', __dirname);
jsonRaw = JSON.stringify(data, null, 2);
jsonAst = parse(jsonRaw, { loc: true });
});

it('prints correctly for enum prop', () => {
const error = new EnumValidationError(
{
keyword: 'enum',
dataPath: '/id',
schemaPath: '#/enum',
params: { allowedValues: ['foo', 'bar'] },
message: `should be equal to one of the allowed values`,
},
{ data, schema, jsonRaw, jsonAst }
);

expect(error.print()).toMatchSnapshot();
});
it('prints correctly for enum prop', () => {
const error = new EnumValidationError(
{
keyword: 'enum',
dataPath: '/id',
schemaPath: '#/enum',
params: { allowedValues: ['foo', 'bar'] },
message: `should be equal to one of the allowed values`,
},
{ data, schema, jsonRaw, jsonAst }
);

expect(error.print()).toMatchSnapshot();
});

it('prints correctly for no levenshtein match', () => {
const error = new EnumValidationError(
{
keyword: 'enum',
dataPath: '/id',
schemaPath: '#/enum',
params: { allowedValues: ['one', 'two'] },
message: `should be equal to one of the allowed values`,
},
{ data, schema, jsonRaw, jsonAst }
);

expect(error.print()).toMatchSnapshot();
});

it('prints correctly for empty value', () => {
const error = new EnumValidationError(
{
keyword: 'enum',
dataPath: '/id',
schemaPath: '#/enum',
params: { allowedValues: ['foo', 'bar'] },
message: `should be equal to one of the allowed values`,
},
{ data, schema, jsonRaw, jsonAst }
);

it('prints correctly for no levenshtein match', () => {
const error = new EnumValidationError(
{
keyword: 'enum',
dataPath: '/id',
schemaPath: '#/enum',
params: { allowedValues: ['one', 'two'] },
message: `should be equal to one of the allowed values`,
},
{ data, schema, jsonRaw, jsonAst }
);

expect(error.print()).toMatchSnapshot();
expect(error.print(schema, { id: '' })).toMatchSnapshot();
});
});

it('prints correctly for empty value', () => {
const error = new EnumValidationError(
{
keyword: 'enum',
dataPath: '/id',
schemaPath: '#/enum',
params: { allowedValues: ['foo', 'bar'] },
message: `should be equal to one of the allowed values`,
},
{ data, schema, jsonRaw, jsonAst }
);

expect(error.print(schema, { id: '' })).toMatchSnapshot();
describe('when value is a primitive', () => {
let schema, data, jsonRaw, jsonAst;
beforeAll(async () => {
[schema, data] = await getSchemaAndData('enum-string', __dirname);
jsonRaw = JSON.stringify(data, null, 2);
jsonAst = parse(jsonRaw, { loc: true });
});

it('prints correctly for enum prop', () => {
const error = new EnumValidationError(
{
keyword: 'enum',
dataPath: '',
schemaPath: '#/enum',
params: {
allowedValues: ['foo', 'bar'],
},
message: 'should be equal to one of the allowed values',
},
{ data, schema, jsonRaw, jsonAst }
);

expect(error.print()).toMatchSnapshot();
});

it('prints correctly for no levenshtein match', () => {
const error = new EnumValidationError(
{
keyword: 'enum',
dataPath: '',
schemaPath: '#/enum',
params: {
allowedValues: ['one', 'two'],
},
message: 'should be equal to one of the allowed values',
},
{ data, schema, jsonRaw, jsonAst }
);

expect(error.print()).toMatchSnapshot();
});

it('prints correctly for empty value', () => {
const error = new EnumValidationError(
{
keyword: 'enum',
dataPath: '',
schemaPath: '#/enum',
params: {
allowedValues: ['foo', 'bar'],
},
message: 'should be equal to one of the allowed values',
},
{ data, schema, jsonRaw, jsonAst }
);

expect(error.print(schema, '')).toMatchSnapshot();
});
});
});
4 changes: 3 additions & 1 deletion src/validation-errors/enum.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ export default class EnumValidationError extends BaseValidationError {
dataPath,
params: { allowedValues },
} = this.options;
const currentValue = pointer.get(this.data, dataPath);

const currentValue =
dataPath === '' ? this.data : pointer.get(this.data, dataPath);

if (!currentValue) {
return null;
Expand Down

0 comments on commit 038d07c

Please sign in to comment.