Skip to content

Feature Request: Allow set field schema refinement is associated with #158

@chrchrd

Description

@chrchrd

Hi,

Given the following schema:

import * as S from 'sury'

const SSchema = S.schema({
  username: S.string,
  password: S.string,
  confirm_password: S.string,
}).with(S.refine, (value, s) => {
  if (value.password !== value.confirm_password) {
    s.fail('Password does not match')
  }
})
type SSchema = S.Output<typeof SSchema>

I would like to be able associate the S.refine with a particular field of the schema (such as the confirm_password field) so that the error message would be displayed next to that form field. I have not figured out a way to do that with your library.

Other libraries that implement the Standard Schema interface, such as Zod and Valibot, allow me to set the field/path the schema refinements are associated with....

import * as z from 'zod/mini'

const ZSchema = z.object({
  username: z.string(),
  password: z.string(),
  confirm_password: z.string(),
}).check(z.refine(data => data.password === data.confirm_password, {
  message: 'Password does not match',
  path: ['confirm_password'], // associate refinement with this field
}))
type ZSchema = z.output<typeof ZSchema>

...so that the error message will be displayed next to that form field.

Maybe the S.fail function can be modified to accept an object with message and path fields like Zod, rather than just accepting a string.

May you consider adding this feature? Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions