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 🙏

© 2025 – Pkg Stats / Ryan Hefner

formshape

v0.0.5

Published

Type-safe form validation for SvelteKit's remote functions

Readme

formshape

npm

Type-safe form validation for SvelteKit Remote Functions using Standard Schema compatible validators.

Features

  • 🎯 Works with any Standard Schema compatible library (Zod, Valibot, Arktype, etc.)
  • 🔒 Full type safety with automatic type inference
  • ⚡ Preserves all SvelteKit form features (progressive enhancement, enhance, buttonProps, etc.)
  • 📦 Lightweight with zero dependencies (besides your validator)
  • 🎭 Same API as SvelteKit's form function

Installation

npm install formshape
# or
pnpm add formshape

Usage

1. Create a validated function in your server code

// src/routes/contact/data.remote.ts
import { z } from 'zod'
import { form } from '$app/server'
import { createValidated } from 'formshape'

// Create the validated function using your app's form function
const validated = createValidated(form)

// Define your schema
const contactSchema = z.object({
	name: z.string().min(2, 'Name must be at least 2 characters'),
	email: z.string().email('Invalid email address'),
	message: z.string().min(10, 'Message must be at least 10 characters')
})

// Create your form handler - data is fully typed!
export const submitContact = validated(contactSchema, async (data) => {
	// data is typed as { name: string; email: string; message: string }
	await sendEmail(data)

	return {
		success: true,
		message: 'Thank you for your message!'
	}
})

2. Use it in your Svelte component

<script lang="ts">
	import { submitContact } from './data.remote.js'
</script>

<form {...submitContact}>
	<input name="name" />
	{#if submitContact.result && 'errors' in submitContact.result}
		<span>{submitContact.result.errors.name?.join(', ')}</span>
	{/if}

	<input name="email" type="email" />
	{#if submitContact.result && 'errors' in submitContact.result}
		<span>{submitContact.result.errors.email?.join(', ')}</span>
	{/if}

	<textarea name="message"></textarea>
	{#if submitContact.result && 'errors' in submitContact.result}
		<span>{submitContact.result.errors.message?.join(', ')}</span>
	{/if}

	<button>Send Message</button>
</form>

{#if submitContact.result?.success === true}
	<p>{submitContact.result.message}</p>
{/if}

How it works

The createValidated function takes your app's form function and returns a validated function. This approach ensures that the package works correctly when installed from npm, as it uses your app's SvelteKit context rather than trying to import from $app/server directly.

When validation fails, the function returns:

{
  success: false,
  errors: Record<string, string[]>,
  data: unknown // The original form data
}

When validation succeeds, your handler is called with the validated data and its return value is passed through.

Using with enhance

The validated form maintains full compatibility with SvelteKit's enhance:

<form
	{...submitContact.enhance(async ({ submit }) => {
		const result = await submit()
		// Handle the result
	})}
>
	<!-- form fields -->
</form>

Using with other validators

Any Standard Schema compatible validator works:

Valibot

import * as v from 'valibot'

const schema = v.object({
	email: v.pipe(v.string(), v.email())
})

export const myForm = validated(schema, async (data) => {
	// ...
})

Arktype

import { type } from 'arktype'

const schema = type({
	email: 'email',
	age: 'number > 0'
})

export const myForm = validated(schema, async (data) => {
	// ...
})

API

createValidated(form)

Creates a validated function using your app's form function.

  • Parameters:
    • form: The form function from $app/server
  • Returns: A validated function

validated(schema, handler)

Creates a form handler with validation.

  • Parameters:
    • schema: A Standard Schema compatible validator
    • handler: An async function that receives validated data
  • Returns: A RemoteForm object (same as SvelteKit's form)

License

MIT