jquense / yup

Dead simple Object schema validation
MIT License
22.93k stars 934 forks source link

Improve addMethod() documentation #2160

Closed tillsanders closed 10 months ago

tillsanders commented 10 months ago

Not a question, but a suggestion. I struggled to use the addMethod() function. I think there are two things that could be improved in the documentation that would have helped me:

  1. Show the import statement when declaring the types, here: https://github.com/jquense/yup?tab=readme-ov-file#extending-built-in-schema-with-new-methods. E.g. I used: import { type StringSchema } from 'yup'. Without this import, the types were broken, but I didn't get any helpful feedback from TypeScript, so it took me a while to understand the problem.
  2. Add another example. The current example shows how to format a string. It would be nice to also have an example showing how to validate something – see below.
import { isValidBIC } from "ibantools";

addMethod(yup.string, 'bic', function bic(message: string) {
  return this.test({
    message,
    name: "bic",
    test(value: Maybe<string>): boolean | ValidationError {
      if (typeof value !== "string" || isValidBIC(value)) {
        return true;
      }

      return this.createError({
        path: this.path,
        message,
        params: {
          customKey: "string.bic",
        },
      });
    },
  });
});

Also – Thank you for your work!

jquense commented 10 months ago

Happy to take PRs improving the docs 👍 !