Skip to content

Getting instancePath when using with ajv-keywords and uniqueItemProperties #145

Open
@brandondurham

Description

@brandondurham

When using alongside ajv-keywords and uniqueItemProperties, how do I get the instancePath of the erroneous record?

My code:

import Ajv from 'ajv';
import addFormats from 'ajv-formats';
import ajvErrors from 'ajv-errors';
import ajvKeywords from 'ajv-keywords';

const ajv = new Ajv({ allErrors: true });
ajvKeywords(ajv, ['uniqueItemProperties']);
addFormats(ajv, { formats: ['date', 'email'], keywords: true });
ajvErrors(ajv);

… and my schema:

export const employeeSchema = {
	errorMessage: {
		uniqueItemProperties: 'What’s going on?',
	},
	items: {
		properties: {
			firstName: {
				errorMessage: { type: 'required; and must be a string' },
				type: 'string',
			},
			lastName: {
				errorMessage: { type: 'required; and must be a string' },
				type: 'string',
			},
			email: {
				format: 'email',
				type: 'string',
			},
		},
		required: [
			'email',
			'firstName',
			'lastName',
		],
		type: 'object',
	},
	type: 'array',
	uniqueItemProperties: ['email'],
};

And when running against the following data:

[
    {
        email: '[email protected]',
        firstName: 2,
        lastName: 'Hachard',
    },
    {
        email: '[email protected]',
        firstName: 'Me',
        lastName: 'Hachard',
    },
]

… I get the following errors reported:

[
    {
        "instancePath": "",
        "schemaPath": "#/errorMessage",
        "keyword": "errorMessage",
        "params": {
            "errors": [
                {
                    "instancePath": "",
                    "schemaPath": "#/uniqueItemProperties",
                    "keyword": "uniqueItemProperties",
                    "params": {},
                    "message": "must pass \"uniqueItemProperties\" keyword validation",
                    "emUsed": true
                }
            ]
        },
        "message": "What’s going on?"
    }
]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions