arktype: localization support: problem message parser

Request a feature

The maintainers of ArkType will do our best to provide prompt feedback for any feature requests- especially those that are concise and persuasive!

🤷 Motivation

What problem are you having?

The Problem class only expose the formatted message and not the other properties, meaning the message cannot be localized into other languages.

Why should we prioritize solving it?

Any application supporting i18n probably need this support.

šŸ’” Solution

How do you think we should solve the problem?

There are a few ways to do it.

One way is to simplify the Problem class containing only the data, while there is a overridable problem parser that parse the problem into message.

There are other ways such as accepting a problem factory, exposing the problem data so that application can create their own message parser using the resulting problem instances, etc.

I think the parser approach is probably the best as it properly separate the concerns between problem reporter and message parsing.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 21 (9 by maintainers)

Most upvoted comments

@unional I actually think that data was always exposed, although ArkError does need some documentation: https://github.com/arktypeio/arktype/issues/1273

I will close this for now in favor of this discussion for additional feedback on how we can best support i18n internally.

My current recommendation would be to map over ArkErrors, each of which has context on the individual error, and transform those to localized messages directly.

I’m interested in further improvements here but I don’t have a clear idea of the right API for that yet and would need to do more research on popular i18n libraries.

The problem now is that in zod, we can specify each type’s error message easily like

z.object({
  email: z
    .string({
      required_error: t("tips.field_required"),
      invalid_type_error: t("invalid_value"),
    })
    .email(t("msgs.invalid_email"))
    .min(1, t("msgs.incorrect_length"))
    .max(64, t("msgs.incorrect_length"))
})

But in ArkType, I did now see an easy approach, managing error messages is quite complicated.

For me that’s a major reason that stopped me from using ArkType. I respect it provides both ā€œtextā€ and ā€œfluentā€ method to manage type, meanwhile the shortage of managing ā€œoptionā€ or what say ā€œdetailā€ of each type is a bit confusing. From this perspective I think zod did a better job.

In other words, this might not be a ā€œshortageā€ of ArkType, it’s that when comparing between ArkType and zod, they actually gives different design.

When it comes to zod, it’s actually acceptable to either specify error messages in type’s option or manage i18n globally (I’m a bit annoyed by how I need to set every localized error messages in EVERY type, even though they are almost the same so I’m desperately wanting for a new alternative).

But in ArkType, your type-specifying design only allow you to manage a global localization thing, since every type, if specified in ā€œtextā€ method, as far as I see there’s no concept of ā€œoptionsā€. So in this situation, the best solution I can imagine is to figure out a way to manage i18n like date-dns does, the only change is to configure it via global option.

Hi, sorry missed that message @ssalbdivad.

Could you link it to me if it’s open source or otherwise provide a bit of additional context in the example, e.g. what the input/output types are?

Please don’t worry about it. It’s not open source and I’m just trying to figure out how to do it myself too.

@unional Whoops, my bad! Not so useful if you can’t access themšŸ˜…

I’ll be sure those are exposed for the next release, and the rest of this gives me good context to consider for some other details of this problem. I’ll keep this issue open until then to ensure I follow through with that!

I think the data and source are private atm: https://github.com/arktypeio/arktype/blob/main/src/traverse/problems.ts#L42-L43

Yes, with that, it will support the exposing the problem data so that application can create their own message parser using the resulting problem instances scenario.

i.e. something like:


export function Component() {
  return <SomeField validate={(v, fields) => {
     const { problems } = someFieldValidator(v)
     if (problems.length > 0) {
       return i18nProblems({ name: 'someField', fields }, problems)
     }
  }}/>
}


function i18nProblems(field: FieldInfo, problems: Problem[]) {
  return problems.map(p => i18nProblem(field, p))
}

function i18nProblem(field: FieldInfo, problem: Problem) {
  switch (problem.code) {
    case 'required':
      return intl.formatMessage(
        { defaultMessage: `{name) is required` },
        field
      )
    case 'min':
      return intl.formatMessage(
        { defaultMessage: `{name) must be more than {min}` },
        { name: field.name, min: problem.data.min }
      )
    ...
  }
}