@molecule/api-utilities-validation

Utility · validation · API (Node) · v1.0.4 · Apache-2.0

Standalone data-validation helpers (schema/shape checks) for API code — distinct from @molecule/api-middleware-validation, the request-validating middleware

npm install @molecule/api-utilities-validation

npm · Source on GitHub

How it works

@molecule/api-utilities-validation is a utility package for the API (Node) side (validation).

import { getValidProps, isEmail } from '@molecule/api-utilities-validation'
import { z } from 'zod'

const schema = z.object({ email: z.string(), name: z.string().min(1) })
const requestBody: unknown = { email: 'ada@example.com', name: 'Ada' }

const props = getValidProps({ name: 'User', schema, props: requestBody })
// throws Error('User.name: Too small: expected string to have >=1 characters')
// on bad input; returns typed props on success
if (!isEmail(props.email)) throw new Error('User.email: invalid')

Reference

Programmatic Zod-validation helpers for molecule API code, plus two dependency-free string validators.

  • getValidProps({ name, schema, props }) — validate resource props; throws a plain Error whose message lists each failure as Name.field: message (the convention api-resource-* handlers surface to clients).
  • validate(schema, data) — parse or throw the raw ZodError.
  • safeParse(schema, data) — non-throwing discriminated union.
  • isEmail(value) / isUuid(value) — regex checks, no Zod involved.

Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit src/index.ts JSDoc, not this file.

Quick Start

import { getValidProps, isEmail } from '@molecule/api-utilities-validation'
import { z } from 'zod'

const schema = z.object({ email: z.string(), name: z.string().min(1) })
const requestBody: unknown = { email: 'ada@example.com', name: 'Ada' }

const props = getValidProps({ name: 'User', schema, props: requestBody })
// throws Error('User.name: Too small: expected string to have >=1 characters')
// on bad input; returns typed props on success
if (!isEmail(props.email)) throw new Error('User.email: invalid')

Type

utility

Installation

npm install @molecule/api-utilities-validation zod

API

Interfaces

ZodError

An Error-like class used to store Zod validation issues.

interface ZodError<T = unknown> extends $ZodError<T> {
  /** @deprecated Use the `z.treeifyError(err)` function instead. */
  format(): core.$ZodFormattedError<T>
  format<U>(mapper: (issue: core.$ZodIssue) => U): core.$ZodFormattedError<T, U>
  /** @deprecated Use the `z.treeifyError(err)` function instead. */
  flatten(): core.$ZodFlattenedError<T>
  flatten<U>(mapper: (issue: core.$ZodIssue) => U): core.$ZodFlattenedError<T, U>
  /** @deprecated Push directly to `.issues` instead. */
  addIssue(issue: core.$ZodIssue): void
  /** @deprecated Push directly to `.issues` instead. */
  addIssues(issues: core.$ZodIssue[]): void
  /** @deprecated Check `err.issues.length === 0` instead. */
  isEmpty: boolean
}

Types

ZodSchema (deprecated)

Deprecated: Use z.ZodType

type ZodSchema<
  Output = unknown,
  Input = unknown,
  Internals extends core.$ZodTypeInternals<Output, Input> = core.$ZodTypeInternals<Output, Input>,
> = ZodType<Output, Input, Internals>

Functions

getValidProps(options)

Validates resource props against a Zod schema, returning typed valid props or throwing a descriptive error with field-level messages prefixed by the resource name.

function getValidProps<Props>({
  name,
  schema,
  props,
}: {
  /**
   * The name of the resource (used in error messages).
   */
  name: string
  /**
   * The Zod schema used when validating props.
   */
  schema: ZodSchema<Props>
  /**
   * The `props` to be validated.
   */
  props: unknown
}): Props
  • options — Validation options.
  • options.name — The resource name (used in error messages, e.g., "User.email: Required").
  • options.schema — The Zod schema to validate against.
  • options.props — The raw props to validate.

Returns: The validated and typed props.

safeParse(schema, data)

Validates data against a Zod schema without throwing, returning a discriminated union.

function safeParse<T>(
  schema: ZodSchema<T>,
  data: unknown,
): { success: true; data: T } | { success: false; error: ZodError }
  • schema — The Zod schema to validate against.
  • data — The raw data to validate.

Returns: { success: true, data } on success, or { success: false, error } with a ZodError.

validate(schema, data)

Validates data against a Zod schema, throwing a ZodError on failure.

function validate<T>(schema: ZodSchema<T>, data: unknown): T
  • schema — The Zod schema to validate against.
  • data — The raw data to validate.

Returns: The validated and typed data.

isEmail(string, strict)

Basic regex for determining whether or not a string is an email address.

function isEmail(string: string, strict?: boolean): boolean
  • string — The string.
  • strict — Whether strict.

Returns: Whether email.

isUuid(uuid)

Basic regex for determining whether or not a string is a UUID.

function isUuid(uuid: string): boolean
  • uuid — The uuid.

Returns: Whether uuid.

Injection Notes

Runtime Dependencies

  • zod

This is the PROGRAMMATIC validation toolkit (call it inside handlers and services). For validating Express request body/params/query as middleware with structured 400 responses, use @molecule/api-middleware-validation instead — the two packages both export a validate, so avoid importing both into one module without aliasing. Zod is a direct dependency (v4); schemas you pass in must come from the same zod major.