Sanity: Initial values for any type

Created on 30 Oct 2019  ยท  21Comments  ยท  Source: sanity-io/sanity

Problem

If a document field is of some custom type that always has the same default, I have to respecify those defaults in every document that uses it. As an example:

// paid.js
export default {
  name: 'paidContent',
  title: 'Paid content',
  type: 'boolean',
  description: 'Only show this to paying customers',
  options: {
    layout: "checkbox",
  },
}
// docs.js
export const someDocument {
  name: 'someDocument',
  fields: [
    // buncha fields here
    {
      name: 'paidContent',
      type: 'paidContent'
    },
  ],

  initialValue: {
    paidContent: true,
  },
}

export const anotherDocument {
  name: 'anotherDocument',
  fields: [
    // buncha fields here
    {
      name: 'paidContent',
      type: 'paidContent',
    }
  ],

  initialValue: {
    paidContent: true,
  },
}

Proposed solution

/// paid.js
export const paidContent =  {
  name: 'paidContent',
  title: 'Paid content',
  type: 'boolean',
  description: 'Only show this to paying customers',
  options: {
    layout: "checkbox",
  },

  initialValue: {
    paidContent: true,
  },
};
// docs.js
export const someDocument {
  name: 'someDocument',
  fields: [
    // buncha fields here
    {
      name: 'paidContent',
      type: 'paidContent',
    }
  ]
}

export const anotherDocument {
  name: 'anotherDocument',
  fields: [
    // buncha fields here
    {
      name: 'paidContent',
      type: 'paidContent',
    }
  ]
}

Both someDocument and anotherDocument would have the initial value { paidContent: true }.

Alternate solutions

E.g. Exporting the initial value, and then importing & merging it with document initial values. More brittle, requires users of the type to do it manually

Feature Request Enhancement

Most helpful comment

+1 for initial values for objects, please.

All 21 comments

Thanks for submitting this! It's obviously a feature that might be handy. We have no ETA on it as of yet, but feel free to follow this issue.

@kmelve pretty please?

I like the proposed solution from @ORBAT. I would also add that a nice addition would be to ensure that the document can override the default that is set in the type initialValue.

I also look forward to a solution where we can add Initial values for objects.

I would also love this to be resolved!

This is all I want for Christmas

+1 on initial values for objects as well ๐Ÿ™

i++
And is there a temp workaround for this? And is there any news on the ETA by now?

I would like to be able to use an initial values for objects as well.

+1 Yes, please :)

+1 Looking forward to this!!

+1 for initial values for objects, please.

+1 for initial / default values for objects ๐Ÿ™

+1 for would be very nice to set initial values for objects

+1 for initial / default values for objects ๐Ÿง‘โ€๐Ÿณ

This would really be awesome! Are there any workarounds?

+1 on this for sure, major pain point

+1 for this. Any idea where this is on the roadmap?

@kmelve Would you accept a PR for this? On the surface it seems fairly straight-forward, or are you expecting it to be more complex?

Or @rexxars equally ๐Ÿ™‚

^^^

Given how great everything else is, I was super surprised to see default values / initial values aren't possible for objects. Would be great to have a solution to this!

Was this page helpful?
0 / 5 - 0 ratings