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

Cannot check for existing errors on field in CustomValidate #4284

Open
4 tasks done
TheOneTheOnlyJJ opened this issue Aug 30, 2024 · 4 comments
Open
4 tasks done

Cannot check for existing errors on field in CustomValidate #4284

TheOneTheOnlyJJ opened this issue Aug 30, 2024 · 4 comments

Comments

@TheOneTheOnlyJJ
Copy link

Prerequisites

What theme are you using?

core

Version

5.20.0

Current Behavior

I have a username field in my form that I want to invalidate if it is not unique. I check its uniqueness with an API call, which always runs in my CustomValidate function. However, it would be more efficient only to run it if there are no other prior errors on that field (I have a pattern in the JSON schema that disallows certain characters). I am trying to do a simple if based on the errors.username.__errors variable, but no matter in which configuration I write the condition, I can only get it to always be true or never be true, regardless of the presence of other errors.

Expected Behavior

I expect a check such as if (!errors.username.__errors || errors.username.__errors.length === 0) to be true if there are no errors associated to the username field, and false if there is at least one.

Steps To Reproduce

No response

Environment

- OS: Windows 10
- Node: 20.15.0
- npm: 10.8.2

Anything else?

No response

@TheOneTheOnlyJJ TheOneTheOnlyJJ added bug needs triage Initial label given, to be assigned correct labels and assigned labels Aug 30, 2024
@nickgros
Copy link
Contributor

@TheOneTheOnlyJJ Could you create a CodeSandbox reproduction that demonstrates that __errors is not the value you would expect?

@nickgros nickgros added awaiting response needs reproducible example Missing a link to a reproduction in the playground, CodeSandbox, JSFiddle, etc. and removed needs triage Initial label given, to be assigned correct labels and assigned labels Aug 30, 2024
@TheOneTheOnlyJJ
Copy link
Author

Sure, here you go: https://codesandbox.io/p/sandbox/rjsf-field-errors-3ndqph
Observe the errors when inputting a valid string according to the specified pattern, and then an invalid one. For simplicity, I changed the pattern to allow lowercase ASCII letters only. I have played around with the if condition, but I cannot get it to work.

@TheOneTheOnlyJJ
Copy link
Author

@nickgros
Is the example I provided not satisfactory? Can the "needs reproducible example" label be removed now?

@nickgros
Copy link
Contributor

@TheOneTheOnlyJJ sorry for the delay. I took a closer look and discovered that we don't pass existing errors to customValidate, so you cannot use that to add rules based on other errors. We run the rules through the validator, then we run customValidate, and then we merge the two.

You might be able to use the transformErrors function, with something like this:

import { ErrorTransformer } from "@rjsf/utils";


const transformErrors: ErrorTransformer = (errors) => {
  // Skip if no form data or errors
  let hasErrorInUserName = false;
  errors.forEach((e) => {
    if (e.property === ".username") {
      hasErrorInUserName = true;
    }
  });

  if (!hasErrorInUserName) {
    // Synchronous API call...
    errors.push({
      name: "Username taken",
      stack: ".username",
      message: "Username exists in system",
    });
  }

  return errors;
};

@nickgros nickgros removed the needs reproducible example Missing a link to a reproduction in the playground, CodeSandbox, JSFiddle, etc. label Sep 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants