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

feat(app): create better custom error message from the provided input #494

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ravvi-kumar
Copy link

related discussion #361

@fabian-hiller
Copy link
Owner

fabian-hiller commented Mar 24, 2024

Please explain this PR. With the changes we recently added in #397, you can access the issue and therefore the current value. You should have everything you need to create rich error messages.

@fabian-hiller fabian-hiller self-assigned this Mar 24, 2024
@fabian-hiller fabian-hiller added the question Further information is requested label Mar 24, 2024
@ravvi-kumar
Copy link
Author

Hi @fabian-hiller,
i checked the changes in the PR that you mentioned, but i couldn't find the solution i was looking for. can you help me out if i am missing anything. my goal is to achieve this scenerio which is using zod's refine method to create a custom error message which returns a clear message including the input from the data provided in the runtime. here is the code example : -

.refine(
        (val) => {
          const options = val.options.map((v) => v.value);
          return options.includes(val.initial_value);
        },
        (val) => ({
          message: 'Scope: initial_value "${val.initial_value}" must exist in options',
        })
      )

i am trying to acheive the same with the custom method, but the current blocked that i am facing is that i am unable to include the provided input along with the error message , example : -

custom((val) =>
                        val.options.map((v) => v.value).includes(val.initial_value)
                        // #TODO : return better error message
                        // `Type: initial_value "${val.initial_value}" must exist in options`
                    ),

i have done some changes in the "custom" function and made it to accept a third optional parameter which will modify the error message with the provided input, it won't effect anywhere else. and i have also added a test case .
hope this test case might bring clear explaination of what i am trying to acheive .

  test('should return custom error message including input value', () => {
    const val = -1;
    const error = `provided value is : ${val}`;
    const validate = custom<number>((input) => input > 0, undefined, (input) => `provided value is : ${input}`);
    expect(validate._parse(val).issues?.[0].context.message).toBe(error);
  })

please do let me know if there is something that i am missing, thanks.

@fabian-hiller
Copy link
Owner

With the latest version you should be able to write the following. Try it out in our playground. I am currently rewriting Valibot and improving the types. I expect that // @ts-ignore (or any other workaround) will not be necessary in the next version.

import * as v from 'valibot';

const Schema = v.object(
  {
    initial: v.string(),
    options: v.array(v.string())
  },
  [
    v.custom(
      (input) => input.options.includes(input.initial),
      // @ts-ignore
      (issue) => `Scope: initial_value "${issue.input.initial}" must exist in options`
    )
  ]
);

@ravvi-kumar
Copy link
Author

@fabian-hiller ,
thanks it worked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants