SuperAPI AI 工具集

custom-format-validators

编程与开发

查看项目

更新于 10-15 23:40

技能介绍

用途:在 z-schema 中注册和管理自定义 format 校验器,用于 JSON Schema 的 format 关键字约束。

主要能力:内置支持 date、date-time、time、email、idn-email、hostname、host-name、idn-hostname、ipv4、ipv6、uri、strict-uri、uri-reference、uri-template、iri、iri-reference、json-pointer、relative-json-pointer、regex、duration、uuid。可注册同步函数(返回 boolean)或异步函数(返回 Promise<boolean>)。

用法:可用 ZSchema.registerFormat 做全局注册,或用实例的 validator.registerFormat,实例格式会覆盖同名全局格式;也可在 ZSchema.create({ customFormats: {...} }) 中传入;传 null 可禁用内置或全局格式。异步校验器需以 { async: true } 创建,validate 返回 Promise;默认异步超时为 2000 毫秒,可用 asyncTimeout 提高,超时产生 ASYNC_TIMEOUT 错误。format 断言行为随草稿变化,可用 formatAssertions 控制(null 为旧式始终断言,true 尊重现代草稿,false 仅作注解)。draft-04/06/07 默认对未知格式报 UNKNOWN_FORMAT,可用 ignoreUnknownFormats 抑制。可用 unregisterFormat 注销,用 getRegisteredFormats、getSupportedFormats、isFormatSupported 查询格式。

场景:校验邮编、电话、E.164、ISO 日期、来自外部列表的业务规则等。注意:非适用类型应返回 true;格式校验器可修改对象但需谨慎;数据库等异步查询需在 async 模式下使用。具体 API 细节与更多限制资料中暂未提供。