@tola/schema

声明、组合、检查并执行元数据与其他 Typst 值的校验 schema。

导入

typst
#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,诊断会自动定位到源文件。

示例

为文章元数据声明默认值与约束

typst
#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)

导出成员

any

typc
any: value (defined by package)

原样接受任意 Typst 值的 schema。

array-of

typc
let array-of(
  member: type | dictionary,
) = dictionary;

校验数组,要求每个元素均满足 member。

位置参数

  • member type | dictionary 必填:数组元素的类型或 schema。

at-least

typc
let at-least(
  member: type | dictionary,
  minimum: int | float,
) = dictionary;

要求数值(int 或有限 float)大于等于 minimum。

位置参数

  • member type | dictionary 必填:内部数值 schema。
  • minimum int | float 必填:闭区间下限。

at-most

typc
let at-most(
  member: type | dictionary,
  maximum: int | float,
) = dictionary;

要求数值(int 或有限 float)小于等于 maximum。

位置参数

  • member type | dictionary 必填:内部数值 schema。
  • maximum int | float 必填:闭区间上限。

check

typc
let check(
  member: type | dictionary,
  inspect: function,
  on: none | array = none,
) = dictionary;

为 member 附加可返回多条诊断的自定义检查函数。

位置参数

  • member type | dictionary 必填:先执行的内部类型或 schema。
  • inspect function 必填:返回零条或多条 issue(...) 的函数 (value) => array。

命名参数

  • on none | array 默认值: none:可选的附加字段路径或键名。

convert

typc
let convert(
  member: type | dictionary,
  convert-value: function | type,
  output: type | dictionary = any,
) = dictionary;

在通过 member 校验后,用可失败的转换函数返回 ok(...) 或 err(...)。

位置参数

  • member type | dictionary 必填:先执行的内部类型或 schema。
  • convert-value function | type 必填:返回 ok(v) 或 err(...) 的转换函数。

命名参数

  • output type | dictionary 默认值: any:可选输出类型 schema,供 inspect 描述输出形状。

date

typc
date: value (defined by package)

将 "YYYY-MM-DD" 或 Typst datetime 解析为日历日期(Typst datetime)的内置 schema。

defaults

typc
let defaults(
  member: type | dictionary,
) = dictionary;

根据对象 schema 生成默认字典(要求每个字段均为可选或带有默认值)。

位置参数

  • member type | dictionary 必填:待提取默认值的对象 schema。

describe

typc
let describe(
  member: type | dictionary,
  description: string,
) = dictionary;

为 member 附加供 inspect 读取的说明文字。

位置参数

  • member type | dictionary 必填:内部类型或 schema。
  • description string 必填:非空说明字符串。

dictionary-of

typc
let dictionary-of(
  member: type | dictionary,
) = dictionary;

校验字典,要求每个键值均满足 member。

位置参数

  • member type | dictionary 必填:字典值对应的类型或 schema。

email

typc
email: value (defined by package)

校验电子邮箱格式字符串的内置 schema。

enum-of

typc
let enum-of(
  values: array,
) = dictionary;

匹配等于 values 数组中任意一项的值。

位置参数

  • values array 必填:允许值组成的非空数组。

err

typc
let err(
  ..issues: arguments,
) = dictionary;

在 convert 回调中包装一条或多条校验错误。

可变参数

  • ..issues arguments:一条或多条 issue(...) 记录(或消息字符串)。

format-issues

typc
let format-issues(
  issues: array,
) = string;

将 try-parse 返回的错误数组格式化为易读的多行文本。

位置参数

  • issues array 必填:错误记录数组。

http-url

typc
http-url: value (defined by package)

校验绝对 http:// 或 https:// URL 的内置 schema。

https-url

typc
https-url: value (defined by package)

校验绝对 https:// URL 的内置 schema。

inspect

typc
let inspect(
  member: type | dictionary,
  input: bool = false,
) = dictionary;

