npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sanity-quick-fields

v0.2.0

Published

Helper function for simply generating Sanity.io schema fields

Downloads

11

Readme

Sanity.io Quick Fields

Sanity.io Schema is awesome, but the definition files get long.

This quickFields() / qF() function will help you compose fields in a more concise way.

// Before...
fields: [
    {
        name: 'title',
        title: 'Title',
        type: 'string',
    },
    {
        name: 'published',
        title: 'Published',
        type: 'date',
    },
],

// After...
fields: [
    qF('title'),
    qF('published', 'date'),
],

Get started

npm i sanity-quick-fields
or
yarn add sanity-quick-fields

Inside any Sanity document import the qF() or quickFields() functions. They both do the same thing.

import { qF } from "sanity-quick-fields";

Parameters

qF(name, type, options, validation);

name (string or Array)

Pass a string for the field's name, and qF will automatically generate a Capital Case version for the title.

qF('currentLocation')

{ name: 'currentLocation', title: 'Current Location', type: 'string' }

You can overwrite this behaviour by passing in an Array instead, with name and title in that order.

qF(['linkedIn', 'LinkedIn'])

{ name: 'linkedIn', title: 'LinkedIn', type: 'string' }

type (string)

Defaults to stringqF() will pass along whatever type you feed the function, so if it doesn't exist in Sanity, you'll probably get an error.

qF('quantity', 'number')

{ name: 'quantity', title: 'Quantity', type: 'number' }

options (Object)

Pass in an Object of any additional options you need to pass into the field. These are inserted directly into the options key.

qF('dateOfBirth', 'date', { dateFormat: 'YY-MM-DD' })

{
    name: 'dateOfBirth',
    title: 'Date Of Birth',
    type: 'date',
    options: {
      dateFormat: 'YY-MM-DD',
    },
}

You can pass in rows here on the text field type. It's smart enough to store that outside of options (why is it this way Sanity, why?!).

validation (function or Object or Array)

A function is passed as is:

qF('title', 'string', {}, Rule => Rule.required().max(50).error('Required and max 50 chars.'))

{
    name: 'title',
    type: 'string',
    validation: Rule => Rule.required().max(50).error('Required and max 50 chars.')
}
qF('title', 'string', {}, Rule => [
  Rule.required().max(50).error('Required and max 50 chars.'),
  Rule.regex(/^[-a-z0-9]+$/g).error('Only a-z, 0-9, and -.'),
  Rule.min(10).warning('Should be at least 10 chars')
])

{
    name: 'title',
    type: 'string',
    validation: Rule => [
        Rule.required().max(50).error('Required and max 50 chars.'),
        Rule.regex(/^[-a-z0-9]+$/g).error('Only a-z, 0-9, and -.'),
        Rule.min(10).warning('Should be at least 10 chars')
    ]
}

An Object or Array will generate the corresponding function.
The above examples can be defined as follows:

qF('title', 'string', {}, { error: 'Required and max 50 chars.', required: true, max: 50 })

qF('title', 'string', {}, [
  { error: 'Required and max 50 chars.', required: true, max: 50 },
  { error: 'Only a-z, 0-9, and -.', regex: /^[-a-z0-9]+$/g },
  { warning: 'Should be at least 10 chars', min: 10 }
])

Quick Field Builder, Methods

For fields that have children, there are two different functions with helper methods.

qFB() / quickFieldsBuilder()

You can pass in the same first three params as above, but also append .children() and .preview() methods.

Unfortunately you also need to end with .toObject to prevent a Type warning in Sanity.

.children() (Array)

If creating an Array or Object type, pass an Array of fields. This is where nesting qF() becomes powerful.

qFB('contact', 'object')
  .children([qF('name'), qF('email')])
  .toObject

{
    name: 'contact',
    title: 'Contact',
    type: 'object',
    fields: [
        { name: 'name', title: 'Name', type: 'string' },
        { name: 'email', title: 'Email', type: 'string' },
    ],
}

.preview() (Object)

Pass in an object to fill out the select key in preview. This has limited usefulness as you probably want to customise the preview further. But it's a neat shortcut.

qFB('contact', 'object')
  .children([qF('name'), qF('email')])
  .preview({title: 'name', subtitle: 'email'})
  .toObject

{
    name: 'contact',
    title: 'Contact',
    type: 'object',
    fields: [
        { name: 'name', title: 'Name', type: 'string' },
        { name: 'email', title: 'Email', type: 'string' },
    ],
    preview: {
      select: {
        title: 'name',
        subtitle: 'email'
      }
    }
}

Mix and match

qF works best on simple fields, but for more complex fields it probably makes more sense to write them in full. And that's fine, you can selectively use the function where it makes sense.

fields: [
  qF("title"),
  {
    name: "slug",
    title: "Slug",
    type: "slug",
    validation: (Rule) =>
      Rule.required().custom((slug) => {
        const regex = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
        return regex.test(slug.current);
      }),
    options: {
      source: "title",
      maxLength: 96,
    },
  },
  qF("body", "bodyPortableText"),
];

Thanks

This library is insired by ACF Builder, the best way by far to compose Advanced Custom Fields!