next.js 在健全模式类型定义中,如何定义未知属性字段?

6ljaweal  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(69)

我正在将sanity集成到NextJS应用程序中。现在,当我定义project的模式类型时,我遇到了一个错误。我想定义一个名为features的字段。特性是唯一的,因为我不知道这些特性是什么。例如,我希望编辑器能够添加键值对,如

bedrooms -> 2
floors -> 4
length -> 40m

字符串
但请记住,我不知道这些功能是什么,所以我不能使用对象类型。
我试着使用对象类型,但这需要我检查必需的字段,这显然对我不起作用。

我应该使用什么数据类型来实现所需的功能,或者在下一个js和sanity中如何处理这样的问题?

如果您需要的话,这里是模式文件。

export const project: SchemaTypeDefinition = {
  name: "project",
  title: "Project",
  type: "document",
  fields: [
    {
      name: "title",
      title: "Title",
      type: "string",
      description: "Title of the project",
      validation: (Rule) => Rule.required(),
    },
    {
      name: "slug",
      title: "Slug",
      type: "slug",
      options: { source: "title" },
      validation: (Rule) => Rule.required(),
    },
    {
      name: "excerpt",
      title: "Excerpt",
      type: "string",
      description: "a short two sentence description of the project",
      validation: (Rule) => Rule.required(),
    },
    {
      name: "price",
      title: "Price",
      type: "number",
      description: "the price of the project in USD",
    },
    {
      name: "mainImage",
      title: "Main image",
      type: "image",
      description:
        "The face of the project. This image will appear in socials, the shop and more",
      validation: (Rule) => Rule.required(),
    },
    {
      name: "images",
      title: "Images",
      type: "array",
      description: "all other images of the project.",
      of: [
        {
          type: "image",
          fields: [
            {
              name: "alt",
              title: "Alt",
              type: "string",
            },
          ],
        },
      ],
      validation: (Rule) => Rule.required(),
    },
    {
      name: "isPortfolio",
      title: "is portfolio?",
      type: "boolean",
      description:
        "check this if the projects is a portfolio project meaning it can`t be bought.",
    },
    {
      name: "content",
      title: "Content",
      type: "array",
      of: [{ type: "block" }],
      validation: (Rule) => Rule.required(),
      description:
        "use this to create content for your project. Explain what the project is in depth.The better you are at explaining the higher the conversion rate",
    },
    {
      name: "clientDeliverables",
      title: "Client Deliverables",
      type: "array",
      of: [
        {
          type: "file",
          options: {
            accept: ".pdf",
          },
        },
      ],
      description:
        "client deliverables are pdf files the user will download after paying.",
    },
    {
      name: "schematicFile",
      title: "Schematic Drawing File",
      type: "file",
      description:
        "upload the schematic file. remember this file the user will download for free.",
      options: {
        accept: ".pdf",
      },
    },
  ],
};

fumotvh3

fumotvh31#

你应该使用一个数组,像这样:

{
    name: 'features',
    type: 'array',
    title: 'Features',
    of: [
        {
            type: 'object',
            fields: [
                {
                    name: 'key',
                    title: 'Key',
                    type: 'string',
                },
                {
                    name: 'value',
                    title: 'Value',
                    type: 'string',
                }
            ],
            preview: {
                ...
                },
            }
        }
    ],
},

字符串

相关问题