colinhacks / zod

TypeScript-first schema validation with static type inference

Home Page:https://zod.dev

Repository from Github https://github.comcolinhacks/zodRepository from Github https://github.comcolinhacks/zod

Possible BUG: Set of string URLs fails

patrickReiis opened this issue · comments

This code:

z.set(z.string().url()).parse(new Set('https://mint.coinos.io'))

Is failing, however, in theory it should be correct, since the schema is "Set of URLs".

Error message:

Uncaught ZodError: [
  {
    "validation": "url",
    "code": "invalid_string",
    "message": "Invalid url",
    "path": [
      0
    ]
  },
  {
    "validation": "url",
    "code": "invalid_string",
    "message": "Invalid url",
    "path": [
      1
    ]
  },
  {
    "validation": "url",
    "code": "invalid_string",
    "message": "Invalid url",
    "path": [
      2
    ]
  },
  {
    "validation": "url",
    "code": "invalid_string",
    "message": "Invalid url",
    "path": [
      3
    ]
  },
  {
    "validation": "url",
    "code": "invalid_string",
    "message": "Invalid url",
    "path": [
      4
    ]
  },
  {
    "validation": "url",
    "code": "invalid_string",
    "message": "Invalid url",
    "path": [
      5
    ]
  },
  {
    "validation": "url",
    "code": "invalid_string",
    "message": "Invalid url",
    "path": [
      6
    ]
  },
  {
    "validation": "url",
    "code": "invalid_string",
    "message": "Invalid url",
    "path": [
      7
    ]
  },
  {
    "validation": "url",
    "code": "invalid_string",
    "message": "Invalid url",
    "path": [
      8
    ]
  },
  {
    "validation": "url",
    "code": "invalid_string",
    "message": "Invalid url",
    "path": [
      9
    ]
  },
  {
    "validation": "url",
    "code": "invalid_string",
    "message": "Invalid url",
    "path": [
      10
    ]
  },
  {
    "validation": "url",
    "code": "invalid_string",
    "message": "Invalid url",
    "path": [
      11
    ]
  }
]
    at Object.get error (file:///home/me/.cache/deno/npm/registry.npmjs.org/zod/3.23.8/lib/index.mjs:587:31)
    at ZodSet.parse (file:///home/me/.cache/deno/npm/registry.npmjs.org/zod/3.23.8/lib/index.mjs:692:22)
    at <anonymous>:1:46

@patrickReiis,

The issue stems from the input being a set of individual characters from the URL rather than complete URLs. This is why the validation is failing.

Try updating your code to:
z.set(z.string().url()).parse(new Set(['https://mint.coinos.io']));

By providing a set of complete URLs, the schema should validate correctly.

Ohh boy... Thanks for the help!