zod-prisma-types icon indicating copy to clipboard operation
zod-prisma-types copied to clipboard

Support of i18n for validation error messages

Open cimchd opened this issue 1 year ago • 2 comments

First of all: Great work, zod-prisma-types really fits perfect in our project setup. :-)

We are facing the issue, that we want to internationalize our application and we only found a way to create custom error messages with zod-prisma-types on a field level.

We would like to replace all of the zod default error messages on a project level at once (see: https://github.com/aiji42/zod-i18n). Did we miss something and is this already possible?

If not it would be perfect if we could get an option to extend the root zod instance. For i18n we want to do something like this:

import { zodI18nMap } from "zod-i18n-map";

function extendZod(z) {
  z.setErrorMap(zodI18nMap);
}

cimchd avatar May 17 '23 05:05 cimchd

@cimchd this seems like a cool addition but I don't really have experience with i18n or the package you mentiond so I need to dig into your link a bit. Could you also help me out with some examples on how this could/should look like for a sample prisma schema?

chrishoermann avatar May 25 '23 19:05 chrishoermann

@cimchd this seems like a cool addition but I don't really have experience with i18n or the package you mentiond so I need to dig into your link a bit. Could you also help me out with some examples on how this could/should look like for a sample prisma schema?

Sure :-)

The question is how the api should look like. It needs to be able to run setErrorMap on the zod object. Here are some options:

  1. If we want to add a generator option only for the zod-i18n-map-package you could enable it by a special option like:
generator zod {
  provider           = "zod-prisma-types"
  enableI18nErrorMap = true
}
  1. A more general option could be to allow any import for an error mapping:
/// @zod.import(["import { zodI18nMap} from 'zod-i18n-map';"])
generator zod {
  provider            = "zod-prisma-types"
  customErrorMap      = "zodI18nMap"
}
  1. And even more general could be to allow extending the zod instance
generator zod {
  provider         = "zod-prisma-types"
  extendZod        = "extend-zod.js"
}

Where extendZod expects a script that exports an extendZod function:

// extend-zod.js
import { zodI18nMap } from "zod-i18n-map";

export function extendZod(z) {
  z.setErrorMap(zodI18nMap);
}

cimchd avatar May 25 '23 19:05 cimchd