Skip to content

Latest commit

 

History

History
81 lines (61 loc) · 1.15 KB

README.md

File metadata and controls

81 lines (61 loc) · 1.15 KB

Bodek

A zod-like schema validator, 100% type-safe.

Made for fun and learning purposes.

Installation

npm i bodek

Usage

The API is basically the same as Zod's, but only with the core functionality (again, learning purposes...)

parse:

import { b } from 'bodek';

const schema = b.object({
  name: b.string().min(3).max(10),
  age: b.number().min(18),
});

// No errors
schema.parse({
  name: 'John',
  age: 30,
});

// Throws an error
schema.parse({
  name: 'John',
  age: 10,
});

safeParse:

import { b } from 'bodek';

const schema = b.object({
  name: b.string().min(3).max(10),
  age: b.number().min(18),
});

const { success, data, error } = schema.safeParse({
  name: 'John',
  age: 30,
});

if (success) {
  console.log(data);
} else {
  console.error(error);
}

refine:

import { b } from 'bodek';

const schema = b.object({
  name: b
    .string()
    .min(3)
    .max(10)
    .refine((name) => name.toLowerCase() !== 'john', 'Name cannot be John'),
  age: b.number().min(18),
});

// Throws an error
schema.parse({
  name: 'John',
  age: 30,
});