Module 03
Page 03
2 min

Number

Working with Number schema types and grouping via field groups


You've also seen this schema type before, Number. Let's add a field for the view count of this blog.

We don't really have a huge purpose for a Number schema-type field in this blog post, but it's good practice anyways!

Adding a number schema type

Like before, we add it just like this, right after our description

/schemas/blogPost.js
...,{  name: 'viewCount',  type: 'number',  title: 'View Count',},...

Pretty easy - but let's talk validation because just like String and Text, we can play with those rules too.

Validation

Here are a few reasons to add validation to a number:

  • Ensuring the number doesn't go above a set max
  • Ensuring the number doesn't go below a set min
  • Making sure the number is filled out (required)
  • Applying precision in decimals
  • Setting the number to be an integer
  • And a lot more! Check out the Sanity Reference Docs for what else you can do

Here are a few examples of how we'd add the validation rules:

/schemas/blogPost.js
  {    name: 'viewCount',    type: 'number',    title: 'View Count',    group: 'metadata',    validation: (Rule) => Rule.required().min(0).max(1000).integer(),  },

And seeing them in effect:

And that's basically it about Numbers - but let's pivot to something else.

Field Groups

If you haven't noticed, the number of fields we're working is growing - and they will continue to increase even more as we add more schema types.

How do we fix that? With Field Groups.

Field Groups allow us to organize our fields by grouping them under tabs in our documents. They're purely visual, but help us organize our fields.

Adding Field Groups

First, let's define our groups:

/schemas/blogPost.js
...,title: 'Blog Post',groups: [  {    name: 'content',    title: 'Content',  },  {    name: 'metadata',    title: 'Metadata',  },],fields: [...

We defined our groups right before our fields and gave each one a name and title.

Again, name and title are used the same way they are for fields, objects, documents, etc.

But if we go back to Studio, we don't see anything. That's because we need to give each of our fields a group to go under:

/schemas/blogPost.js
fields: [  {    name: 'title',    ...,    group: 'content',    ...  },  {    name: 'description',    ..,    group: 'content',    ...  },  {    name: 'viewCount',    ..,    group: 'metadata',    ...  },],

Now, if we go back to Studio, we'll see our tabs. Click on them, and only the fields that had the group are in it.

We'll be using Field Groups from now on to organize our fields and make sure everything is easy to use. You can always see all of your fields in the All fields tab.

Ritesh Kanchi © 2025