sanity-io / sanity

Sanity Studio – Rapidly configure content workspaces powered by structured content
https://www.sanity.io
MIT License
5.27k stars 425 forks source link

URLs URI validation breaks if its not the only validation rule #3298

Open alexandereneroth opened 2 years ago

alexandereneroth commented 2 years ago

This works

    {
      type: "url",
      name: "url",
      validation: (Rule) => [
        Rule.uri({ scheme: ["http", "https", "mailto", "tel"] }),
      ],
    }

This does not work

    {
      type: "url",
      name: "url",
      validation: (Rule) => [
        Rule.uri({ scheme: ["http", "https", "mailto", "tel"] }),
        Rule.custom(() => true),
      ],
    }

It results in this:

Screenshot 2022-05-20 at 12 34 13

Interestingly, this

    {
      type: "url",
      name: "url",
      validation: (Rule) => [
        Rule.uri({ scheme: ["http", "https", "mailto", "tel"] }),
        Rule.custom(() => true).warning(),
      ],
    }

Results in this:

Screenshot 2022-05-20 at 12 36 00

Which versions of Sanity are you using?

@sanity/base            2.30.0 (up to date)
@sanity/cli             2.30.0 (up to date)
@sanity/core            2.30.0 (up to date)
@sanity/default-layout  2.30.0 (up to date)
@sanity/default-login   2.30.0 (up to date)
@sanity/desk-tool       2.30.0 (up to date)
@sanity/vision          2.30.0 (up to date)

Run sanity versions in the terminal and copy-paste the result here.

What operating system are you using? MacOS Monterey 12.1

Which versions of Node.js / npm are you running?

npm -v && node -v
6.14.16
v14.18.3
KittyGiraudel commented 2 years ago

Does it happen if you chain both rules?

    {
      type: "url",
      name: "url",
      validation: (Rule) => Rule
        .uri({ scheme: ["http", "https", "mailto", "tel"] }),
        .custom(() => true)
      ],
    }
alexandereneroth commented 2 years ago

Thanks! that did work. Seems like the problem appears when you return an array from the validation function

fvieira commented 6 months ago

Hi @RitaDias, do you know why this issue was closed?

What KittyGiraudel suggested worked for alexandereneroth, but the original bug is still present in the most recent versions: uri validations don't work correctly when in an array of validations, which as far as I know are the only way of mixing warnings with errors, which in turn means I can't have a uri validation give an error in a field where I also want another validation to give a warning.

I just had this happen to me and had to give up on the warning validation in order to have the uri validation.

Versions @sanity/cli (global) 3.24.1 (latest: 3.38.0) @sanity/cli 3.24.1 (latest: 3.38.0) @sanity/dashboard 3.1.6 (up to date) @sanity/ui 1.9.3 (latest: 2.1.4) @sanity/vision 3.24.1 (latest: 3.38.0) sanity 3.24.1 (latest: 3.38.0)

RitaDias commented 6 months ago

Hi @fvieira!

I don't quite recall what was done at the time since it's been a while, however I've added this to our triaging to be prioritised :)

Thanks for reporting it!