phel-lang / phel-schema
phel 中的模式验证库。该库受到 zod 的启发
v0.0.0
2024-06-24 15:57 UTC
Requires
- php: >=8.2
- phel-lang/phel-lang: ^0.15
README
phel 中的模式验证库。该库受到 zod 的启发
基本用法
创建简单的字符串模式
(ns app (:require phel\schema :as z)) # creating a schema for strings (def my-schema (z/string)) # parsing (z/parse my-schema "tuna") # => "tuna" (z/parse my-schema 12) # => throws ZodError # "safe" parsing (doesn't throw error if validation fails) (z/safe-parse my-schema "tuna") # => {:success true :data "tuna"} (z/safe-parse my-schema 12) # => {:success false :error ZodError :issues [...]}
(ns app (:require phel\schema :as z)) # creating a schema for strings (def my-schema (as-> (z/string) s (z/min s 3) (z/max s 10) (z/regex s "/^t/"))) # parsing (z/parse my-schema "tuna") # => "tuna" (z/parse my-schema "a tuna") # => throws ZodError
创建对象模式
(ns app (:require phel\schema :as z)) (def user-schama (z/object { :username (z/string) })) (z/parse user-schama {:username "Ludwig"}) # extract the inferred type (z/infer user-schama) # { username: string }
原始数据类型
(ns app (:require phel\schema :as z)) # primitive values (z/string) (z/number) (z/bigint) (z/boolean) (z/date) (z/symbol) # empty types (z/undefined) (z/null) (z/void) # accepts undefined # catch-all types # allows any value (z/any) (z/unknown) # never type # allows no values (z/never)
开发
打开壳
docker compose build docker compose run --rm php_cli bash
测试
# vendor/bin/phel test