👋 Next.js Conf 2024: Come build, party, run, and connect with us! See all events

Discussion on various issues related to schema fields in a Sanity.io Slack thread.

21 replies
Last updated: Mar 19, 2023
user U
Already replied above
Mar 19, 2023, 6:56 AM
user Z
i already did that and got typescript error Argument of type '{ name: "audio"; title: string; type: "file"; accept: string; }' is not assignable to parameter of type '{ type: "file"; name: "audio"; } & Omit<FileDefinition, "preview"> & { preview?: PreviewConfig<Record<string, string>, Record<never, any>> | undefined; } & FieldDefinitionBase'. Object literal may only specify known properties, and 'accept' does not exist in type '{ type: "file"; name: "audio"; } & Omit<FileDefinition, "preview"> & { preview?: PreviewConfig<Record<string, string>, Record<never, any>> | undefined; } & FieldDefinitionBase'.ts(2345)
Mar 19, 2023, 7:13 AM
user U
Can you paste me your schema, please?
Mar 19, 2023, 7:13 AM
Ok. Wait.
Mar 19, 2023, 7:13 AM
this is like my whole schema 
Mar 19, 2023, 7:23 AM
import {defineType, defineField} from 'sanity'

export default defineType({
  name: 'sets',
  title: 'Sets',
  type: 'document',
  fields: [
    defineField({
      name: 'setNumber',
      title: 'Set Number',
      description: 'यहाँ Set Number लेख्नुहोस् र यो नम्बर दोहोर हुनु हुँदैन।',
      type: 'number',
      validation: (Rule) => [Rule.required(), Rule.min(1)],
      placeholder: 'eg: 1',
    }),
    defineField({
      name: 'slug',
      title: 'Slug',
      description: 'Set Number लेखेपछि Generate बटनमा क्लिक गर्नुहोस्',
      type: 'slug',
      options: {
        source: 'setNumber',
        slugify: (input) => 'set-' + input,
      },
      validation: (Rule) => Rule.required(),
    }),
    defineField({
      name: 'questions',
      title: 'Questions',
      type: 'array',
      of: [
        {
          type: 'object',
          name: 'set',
          title: 'Set',
          fields: [
            defineField({
              name: 'questionNumber',
              title: 'Question Number',
              description: 'यहाँ Question Number दिनुहोस् र नम्बर दोहोर हुनु नदिनुहोस्।',
              type: 'number',
              validation: (Rule) => [
                Rule.required().error(
                  ' यहाँ Question Number दिनुहोस् र नम्बर दोहोर हुनु नदिनुहोस्।'
                ),
                Rule.min(1).error(' 1 भन्दा बढी नम्बर हुनुपर्छ'),
              ],
              placeholder: 'eg: 1',
            }),
            defineField({
              name: 'question',
              title: 'Question ',
              description: 'Question यहाँ लेख्नुहोस् ',
              type: 'string',
              validation: (Rule) => Rule.required().error('Question लेख्नुहोस् यहाँ'),
            }),
            defineField({
              name: 'audio',
              title: 'Audio ',
              type: 'file',
            }),
            defineField({
              name: 'options',
              title: 'Options ',
              type: 'array',
              of: [
                {
                  type: 'object',
                  name: 'answer',
                  title: 'Answer',
                  fields: [
                    defineField({
                      name: 'answerText',
                      title: 'Answer',
                      type: 'string',
                      validation: (Rule) => Rule.required(),
                    }),
                    defineField({
                      name: 'isCorrect',
                      title: 'सही भए यसलाई छुनु नत्र भए यसलाई नछुनु',
                      type: 'boolean',
                      initialValue: false,
                    }),
                  ],
                },
              ],
              validation: (Rule) => [
                Rule.custom((answers) => {
                  if (!Array.isArray(answers)) {
                    return 'जवाफहरू लेख्नको लागि "Add Item" मा क्लिक गर्नुहोस् र जवाफ लेख्नुहोस्।'
                  }
                  if (answers.length < 4) {
                    return `कम्तिमा ४ जवाफहरु हुनुपर्छ, तर ${answers.length} जवाफ मात्र प्रदान गरिएका छन्`
                  }
                  if (answers.length > 4) {
                    return `अधिकतम ४ जवाफहरु हुन सक्छन्, तर ${
                      answers.length
                    } जवाफहरु प्रदान गरिएका छन्! ${answers.length - 4} जवाफ Delete गर्नुहोस्`
                  }
                  const numCorrect = answers.filter((answer) => answer.isCorrect).length
                  if (numCorrect === 0) {
                    return 'कम्तिमा एउटा जवाफ सही हुनुपर्छ'
                  }
                  if (numCorrect > 1) {
                    return `केवल एउटा जवाफ सही हुन सक्छ! तर तपाईंले ${numCorrect} जवाफमा Tick लाउनु भएको छ`
                  }
                  return true
                }),
              ],
            }),
          ],
          preview: {
            select: {
              number: 'questionNumber',
              question: 'question',
            },
            prepare({number, question}) {
              return {
                title: `Question (${number}) - ${question ? question : 'Question लेखन भुलिनुभयो'} `,
              }
            },
          },
        },
      ],
      validation: (Rule) =>
        Rule.custom((questions) => {
          if (!Array.isArray(questions)) {
            return '40 ओटा प्रश्नहरु लेख्नुहोस्।'
          }
          if (questions.length !== 40) {
            return 'Questions 40 ओटा भन्दा बढी या कम हुनु हुदैन'
          }
          return true
        }),
    }),
  ],

  preview: {
    select: {
      title: 'setNumber',
    },
    prepare({title}) {
      return {
        title: `Set-${title} `,
        subtitle: `By Govinda Dhakal`,
        imageUrl: '../static/govinda.jpg',
      }
    },
  },
})
Mar 19, 2023, 7:24 AM
this is my whole schema and audio is simply this
defineField({
name: 'audio',
title: 'Audio ',
type: 'file',
accept: 'audio/*',
}),
Mar 19, 2023, 7:26 AM
Robin, your issue is you're not using "options.accept". This is what you're looking for:
            defineField({
              name: 'audio',
              title: 'Audio ',
              type: 'file',
              options: { accept: 'audio/*' },
            }),
