Skip to content

Instantly share code, notes, and snippets.

View colinhacks's full-sized avatar

Colin McDonnell colinhacks

View GitHub Profile

Adapted from this recommendation by @jandockx

Cyclical objects

Despite supporting recursive schemas, passing cyclical data into Zod will cause an infinite loop in some cases.

You can protect against cyclical objects starting an infinite loop (at a performance cost) with the following approach (using the above jsonSchema as an example):

@colinhacks
colinhacks / script.sh
Last active April 23, 2024 19:55
Repro portability error
gh repo clone https://github.com/t3-oss/t3-env t3env
cd t3env
git checkout @t3-oss/env-nextjs@0.9.2
pnpm i
pnpm update zod --latest --recursive
pnpm run build
import Benchmark from "benchmark";
const datetimeValidationSuite = new Benchmark.Suite("datetime");
const DATA = "2020-01-01";
const MONTHS_31 = new Set([1, 3, 5, 7, 8, 10, 12]);
const MONTHS_30 = new Set([4, 6, 9, 11]);
const simpleDatetimeRegex = /^(\d{4})-(\d{2})-(\d{2})$/;
const datetimeRegexNoLeapYearValidation =
@colinhacks
colinhacks / index.tsx
Created September 14, 2023 20:52
CrappyNext.js
import { renderToString } from "react-dom/server";
const router = new Bun.FileSystemRouter({
style: "nextjs",
dir: "./pages",
});
Bun.serve({
port: 3000,
development: true,
@colinhacks
colinhacks / package.json
Last active September 8, 2023 16:55
Script runner benchmark
{
"name": "sayhi",
"module": "index.ts",
"type": "module",
"dependencies": {
"cowsay": "^1.5.0"
},
"scripts": {
"hi": "echo 'hi'"
}
@colinhacks
colinhacks / bench.sh
Last active January 5, 2024 09:37
Benchmark testing libraries against Zod's test suite
# requires hyperfine and Bun v0.7 or later
# https://github.com/sharkdp/hyperfine
git clone git@github.com:colinhacks/zod.git
cd zod
bun install
hyperfine --warmup 3 --runs 10 \
"bun test src" \
"npx vitest --config configs/vitest.config.ts" \
"npx jest -c configs/babel-jest.config.json" \
@colinhacks
colinhacks / index.html
Created May 13, 2022 04:13
Minimal docsify index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>user/repo</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta
name="description"
content="This is a description of user/repo"
/>
@colinhacks
colinhacks / log_json_shortcuts.json
Created January 16, 2022 23:50
VSCode shortcuts for console.log and JSON.stringify
// keybindings.json
// Command Pallette > Open Keyboard Shortcuts (JSON)
[
{
"key": "cmd+shift+l",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus",
"args": {
"snippet": "console.log(${TM_SELECTED_TEXT}$1)$0;"
@colinhacks
colinhacks / ultimate_eslint.md
Last active April 23, 2021 03:12
Ultimate ESLint + Prettier setup

The Ultimate ESLint and Prettier setup

Featuring:

  • TypeScript support
  • Prettier auto-formatting
  • auto-removal of unused imports
  • auto-sorting of all imports
  • auto-consolidation of imports

Install deps

type Generics = Partial<{
asdf:string;
qwer : number;
}>
type Defaults = Required<{
asdf: 'default';
qwer: 1234;
}>;