@ formspec/ 配置
FormSpec项目的统一配置和DSL政策验证.
需要项目级别规则时使用此软件包, 例如 :
- 拒绝某些字段类型
- 限制布局嵌入
- 限制选定的字段选项
- 注册扩展名和全项目表格Spec 设置
安装
pnpm add @formspec/config
formspec.config.ts
import { defineFormSpecConfig } from "@formspec/config";
export default defineFormSpecConfig({
constraints: {
fieldTypes: {
dynamicEnum: "warn",
dynamicSchema: "error",
},
layout: {
conditionals: "off",
maxNestingDepth: 2,
},
fieldOptions: {
placeholder: "off",
minItems: "warn",
},
},
});
程序化使用
import { loadFormSpecConfig, validateFormSpecElements } from "@formspec/config";
import { field, formspec } from "@formspec/dsl";
const result = await loadFormSpecConfig();
const constraints = result.found ? result.config.constraints : undefined;
const form = formspec(field.text("name"), field.dynamicEnum("country", "countries"));
const validation = validateFormSpecElements(form.elements, { constraints });
静态非节点导入
主要内容 @formspec/config 输入点不会静态导入节点文件系统模块, 因此面向浏览器的工具可以不拉入便导入策略帮助器 node:fs 或者说 node:path。需要配置发现的主机可以通过 FileSystem 路径和文件操作的适配器 :
import { loadFormSpecConfig, type FileSystem } from "@formspec/config";
const fileSystem: FileSystem = {
exists: async (path) => existsInHost(path),
readFile: async (path) => readFromHost(path),
resolve: (...segments) => resolveInHost(...segments),
dirname: (path) => dirnameInHost(path),
};
await loadFormSpecConfig({ searchFrom: ".", fileSystem });
适配器包括发现,存在检查,工作空间-根部检查,以及文件读取. 评价 TypeScript 配置模块仍然使用当前节点兼容 jiti 加载器;非节点模块评价员是今后的工作.
主要出口
loadFormSpecConfigdefineFormSpecConfigdefineDSLPolicymergeWithDefaultsvalidateFormSpecElementsvalidateFormSpec
许可证
这个包是FormSpec monorepo的一部分,根据MIT许可证发行. 见 自由主义 详细情况.