Mar 19, 2023, 7:26 AM
user Z
thanks it worked and also can i do conditional rendering here
defineField({
name: 'question',
title: 'Question ',
description: 'Question यहाँ लेख्नुहोस् ',
type: 'string',
validation: (Rule) =&gt; Rule.required().error('Question लेख्नुहोस् यहाँ'),
}),
defineField({
name: 'audio',
title: 'Audio ',
type: 'file',
options: {accept: 'audio/*'},
}), like if there is a text in question, hidden true to audio field and if there is a audio file selected, question field hidden
Mar 19, 2023, 7:29 AM
Yes. You can use Sanity's hidden property as a function, e.g. hidden: ({ parent }) =&gt; !!parent?.question in the audio file will hide it if question has any text in it
Mar 19, 2023, 7:31 AM
The related documentation is a great place to start: https://www.sanity.io/docs/conditional-fields
Mar 19, 2023, 7:32 AM
user Z
thanks, that worked out too, and now here defineField({ name: 'setNumber',
title: 'Set Number',
description: 'यहाँ Set Number लेख्नुहोस् र यो नम्बर दोहोर हुनु हुँदैन।',
type: 'number',
validation: (Rule) =&gt; [Rule.required(), Rule.min(1)],
placeholder: 'eg: 1',
}),
defineField({
name: 'slug',
title: 'Slug',
description: 'Set Number लेखेपछि Generate बटनमा क्लिक गर्नुहोस्',
type: 'slug',
options: {
source: 'setNumber',
slugify: (input) =&gt; 'set-' + input,
},
validation: (Rule) =&gt; Rule.required(),
}), can i also generate slug without even clicking on generate button, i want it hidden and it has to get auto generated when i type setNumber,
Mar 19, 2023, 7:38 AM
user U
While you can do that, I believe you'd need to go a bit too far to make that one happen, so best leave the slug field visible
Mar 19, 2023, 7:39 AM
you know how clients are, they are stupid. they want it hidden and gets auto generated
Mar 19, 2023, 7:40 AM
user U
I would tell them to settle for this one. In order to make this happen you'd need to write a custom component for your type, which would take you some time, but it's still possible
Mar 19, 2023, 7:41 AM
user Z
and now here defineField({ name: 'questions',
title: 'Questions',
type: 'array',
of: [
{
type: 'object',
name: 'set',
title: 'Set',
fields: [
defineField({
name: 'questionNumber',
title: 'Question Number',
description: 'यहाँ Question Number दिनुहोस् र नम्बर दोहोर हुनु नदिनुहोस्।',
type: 'number',
validation: (Rule) =&gt; [
Rule.required().error(
' यहाँ Question Number दिनुहोस् र नम्बर दोहोर हुनु नदिनुहोस्।'
),
Rule.min(1).error(' 1 भन्दा बढी नम्बर हुनुपर्छ'),
],
placeholder: 'eg: 1',
}), can i make the question number auto incremented
Mar 19, 2023, 7:41 AM
The documentation will mostly answer all of those questions as you walk through it. You're looking for this: https://www.sanity.io/docs/initial-value-templates#f853d3151a21
Mar 19, 2023, 7:44 AM
Now, I am not particularly aware if you can read document information from initialValue, but there's the fetch route
Mar 19, 2023, 7:45 AM
If your question number is just its index + 1, you don't need a question number at all in your schema, right?
Mar 19, 2023, 7:45 AM
If a field's value is always derived from another field, the field shouldn't exist in the first place, technically speaking
Mar 19, 2023, 7:46 AM
and now this is the final question i swear🥳🥳, can i make the string or even number field unique?
Mar 19, 2023, 7:48 AM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?