不解析实际数据,直接返回 schema 的字段、类型、默认值与说明结构。

位置参数

  • member type | dictionary 必填:待检查的 schema。

命名参数

  • input bool 默认值: false:设为 true 返回转换前的输入形状;false(默认)返回输出形状。

ip

typc
ip: value (defined by package)

同时接受 IPv4 与 IPv6 地址的内置 schema。

ipv4

typc
ipv4: value (defined by package)

校验点分十进制 IPv4 地址的内置 schema。

ipv6

typc
ipv6: value (defined by package)

校验完整或 :: 压缩 IPv6 地址的内置 schema。

issue

typc
let issue(
  message: string,
  code: string = "custom",
  notes: array = (),
  path: array = (),
) = dictionary;

构造供 check 或 convert 返回的结构化校验错误。

位置参数

  • message string 必填:面向作者的问题描述。

命名参数

  • code string 默认值: "custom":可选的错误代码标识。
  • path array 默认值: ():可选的字段或索引路径数组。
  • notes array 默认值: ():可选的补充说明字符串数组。

lazy

typc
let lazy(
  factory: function,
) = dictionary;

延迟构造 schema,用于定义递归数据结构。

位置参数

  • factory function 必填:按需调用的无参函数 () => schema。

literal

typc
let literal(
  value: any,
) = dictionary;

仅匹配单个精确的 Typst 常量值。

位置参数

  • value any 必填:期望匹配的精确值。

map

typc
let map(
  member: type | dictionary,
  convert-value: function | type,
  output: type | dictionary = any,
) = dictionary;

在通过 member 校验后,用不会产生 schema 错误的转换函数映射值。

位置参数

  • member type | dictionary 必填:先执行的内部类型或 schema。
  • convert-value function | type 必填:转换函数 (value) => new-value。

命名参数

  • output type | dictionary 默认值: any:可选输出类型 schema,供 inspect 描述输出形状。

matches

typc
let matches(
  member: type | dictionary,
  expression: regex,
) = dictionary;

要求字符串匹配指定的正则表达式。

位置参数

  • member type | dictionary 必填:内部字符串 schema。
  • expression regex 必填:Typst regex(...) 或正则字符串。

max-length

typc
let max-length(
  member: type | dictionary,
  maximum: int,
) = dictionary;

要求字符串、数组、字典或字节长度不大于 maximum。

位置参数

  • member type | dictionary 必填:内部类型或 schema。
  • maximum int 必填:非负最大长度。

min-length

typc
let min-length(
  member: type | dictionary,
  minimum: int,
) = dictionary;

要求字符串、数组、字典或字节长度不小于 minimum。

位置参数

  • member type | dictionary 必填:内部类型或 schema。
  • minimum int 必填:非负最小长度。

non-empty

typc
let non-empty(
  member: type | dictionary,
) = dictionary;

在通过 member 校验后,拒绝空字符串、空数组、空字典或空字节。

位置参数

  • member type | dictionary 必填:内部类型或 schema。

nullable

typc
let nullable(
  member: type | dictionary,
) = dictionary;

在 member 允许的值之外,额外接受显式 none。

位置参数

  • member type | dictionary 必填:值不为 none 时应用的内部类型或 schema。

ok

typc
let ok(
  value: any,
) = dictionary;

在 convert 回调中包装转换成功的结果值。

位置参数

  • value any 必填:转换后的值。

one-or-many

typc
let one-or-many(
  member: type | dictionary,
) = dictionary;

接受单个 member 值或 member 数组,统一规范化为数组。

位置参数

  • member type | dictionary 必填:元素类型或 schema。

optional

typc
let optional(
  member: type | dictionary,
  ..options: arguments,
) = dictionary;

允许字典字段缺失,并可在缺失时提供默认值。

位置参数

  • member type | dictionary 必填:字段存在时应用的内部类型或 schema。

