diff --git a/.changeset/moody-days-switch/changes.json b/.changeset/moody-days-switch/changes.json new file mode 100644 index 00000000..f3490fa1 --- /dev/null +++ b/.changeset/moody-days-switch/changes.json @@ -0,0 +1,4 @@ +{ + "releases": [{ "name": "better-ajv-errors", "type": "patch" }], + "dependents": [] +} diff --git a/.changeset/moody-days-switch/changes.md b/.changeset/moody-days-switch/changes.md new file mode 100644 index 00000000..09043aaa --- /dev/null +++ b/.changeset/moody-days-switch/changes.md @@ -0,0 +1 @@ +Handle primitive values in EnumValidationError diff --git a/src/validation-errors/__fixtures__/enum-string/data.json b/src/validation-errors/__fixtures__/enum-string/data.json new file mode 100644 index 00000000..05e340ec --- /dev/null +++ b/src/validation-errors/__fixtures__/enum-string/data.json @@ -0,0 +1 @@ +"baz" diff --git a/src/validation-errors/__fixtures__/enum-string/schema.json b/src/validation-errors/__fixtures__/enum-string/schema.json new file mode 100644 index 00000000..d1cc9e76 --- /dev/null +++ b/src/validation-errors/__fixtures__/enum-string/schema.json @@ -0,0 +1,4 @@ +{ + "type": "string", + "enum": ["foo", "bar"] +} diff --git a/src/validation-errors/__tests__/__snapshots__/enum.js.snap b/src/validation-errors/__tests__/__snapshots__/enum.js.snap index e15d280d..860494b4 100644 --- a/src/validation-errors/__tests__/__snapshots__/enum.js.snap +++ b/src/validation-errors/__tests__/__snapshots__/enum.js.snap @@ -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) @@ -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) @@ -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) diff --git a/src/validation-errors/__tests__/enum.js b/src/validation-errors/__tests__/enum.js index b5d36ad0..92a11a2a 100644 --- a/src/validation-errors/__tests__/enum.js +++ b/src/validation-errors/__tests__/enum.js @@ -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(); + }); }); }); diff --git a/src/validation-errors/enum.js b/src/validation-errors/enum.js index a73de1d2..276b114f 100644 --- a/src/validation-errors/enum.js +++ b/src/validation-errors/enum.js @@ -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;