arktypea
arktype5mo ago
afonso

Multiple error messages

I'm trying to migrate a zod schema to arktype. This is the zod schema I have:
import { z } from 'zod'

export const createPasswordSchema = z
  .object({
    // According to the SOC 2 Logical Access policy, the password must meet the following requirements:
    // Length: At least 12 characters.
    // Complexity: Must include a mix of uppercase and lowercase letters, numbers, and symbols.
    password: z
      .string()
      // 1. At least 12 characters
      .min(12, 'length')
      // 2. At least one lowercase letter
      .regex(/[a-z]/, 'lowercase')
      // 3. At least one uppercase letter
      .regex(/[A-Z]/, 'uppercase')
      // 4. At least one digit
      .regex(/\d/, 'number')
      // 5. At least one symbol
      .regex(/[^A-Za-z0-9]/, 'special'),

    confirmation: z.string(),
  })
  // 6. Match with confirmation
  .refine(({ password, confirmation }) => password === confirmation, {
    message: 'noMatch',
  })

If any of these checks fail, Superforms will provide me with a $allErrors for the password field that will be an array like ['lenght', 'uppercase'], which in turn I can use in my i18n layer.

I'm trying to achieve the same behaviour with Arktype, but I can't have a different error being reported, or even trim the error message down to a word, for my i18n layer.
This is what I have so far, which I know it does not work:
export const createPasswordSchema = type({
  password: type.string
    .atLeastLength(12)
    .describe('length')
    .matching(/[a-z]/)
    .describe('lowercase')
    .matching(/[A-Z]/)
    .describe('uppercase')
    .matching(/\d/)
    .describe('number')
    .matching(/[^A-Za-z0-9]/)
    .describe('special'),
  confirmation: type.string,
}).narrow(({ password, confirmation }, context) => {
  if (password !== confirmation) {
    return context.reject('noMatch')
  }
  return true
})

any thoughts on how I can achieve this?
Was this page helpful?