Skip to content

Instantly share code, notes, and snippets.

View PetterRuud's full-sized avatar
🥓
Focusing

Petter PetterRuud

🥓
Focusing
View GitHub Profile
@PetterRuud
PetterRuud / snippets.css
Created December 29, 2021 17:24
CSS snippets
:checked { /* if */ }
:not(:checked) { /* else */ }
[data-attr="true"] { /* if */ }
[data-attr="false"] { /* elseif */ }
:not([data-attr]) { /* else */}
@PetterRuud
PetterRuud / gist:51b872369515e86ddc4710969737985a
Created November 24, 2021 14:18
How to install everything from package.json
npm install (with no args, in package dir)
npm install [<@scope>/]<name>
npm install [<@scope>/]<name>@<tag>
npm install [<@scope>/]<name>@<version>
npm install [<@scope>/]<name>@<version range>
npm install <git-host>:<git-user>/<repo-name>
npm install <git repo url>
npm install <tarball file>
npm install <tarball url>
npm install <folder>
@PetterRuud
PetterRuud / Sanity GROQ Array reference in Array
Last active June 29, 2021 11:57
Sanity GROQ Array reference in Array
"map_region_data": *[_type == "region" && count((country[]._ref)[@ in ^.country[]._ref]) > 0]{
reg_name,
slug
}
@PetterRuud
PetterRuud / utils.ts
Created June 7, 2021 20:38
Sanity Studio Preview
const schema = {
AUTHOR: 'author',
ARTICLE: 'article',
DEPARTMENT: 'department',
PAGE: 'page',
SITE_SETTINGS: 'siteSettings',
REDIRECT: 'redirect',
SEO_SETTINGS: 'seoSettings',
SOCIAL_MEDIA_SETTINGS: 'socialMediaSettings',
MEDIA_TAG: 'media.tag',
@PetterRuud
PetterRuud / translate.js
Created May 3, 2021 15:36
Translate all locale strings with missing translations in a Sanity dataset - https://sanity.io
/* eslint-disable no-console */
import client from 'part:@sanity/base/client'
import reduce from 'json-reduce'
// see https://github.com/sanity-io/sanity/blob/next/packages/example-studio/schemas/languages.js for an example
import {SUPPORTED_LANGUAGES} from '../schemas/languages'
import Translate from '@google-cloud/translate'
const translateAPI = new Translate({
@PetterRuud
PetterRuud / ExternalDataInput.js
Created May 3, 2021 15:33
ExternalDataInput.js
import React from 'react'
import SearchableSelect from 'part:@sanity/components/selects/searchable'
import FormField from 'part:@sanity/components/formfields/default'
import {PatchEvent, set, unset} from 'part:@sanity/form-builder/patch-event'
const STREET_NAMES = [
'Thorvald Meyers gate',
'Champs-Élysées',
'Lombard Street',
'Abbey Road',
@PetterRuud
PetterRuud / query.groq
Created April 19, 2021 20:33
GROQ query to pull data for image palette information
*[_id =="documentWithImage"]{
...,
image{
..., // Get all information
"palette": asset->metadata.palette // Pull palette from metadata on the asset
}
}
@PetterRuud
PetterRuud / deleteDocsWithoutSchema.js
Created March 23, 2021 05:40
deleteDocsWithoutSchema.js
/**
* This script can be placed in the root of your studio folder,
* and be run with `sanity exec deleteDocsWithoutSchema.js --with-user-credentials
*/
import client from 'part:@sanity/base/client'
const getSanitySchema = require('./node_modules/@sanity/core/lib/actions/graphql/getSanitySchema')
const Schema = getSanitySchema(process.cwd())
const types = Schema._original.types.map(({name}) => name)
@PetterRuud
PetterRuud / schema-validation.md
Last active May 3, 2021 15:37
Sanity Schema Validations rules

A great feature of SANITY.io is the ability to add field validation to schema types. This can ensure data conforms to specific patterns, e.g. a ZIP code is actually a valid ZIP code or an email field is a valid email. This brief article covers just a few of the ways you can validate a field, I will add more snippets over time as I find useful ones.

Required field

The simplest type of validation - make a field required without any other custom rules.

{
  name: "title",
  title: "Title",
map(['🐮','🍠', '🐔', '🌽'], cook);
=> ['🍔', '🍟', '🍗', '🍿'];
filter(['🍔', '🍟', '🍗', '🍿'], isVegetarian);
=> ['🍟', '🍿'];
reduce(['🍔', '🍟', '🍗', '🍿'], eat);
=> ['💩'];