@tola/schema
声明、组合、检查并执行元数据与其他 Typst 值的校验 schema。
导入
#import "@tola/schema:0.0.0": *向 schema(...) 传入字段规则字典,每个字段可填 Typst 类型(如 str、int)或规则函数(如 array-of(str))。包装器由内向外求值:例如 non-empty(trim(str)) 先去掉首尾空白,再拒绝空字符串。
用 parse 直接返回校验结果或在失败时报错;用 try-parse 捕获 (ok:, value:) / (ok:, issues:) 自行处理。校验内容文件元数据时,优先使用 @tola/source#parse-sources,诊断会自动定位到源文件。
示例
为文章元数据声明默认值与约束
#import "@tola/schema:0.0.0": array-of, date, non-empty, optional, parse, schema, trim
#let post-schema = schema((
title: non-empty(trim(str)),
draft: optional(bool, default: false),
published: optional(date),
tags: optional(array-of(non-empty(trim(str))), default: ()),
))
#let meta = parse((title: " 笔记 "), post-schema)导出成员
anyarray-ofat-leastat-mostcheckconvertdatedefaultsdescribedictionary-ofemailenum-oferrformat-issueshttp-urlhttps-urlinspectipipv4ipv6issuelazyliteralmapmatchesmax-lengthmin-lengthnon-emptynullableokone-or-manyoptionalparsepartialpartial-daterefineschematimestamptrimtry-parsetupleunionurl-pathvariant
any
any: value (defined by package)原样接受任意 Typst 值的 schema。
array-of
let array-of(
member: type | dictionary,
) = dictionary;校验数组,要求每个元素均满足 member。
位置参数
membertype | dictionary必填:数组元素的类型或 schema。
at-least
let at-least(
member: type | dictionary,
minimum: int | float,
) = dictionary;要求数值(int 或有限 float)大于等于 minimum。
位置参数
membertype | dictionary必填:内部数值 schema。minimumint | float必填:闭区间下限。
at-most
let at-most(
member: type | dictionary,
maximum: int | float,
) = dictionary;要求数值(int 或有限 float)小于等于 maximum。
位置参数
membertype | dictionary必填:内部数值 schema。maximumint | float必填:闭区间上限。
check
let check(
member: type | dictionary,
inspect: function,
on: none | array = none,
) = dictionary;为 member 附加可返回多条诊断的自定义检查函数。
位置参数
membertype | dictionary必填:先执行的内部类型或 schema。inspectfunction必填:返回零条或多条issue(...)的函数(value) => array。
命名参数
onnone | array默认值:none:可选的附加字段路径或键名。
convert
let convert(
member: type | dictionary,
convert-value: function | type,
output: type | dictionary = any,
) = dictionary;在通过 member 校验后,用可失败的转换函数返回 ok(...) 或 err(...)。
位置参数
membertype | dictionary必填:先执行的内部类型或 schema。convert-valuefunction | type必填:返回ok(v)或err(...)的转换函数。
命名参数
outputtype | dictionary默认值:any:可选输出类型 schema,供inspect描述输出形状。
date
date: value (defined by package)将 "YYYY-MM-DD" 或 Typst datetime 解析为日历日期(Typst datetime)的内置 schema。
defaults
let defaults(
member: type | dictionary,
) = dictionary;根据对象 schema 生成默认字典(要求每个字段均为可选或带有默认值)。
位置参数
membertype | dictionary必填:待提取默认值的对象 schema。
describe
let describe(
member: type | dictionary,
description: string,
) = dictionary;为 member 附加供 inspect 读取的说明文字。
位置参数
membertype | dictionary必填:内部类型或 schema。descriptionstring必填:非空说明字符串。
dictionary-of
let dictionary-of(
member: type | dictionary,
) = dictionary;校验字典,要求每个键值均满足 member。
位置参数
membertype | dictionary必填:字典值对应的类型或 schema。
email
email: value (defined by package)校验电子邮箱格式字符串的内置 schema。
enum-of
let enum-of(
values: array,
) = dictionary;匹配等于 values 数组中任意一项的值。
位置参数
valuesarray必填:允许值组成的非空数组。
err
let err(
..issues: arguments,
) = dictionary;在 convert 回调中包装一条或多条校验错误。
可变参数
..issuesarguments:一条或多条issue(...)记录(或消息字符串)。
format-issues
let format-issues(
issues: array,
) = string;将 try-parse 返回的错误数组格式化为易读的多行文本。
位置参数
issuesarray必填:错误记录数组。
http-url
http-url: value (defined by package)校验绝对 http:// 或 https:// URL 的内置 schema。
https-url
https-url: value (defined by package)校验绝对 https:// URL 的内置 schema。
inspect
let inspect(
member: type | dictionary,
input: bool = false,
) = dictionary;不解析实际数据,直接返回 schema 的字段、类型、默认值与说明结构。
位置参数
membertype | dictionary必填:待检查的 schema。
命名参数
inputbool默认值:false:设为true返回转换前的输入形状;false(默认)返回输出形状。
ip
ip: value (defined by package)同时接受 IPv4 与 IPv6 地址的内置 schema。
ipv4
ipv4: value (defined by package)校验点分十进制 IPv4 地址的内置 schema。
ipv6
ipv6: value (defined by package)校验完整或 :: 压缩 IPv6 地址的内置 schema。
issue
let issue(
message: string,
code: string = "custom",
notes: array = (),
path: array = (),
) = dictionary;构造供 check 或 convert 返回的结构化校验错误。
位置参数
messagestring必填:面向作者的问题描述。
命名参数
codestring默认值:"custom":可选的错误代码标识。patharray默认值:():可选的字段或索引路径数组。notesarray默认值:():可选的补充说明字符串数组。
lazy
let lazy(
factory: function,
) = dictionary;延迟构造 schema,用于定义递归数据结构。
位置参数
factoryfunction必填:按需调用的无参函数() => schema。
literal
let literal(
value: any,
) = dictionary;仅匹配单个精确的 Typst 常量值。
位置参数
valueany必填:期望匹配的精确值。
map
let map(
member: type | dictionary,
convert-value: function | type,
output: type | dictionary = any,
) = dictionary;在通过 member 校验后,用不会产生 schema 错误的转换函数映射值。
位置参数
membertype | dictionary必填:先执行的内部类型或 schema。convert-valuefunction | type必填:转换函数(value) => new-value。
命名参数
outputtype | dictionary默认值:any:可选输出类型 schema,供inspect描述输出形状。
matches
let matches(
member: type | dictionary,
expression: regex,
) = dictionary;要求字符串匹配指定的正则表达式。
位置参数
membertype | dictionary必填:内部字符串 schema。expressionregex必填:Typstregex(...)或正则字符串。
max-length
let max-length(
member: type | dictionary,
maximum: int,
) = dictionary;要求字符串、数组、字典或字节长度不大于 maximum。
位置参数
membertype | dictionary必填:内部类型或 schema。maximumint必填:非负最大长度。
min-length
let min-length(
member: type | dictionary,
minimum: int,
) = dictionary;要求字符串、数组、字典或字节长度不小于 minimum。
位置参数
membertype | dictionary必填:内部类型或 schema。minimumint必填:非负最小长度。
non-empty
let non-empty(
member: type | dictionary,
) = dictionary;在通过 member 校验后,拒绝空字符串、空数组、空字典或空字节。
位置参数
membertype | dictionary必填:内部类型或 schema。
nullable
let nullable(
member: type | dictionary,
) = dictionary;在 member 允许的值之外,额外接受显式 none。
位置参数
membertype | dictionary必填:值不为none时应用的内部类型或 schema。
ok
let ok(
value: any,
) = dictionary;在 convert 回调中包装转换成功的结果值。
位置参数
valueany必填:转换后的值。
one-or-many
let one-or-many(
member: type | dictionary,
) = dictionary;接受单个 member 值或 member 数组,统一规范化为数组。
位置参数
membertype | dictionary必填:元素类型或 schema。
optional
let optional(
member: type | dictionary,
..options: arguments,
) = dictionary;允许字典字段缺失,并可在缺失时提供默认值。
位置参数
membertype | dictionary必填:字段存在时应用的内部类型或 schema。
可变参数
..optionsarguments:可选具名参数default: 值,字段缺失时采用(默认值本身须满足member)。字段已提供但值无效时会报错,不会回退到默认值。
parse
let parse(
value: any,
member: type | dictionary,
) = any;按 member 校验 value,成功返回解析结果,失败则抛出格式化错误。
位置参数
valueany必填:待校验的输入值。membertype | dictionary必填:类型或 schema 声明。
partial
let partial(
member: type | dictionary,
..fixed: arguments,
) = function;将对象 schema 的全部字段变为可选(fixed 列出的字段除外)。
位置参数
membertype | dictionary必填:由schema(...)创建的对象 schema。
可变参数
..fixedarguments:保持必填的可选字段名列表。
partial-date
partial-date: value (defined by package)将年份("YYYY")、年月("YYYY-MM")或完整日期解析为 (year:, month:, day:, precision:, iso:) 的内置 schema。
refine
let refine(
member: type | dictionary,
predicate: function,
message: string = none,
) = dictionary;为 member 附加布尔谓词校验。
位置参数
membertype | dictionary必填:先执行的内部类型或 schema。predicatefunction必填:判断合法性的函数(value) => bool。
命名参数
messagestring默认值:none:谓词返回false时的可选错误消息。
schema
let schema(
fields: dictionary,
unknown: string = "error",
) = dictionary;声明字典字段到类型或子 schema 的映射规则。
位置参数
fieldsdictionary必填:字段名到 Typst 类型或@tola/schema规则的字典。
命名参数
unknownstring默认值:"error":未声明字段的处理方式:"error"(默认,报错)、"strip"(丢弃)或"keep"(原样保留)。
timestamp
timestamp: value (defined by package)将带时区偏移的 RFC 3339 时间戳字符串解析为 (datetime:, offset:, rfc3339:) 的内置 schema。
可直接将返回值的 .rfc3339 传给 @tola/feed#feed 或 @tola/sitemap#sitemap。
trim
let trim(
member: type | dictionary,
) = dictionary;先去除字符串首尾空白,再交给 member 校验。
位置参数
membertype | dictionary必填:内部字符串 schema(通常为str)。
try-parse
let try-parse(
value: any,
member: type | dictionary,
) = dictionary;按 member 校验 value 而不中断求值,返回 (ok: true, value: ...) 或 (ok: false, issues: ...)。
位置参数
valueany必填:待校验的输入值。membertype | dictionary必填:类型或 schema 声明。
tuple
let tuple(
..members: arguments,
rest: none | type | dictionary = none,
) = dictionary;校验固定位置数组,各位置独立声明 schema,并支持可选尾部 rest 规则。
可变参数
..membersarguments:按索引顺序传入的各必填位置 schema。
命名参数
restnone | type | dictionary默认值:none:超出固定位置后的剩余元素 schema;none(默认)拒绝多余元素。
union
let union(
..members: arguments,
) = dictionary;按顺序尝试多个候选 schema,采用首个匹配项。
可变参数
..membersarguments:两个及以上按从左到右尝试的候选类型或 schema。
url-path
url-path: value (defined by package)校验站点根 URL 路径(如固定链接)并解码为可传给 output-of 或 route-url 的路由。
variant
let variant(
tag-key: string,
..branches: arguments,
) = dictionary;根据判别字段(tag-key)的值选择对应的分支对象 schema。
位置参数
tag-keystring必填:用于区分分支的字段名。
可变参数
..branchesarguments:多个候选schema(...),各分支通过literal(...)或enum-of(...)固定tag-key。