Skip to content

Instantly share code, notes, and snippets.

@o-henry
Last active October 4, 2022 03:10
Show Gist options
  • Save o-henry/b648181298c64e06bf982c2f0a7275e0 to your computer and use it in GitHub Desktop.
Save o-henry/b648181298c64e06bf982c2f0a7275e0 to your computer and use it in GitHub Desktop.
// schema validation
import { z } from "zod"
enum Risk {
Low,
Medium,
High,
}
const _CalculateRiskProfile = (age: _Age): Risk => (__Age.parse(age) < 60 ? Risk.Low : Risk.Medium)
const __Age = z.number().refine((age: number) => 0 <= age && age < 120)
type _Age = z.infer<typeof __Age>
console.log(_CalculateRiskProfile(70)) // 1
console.log(_CalculateRiskProfile(9999)) // Error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment