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

Required Keyword Error Message - Error is raised at incorrect level #148

Open
james-portelli-cko opened this issue Feb 10, 2023 · 3 comments

Comments

@james-portelli-cko
Copy link

Hi,

I came across something strange when using ajv and ajv errors along with react hook form.

Codesandbox -> https://codesandbox.io/s/epic-golick-q7opzj

Steps to reproduce:

  1. Click in and out of the account number to see the errors being printed to the console.
  2. Toggle the errorMessage within account_detail (lines 40-42) to see the difference in behaviour on how the errors are raised.

errorMessage at root level
You can see in this image the error is raised at the root level (account_details) with the overridden required error message as specified in the errorMessage on the schema.
image

errorMessage at root level is commented out
You can see in this image the error is raised at the lowest possible level (the property itself account_details.account_type) with the standard required error message from ajv.
image

Is this behaviour intended? It makes it difficult to override the errorMessage for 'required' fields and correlate them to the correct property on the schema given that they are being raised at the root level.

Looking forward to hearing from you.

Thanks
James

@manuganji
Copy link

manuganji commented Sep 22, 2023

I think the schemaPath in the generated error message is wrong. It's at the root level when it should be at the property level.

@manuganji
Copy link

You need to use keepErrors: false, add ajv-errors to ajv.

@manuganji
Copy link

I'm using this snippet to make it work correctly with JsonForms. Hope this helps you:

import type { ErrorObject } from "ajv";

function transformError(error: ErrorObject): ErrorObject {
	if (error.params.errors[0].keyword === "required") {
		// console.log(error);
		const newError = structuredClone(error);
		newError.params = error.params.errors[0].params;
		newError.keyword = "required";
		return newError;
	}
	return error;
}

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