Skip to main content
Module

x/valita/mod.ts>ValitaResult

A typesafe validation & parsing library for TypeScript.
Latest
type alias ValitaResult
import { type ValitaResult } from "https://deno.land/x/valita@v0.3.8/mod.ts";

A validation/parsing success or failure.

Used by parsing-related methods where and both success and failure cases are returned as values (instead of raising an exception on failure). The most notable example is the Type.try(...) method.

The .ok property can to assert whether the value represents a success or failure and access further information in a typesafe way.

Examples

Example 1

const t = v.string();

// Make parsing fail or succeed about equally.
const result = t.try(Math.random() < 0.5 ? "hello" : null);

if (result.ok) {
  // TypeScript allows accessing .value within this code block.
  console.log(`Success: ${result.value}`);
} else {
  // TypeScript allows accessing .message within this code block.
  console.log(`Failed: ${result.message}`);
}
definition: Ok<V> | Err