Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
brandondurham opened this issue Mar 29, 2022 · 1 comment

Comments

@brandondurham
Copy link

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: 'dhachardy@tamu.edu',
        firstName: 2,
        lastName: 'Hachard',
    },
    {
        email: 'dhachardy@tamu.edu',
        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?"
    }
]
@SamehHanna
Copy link

SamehHanna commented Aug 19, 2022

I have also the same problem.
@epoberezkin : Is there anyway to identify the problematic duplicated: email / id / parameter? It would be great to identify the problem in stead of searching in a whole file for which one was duplicated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants