seasonedcc / composable-functions

Types and functions to make composition easy and safe
MIT License
653 stars 13 forks source link

errorMessagesForSchema function cannot get error messages for nested schema fields #24

Closed correiarmjoao closed 2 years ago

correiarmjoao commented 2 years ago

What version 0.3.0

What happend errorMessagesForSchema function cannot get error messages for nested schema fields since errorMessageFor only checks the schema keys against the joined path.

https://github.com/SeasonedSoftware/remix-domains/blob/e2288f0d3885bdcd0d10df575f4974f20186014a/src/errors.ts#L22-L38

Steps to reproduce

const data = {
  a: "bar",
  b: "foo",
  c: {
    c1: "c1 foo",
    c2: "c2 bar"
  },
  d: {
    d1: "d1 foo",
    d2: "d2 bar"
  }
}

const schema = z.object({
  a: z.string(),
  b: z.string(),
  c: z.object({
    c1: z.string(),
    c2: z.array(z.string()),
  }),
  d: z.object({
    d1: z.object({
      d1a: z.string(),
      d1b: z.number()
    }),
    d2: z.array(z.string()),
  })
});

const domainFn = makeDomainFunction(schema)(async (data) => {
  return data
})

export const loader = async () => {
  const result = await domainFn(data)
  return errorMessagesForSchema(result.inputErrors, schema)
}

domainFn return

{
  "success": false,
  "errors": [],
  "inputErrors": [
    {
      "path": [ "c", "c2" ],
      "message": "Expected array, received string"
    },
    {
      "path": [ "d", "d1" ],
      "message": "Expected object, received string"
    },
    {
      "path": [ "d", "d2" ],
      "message": "Expected array, received string"
    }
  ],
  "environmentErrors": []
}

errorMessagesForSchema return

{ a: [], b: [], c: [], d: [] }
diogob commented 2 years ago

@correiarmjoao thanks for the detailed steps to reproduce. I'm looking into this

gustavoguichard commented 2 years ago

Hey, @correiarmjoao ! I believe the v0.3.1 should fix this issue.

Let us know if the problem persists