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/template-validator

v1.2.2

Published

Sanity template validator for remote templates

Downloads

21,081

Readme

Sanity Template Validator

A validation utility for Sanity.io template repositories. Use it as a dependency in your projects or as a GitHub Action to ensure your Sanity templates meet the required standards.

Features

  • Validates Sanity.io template structure and requirements
  • Supports monorepo detection and validation
  • Can be used as a Node.js dependency, GitHub Action, or CLI tool
  • Validates environment variables and configuration files
  • TypeScript support with full type definitions
  • Local directory validation support

Installation

npm install @sanity/template-validator
# or
yarn add @sanity/template-validator
# or
pnpm add @sanity/template-validator

Usage

As a CLI Tool

The package includes a CLI tool that can validate local directories:

# Install globally
npm install -g @sanity/template-validator

# Validate current directory
sanity-template-validate

# Validate specific directory
sanity-template-validate path/to/template

# Using Npx
npx @sanity/template-validator

Adding as a Dev Dependency

The recommended way to use the validator in your template project is to add it as a dev dependency and create a validation script:

  1. Add to your project:
npm install --save-dev @sanity/template-validator
  1. Add a script to your package.json:
{
  "scripts": {
    "validate": "sanity-template-validate"
  }
}
  1. Run the validation:
npm run validate

As a Node.js Dependency

import {validateLocalTemplate, validateRemoteTemplate} from '@sanity/template-validator'

// Validate a local directory
async function validateLocal() {
  const result = await validateLocalTemplate('/path/to/template')

  if (result.isValid) {
    console.log('Template is valid!')
  } else {
    console.error('Validation failed:', result.errors)
  }
}

// Validate a remote repository
async function validateRemote() {
  const baseUrl = 'https://raw.githubusercontent.com/owner/repo/branch'
  const result = await validateRemoteTemplate(baseUrl)

  if (result.isValid) {
    console.log('Template is valid!')
  } else {
    console.error('Validation failed:', result.errors)
  }
}

// Advanced usage with FileReader
import {LocalFileReader, getMonoRepo} from '@sanity/template-validator'

async function advancedValidation() {
  const fileReader = new LocalFileReader('/path/to/template')
  const packages = await getMonoRepo(fileReader)
  // Use packages for further processing
}

As a GitHub Action

name: Validate Template
on: push

jobs:
  validate:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Validate Sanity Template
        uses: sanity-io/[email protected]

API Reference

validateLocalTemplate

Validates a local Sanity template directory.

async function validateLocalTemplate(directory: string): Promise<ValidationResult>

Parameters:

  • directory: Path to the template directory

validateRemoteTemplate

Validates a remote Sanity template repository.

async function validateRemoteTemplate(
  baseUrl: string,
  headers?: Record<string, string>
): Promise<ValidationResult>

Parameters:

  • baseUrl: The base URL to the raw repository content
  • headers: Custom headers for API requests (optional)

Returns:

type ValidationResult = {
  isValid: boolean
  errors: string[]
}

File Readers

The package provides two file reader classes for advanced usage:

class LocalFileReader implements FileReader {
  constructor(basePath: string)
  readFile(filePath: string): Promise<{exists: boolean; content: string}>
}

class GitHubFileReader implements FileReader {
  constructor(baseUrl: string, headers?: Record<string, string>)
  readFile(filePath: string): Promise<{exists: boolean; content: string}>
}

getMonoRepo

Helper function to detect monorepo configuration.

async function getMonoRepo(fileReader: FileReader): Promise<string[] | undefined>

Validation Rules

A valid Sanity template must meet the following criteria:

For Single-Package Repositories:

  • Must have a valid package.json with 'sanity' dependency
  • Must have sanity.config.js/ts and sanity.cli.js/ts
  • Must have one of: .env.template, .env.example, or .env.local.example

For Monorepos:

  • Each package must have a valid package.json
  • At least one package must include 'sanity' in dependencies
  • At least one package must have Sanity configuration files
  • Each package must have appropriate environment template files

Environment Files Must Include:

  • SANITY_PROJECT_ID or SANITY_STUDIO_PROJECT_ID
  • SANITY_DATASET or SANITY_STUDIO_DATASET

GitHub Action Inputs

| Input | Description | Required | |-------|-------------|----------| | directory | The directory to validate. Use this if you have multiple templates in a repository. | No |

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT © Sanity.io

Support