standard-parse
TypeScript icon, indicating that this package has built-in type declarations

0.3.0 • Public • Published

standard-parse

Unified API for any schema that conforms to the Standard Schema specification — works with Zod, Valibot, Arktype, and more.

standard-parse provides a simple, consistent interface (parse, safeParse) for validating and parsing input using any schema that implements the Standard Schema V1 interface.

Why?

Library authors often need to accept schemas from users without knowing which validation library they're using. Instead of supporting each library individually:

// Before: supporting multiple libraries is painful
if (isZodSchema(schema)) {
  return schema.parse(input)
} else if (isValibotSchema(schema)) {
  return parse(schema, input)
} else if (isArktypeSchema(schema)) {
  return schema(input)
}

Use standard-parse to handle any Standard Schema-compliant library:

// After: one API for all
import * as s from "standard-parse"
return s.parse(schema, input)

Perfect for form libraries, API frameworks, configuration parsers, or any library that needs to work with user-provided schemas.

Features

  • Works with Zod, Valibot, Arktype, and more.
  • Unified API: parse(), safeParse(), is()
  • Lightweight abstraction layer
  • Compatible with any schema that implements Standard Schema V1.

Installation

npm install standard-parse

Usage

Zod v4

import * as z from "zod/v4"
import * as s from "standard-parse"

const schema = z.object({ name: z.string() })

const result = s.safeParse(schema, { name: "Alice" })
if (result.issues) {
  console.error("Invalid:", result.issues)
} else {
  console.log("Valid!", result.value)
}

Zod v3

import { z } from "zod/v3"
import * as s from "standard-parse"

const schema = z.object({ name: z.string() })

const result = s.safeParse(schema, { name: "Alice" })
if (result.issues) {
  console.error("Invalid:", result.issues)
} else {
  console.log("Valid!", result.value)
}

Valibot

import * as v from "valibot"
import * as s from "standard-parse"

const schema = v.object({ name: v.string() })
const value = s.parse(schema, { name: "Alice" }) // throws on failure

Arktype

import { type } from "arktype"
import * as s from "standard-parse"

const schema = type({ name: "string" })
const value = s.parse(schema, { name: "Alice" })

API

parse<TSchema>(schema: TSchema, input: unknown): Output<TSchema>

Validates and returns the parsed value, or throws ValidationError on failure.

const output = s.parse(schema, input)

safeParse<TSchema>(schema: TSchema, input: unknown): Result<Output<TSchema>>

Returns an object with either value or issues:

const result = s.safeParse(schema, input)
if (result.issues) {
  // handle errors
} else {
  // result.value is typed
}

is<TSchema>(schema: TSchema, input): input is Output<TSchema>

Validates that a input is the schema, with additional type guard.

if (s.is(schema, input)) {
  // input matches schema
}

Types

import type * as s from "standard-parse"
// s.Schema<TInput, TOutput>
// s.Input<TSchema> // The schema's typescript input values
// s.Output<TSchema> // The schema's typescript output returned from `parse`
// s.Result<s.Output<TSchema>> // Returned by `safeParse`
// s.Issue

These are re-exported from @standard-schema/spec.

Test Matchers

This library also includes some test matchers to make testing schemas easier in test files.

Vitest

Import the matchers in your setup file:

// vitest.setup.ts
import "standard-parse/test-matchers/vitest"

Then use the toMatchSchema matcher in your tests:

import { expect, test } from "vitest"
import * as z from "zod"

const schema = z.object({ name: z.string() })

test("valid input matches schema", () => {
  expect({ name: "Alice" }).toMatchSchema(schema)
})

test("invalid input does not match schema", () => {
  expect({ name: 123 }).not.toMatchSchema(schema)
})

test("with additional checks", () => {
  expect({ name: "Alice" }).toMatchSchema(schema, (parsed) => {
    expect(parsed.name).toBe("Alice")
  })
})

ValidationError

parse will throw a ValidationError on failure:

import * as s from "standard-parse"

try {
  s.parse(schema, badInput)
} catch (err) {
  if (err instanceof s.ValidationError) {
    console.error(err.issues)
  }
}

License (MIT)

Copyright (c) 2025 Catena Labs, Inc. See LICENSE.

Readme

Keywords

none

Package Sidebar

Install

npm i standard-parse

Weekly Downloads

203

Version

0.3.0

License

MIT

Unpacked Size

19.3 kB

Total Files

15

Last publish

Collaborators

  • venables
  • domleboss