A function that takes a JSON string and returns a Result
with the parsed
object or a list of errors.
import { json } from "typerun";
import { object, string } from "typerun/schema";
import { isOk } from "typerun/result";
const schema = object({ name: string });
try {
const result = json(schema, { throwing: true })(`{"name": "Alice"}`);
console.log(result.name); // "Alice"
} catch (errors) {
console.error(errors);
}
A function that takes a JSON string and returns the parsed object or throws an array of errors if the validation fails.
The
json
function is a parsing utility to get a typed and runtime-validated object out of a JSON string. It exists in two versions: one that returns aResult
and one that throws an error if the validation fails.Safe version
Example