Skip to content

Nullable Array always projects/arraypostfixes to a null #641

Open
@OMikkel

Description

Packages:

@sanity-typed/client
@sanity-types/types

Issue:

When i am querying for data with groq queries, the generated types does not show hidden fields correct.
Hidden fields are permanently null and they do not respect the hidden fields rule set in the schema.

I have been dealing with this issue for 5 hours now and i believe i have tried everything. Any help or insight into why this issue occurs is much appreciated.

Context:

Frontend:

const data = await sanityClient.fetch(`
    *[_type == "footer" && type == "links"]{
        _id,
        title,
        links[]{
            _key,
            title,
            url,
            target
        }
    }
`)

Schema:

export default defineType({
  name: "footer",
  type: "document",
  title: "Footer",
  fields: [
    defineField({
      name: "type",
      type: "string",
      title: "Type",
      options: {
        list: [
          { title: "About", value: "about" },
          { title: "Social", value: "social" },
          { title: "Links", value: "links" },
        ],
      },
      validation: (Rule) => Rule.required(),
    }),
    defineField({
      name: "title",
      type: "string",
      title: "Title",
      validation: (Rule) => Rule.required(),
    }),
   ...,
    defineField({
      name: "links",
      type: "array",
      title: "Links",
      of: [
        defineArrayMember({
          type: "object",
          icon: <LinkIcon />,
          fields: [
            defineField({
              name: "title",
              type: "string",
              title: "Title",
            }),
            defineField({
              name: "url",
              type: "string",
              title: "URL",
            }),
            defineField({
              name: "target",
              type: "string",
              title: "Target",
              options: {
                list: [
                  { title: "Self", value: "_self" },
                  { title: "Blank", value: "_blank" },
                ],
              },
            }),
          ],
          preview: {
            select: {
              title: "title",
              subtitle: "url",
            },
          },
        }),
      ],
      validation: (Rule) =>
        Rule.custom((field, context) => {
          if (context.document?.type === "links" && !field?.length) {
            return "Links are required";
          }
          return true;
        }),
      hidden: ({ document }) => document?.type !== "links",
    }),
    ...,
  ],
});

Real data:

Metadata

Assignees

No one assigned

    Labels

    help wantedPlease help me! I'm lonely.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions