Yieldless
Reference

yieldless/schema

Tuple adapters for safeParse(), parse(), safeParseAsync(), and parseAsync() style validators.

yieldless/schema keeps validation inside the same error model as the rest of the library.

Exports

  • parseSafe(schema, input)
  • parseAsyncSafe(schema, input)

Supported schema shapes

  • Objects with safeParse()
  • Objects with parse()
  • Objects with safeParseAsync()
  • Objects with parseAsync()

Example with a safeParse() schema

import { parseSafe } from "yieldless/schema";

const [error, user] = parseSafe(userSchema, input);
if (error) {
  return [error, null] as const;
}

Example with an async parser

const [error, user] = await parseAsyncSafe(userSchema, input);

Why it exists

Most validation libraries are already good at describing schemas. Yieldless does not try to replace them.

Good fits

  • HTTP request validation
  • Environment parsing
  • Normalizing database payloads
  • Decoding IPC input

Good

Validate unknown input before it enters domain code.

const [error, input] = parseSafe(createUserSchema, await request.json());
if (error) {
  return [new ValidationError("Invalid request body", { details: error }), null];
}

return createUser(input);

Use parseAsyncSafe() only when the schema itself performs asynchronous validation.

const [error, user] = await parseAsyncSafe(userSchema, input);

Avoid

Do not validate the same data repeatedly in inner functions.

function renderUser(input: unknown) {
  const [error, user] = parseSafe(userSchema, input);
  if (error) throw error;
  return user.name;
}

Validate once at the boundary, then pass typed values through ordinary code.

On this page