可变参数

  • ..options arguments:可选具名参数 default: 值,字段缺失时采用(默认值本身须满足 member)。字段已提供但值无效时会报错,不会回退到默认值。

parse

typc
let parse(
  value: any,
  member: type | dictionary,
) = any;

按 member 校验 value,成功返回解析结果,失败则抛出格式化错误。

位置参数

  • value any 必填:待校验的输入值。
  • member type | dictionary 必填:类型或 schema 声明。

partial

typc
let partial(
  member: type | dictionary,
  ..fixed: arguments,
) = function;

将对象 schema 的全部字段变为可选(fixed 列出的字段除外)。

位置参数

  • member type | dictionary 必填:由 schema(...) 创建的对象 schema。

可变参数

  • ..fixed arguments:保持必填的可选字段名列表。

partial-date

typc
partial-date: value (defined by package)

将年份("YYYY")、年月("YYYY-MM")或完整日期解析为 (year:, month:, day:, precision:, iso:) 的内置 schema。

refine

typc
let refine(
  member: type | dictionary,
  predicate: function,
  message: string = none,
) = dictionary;

为 member 附加布尔谓词校验。

位置参数

  • member type | dictionary 必填:先执行的内部类型或 schema。
  • predicate function 必填:判断合法性的函数 (value) => bool。

命名参数

  • message string 默认值: none:谓词返回 false 时的可选错误消息。

schema

typc
let schema(
  fields: dictionary,
  unknown: string = "error",
) = dictionary;

声明字典字段到类型或子 schema 的映射规则。

位置参数

  • fields dictionary 必填:字段名到 Typst 类型或 @tola/schema 规则的字典。

命名参数

  • unknown string 默认值: "error":未声明字段的处理方式:"error"(默认,报错)、"strip"(丢弃)或 "keep"(原样保留)。

timestamp

typc
timestamp: value (defined by package)

将带时区偏移的 RFC 3339 时间戳字符串解析为 (datetime:, offset:, rfc3339:) 的内置 schema。

可直接将返回值的 .rfc3339 传给 @tola/feed#feed 或 @tola/sitemap#sitemap。

trim

typc
let trim(
  member: type | dictionary,
) = dictionary;

先去除字符串首尾空白,再交给 member 校验。

位置参数

  • member type | dictionary 必填:内部字符串 schema(通常为 str)。

try-parse

typc
let try-parse(
  value: any,
  member: type | dictionary,
) = dictionary;

按 member 校验 value 而不中断求值,返回 (ok: true, value: ...) 或 (ok: false, issues: ...)。

位置参数

  • value any 必填:待校验的输入值。
  • member type | dictionary 必填:类型或 schema 声明。

tuple

typc
let tuple(
  ..members: arguments,
  rest: none | type | dictionary = none,
) = dictionary;

校验固定位置数组,各位置独立声明 schema,并支持可选尾部 rest 规则。

可变参数

  • ..members arguments:按索引顺序传入的各必填位置 schema。

命名参数

  • rest none | type | dictionary 默认值: none:超出固定位置后的剩余元素 schema;none(默认)拒绝多余元素。

union

typc
let union(
  ..members: arguments,
) = dictionary;

按顺序尝试多个候选 schema,采用首个匹配项。

可变参数

  • ..members arguments:两个及以上按从左到右尝试的候选类型或 schema。

url-path

typc
url-path: value (defined by package)

校验站点根 URL 路径(如固定链接)并解码为可传给 output-of 或 route-url 的路由。

variant

typc
let variant(
  tag-key: string,
  ..branches: arguments,
) = dictionary;

根据判别字段(tag-key)的值选择对应的分支对象 schema。

位置参数

  • tag-key string 必填:用于区分分支的字段名。

可变参数

  • ..branches arguments:多个候选 schema(...),各分支通过 literal(...) 或 enum-of(...) 固定 tag-key。

站内搜索

可以用上下方向键选择结果。

输入关键词开始搜索。