@basenative/validate

中文文档
3.0.0发布于 2026/10/31Apache-2.0TypeScriptES Module构建来源已验证

Structured validation for BaseNative templates — machine-actionable errors a model can repair from the error object alone

原始使用文档
文档目录6 节
  1. Why this exists
  2. Diagnostic codes
  3. BN_E_CONTROL_FLOW_ON_ELEMENT is the one that matters most
  4. Severity contract
  5. Design notes
  6. License

@basenative/validate

Structured validation for BaseNative templates. Every diagnostic is designed to be repairable by a model from the error object alone, with no access to documentation.

import { validateTemplate } from '@basenative/validate';

validateTemplate('<div v-if="isAdmin">…</div>');
// {
//   valid: false,
//   diagnostics: [{
//     code: 'BN_E_FOREIGN_DIRECTIVE',
//     severity: 'error',
//     message: '"v-if" is Vue syntax; BaseNative does not support it — BaseNative puts
//               conditionals on a <template>, not on the element itself',
//     suggestion: '<template @if="isAdmin">',
//     span: { line: 1, col: 6 },
//     confidence: 'high'
//   }]
// }

Pass a context to also check references:

validateTemplate('<p>{{ itmes }}</p>', { context: { items: [] } });
// → BN_E_UNBOUND_REF, suggestion: 'Did you mean "items"? …'

Why this exists

BaseNative's syntax is a hybrid of four ecosystems: {{ }} from Vue/Angular, @for="item of items; track item.id" near-verbatim from Angular 17, :disabled from Vue, signal()/computed()/effect() from Solid. That familiarity makes it trivially readable — and makes the failure mode confident regression to the nearest neighbour. A model under load emits v-if, or Angular's block form, or $state, rather than inventing something novel.

Reading is not writing. This package measures the difference.

Diagnostic codes

CodeSeverityCatches
BN_E_FOREIGN_DIRECTIVEerrorVue / Angular / Svelte / Alpine syntax, with the BaseNative equivalent
BN_E_CONTROL_FLOW_ON_ELEMENTerror@if/@for/@switch on a non-<template> element
BN_E_UNKNOWN_DIRECTIVEerrorAn @-directive BaseNative does not define
BN_E_EXPR_UNSUPPORTEDerrorExpression outside the CSP-safe subset, or blocked prototype access
BN_E_MALFORMED_FORerror / warning@for not matching item of items; missing track warns
BN_E_ORPHAN_BRANCHerror@else/@empty/@case/@default with no governing directive
BN_E_UNBOUND_REFwarningReference absent from the supplied context

BN_E_CONTROL_FLOW_ON_ELEMENT is the one that matters most

On a <template>, @if is control flow. On any other element, every @name attribute is registered as an event listener. So <div @if="admin"> silently binds an "if" event and never renders conditionally — with no runtime error at all. It is the one mistake in the language that is completely invisible without static validation.

Severity contract

valid is false only when at least one error-severity diagnostic is present.

The validator never reports as an error something the runtime accepts. @for without track renders fine (it forfeits keyed reconciliation), so it warns rather than fails — a validator that disagrees with the thing it validates is worse than no validator.

Design notes

Expression checking imports compileExpression from @basenative/runtime/shared/expression — the same parser the evaluator uses. A second, independent expression parser would drift from the runtime, which is precisely the class of bug this package exists to catch.

The markup scanner is deliberately not a DOM parser: a DOM parse discards byte offsets, normalises casing, and silently repairs malformed markup — which is exactly the input we are here to complain about.

License

Apache-2.0

版本与发布记录

@basenative/validate 共发布 11 个版本,首次发布于 2026/10/18;过去 12 个月发布了 11 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.0.0latest2026/10/312 天前
2.4.02026/10/268 天前
2.3.22026/10/2212 天前
2.3.12026/10/2212 天前
2.3.02026/10/2212 天前
2.2.02026/10/2113 天前

关于 @basenative/validate 的常见问题

@basenative/validate 是什么?

BaseNative模板的结构化验证——模型可以仅从错误对象修复机器可操作的错误

如何安装 @basenative/validate?

在项目目录运行 npm install @basenative/validate,或使用 pnpm add / yarn add / bun add。

@basenative/validate 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@basenative/validate 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@basenative/validate 可以商用吗?

@basenative/validate 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@basenative/validate 还在维护吗?

最新版本 3.0.0 发布于 2026/10/31(2 天前);至今共发布 11 个版本,其中过去 12 个月发布了 11 个;首次发布于 2026/10/18。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。