validator

package
v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 4, 2026 License: Apache-2.0 Imports: 30 Imported by: 0

Documentation

Overview

Package validator 提供 OFD 文件结构、资源、XML 和签名的校验能力。

Index

Constants

This section is empty.

Variables

View Source
var ToolVersion = version.Version

ToolVersion 是校验器报告中的工具版本。

取自 internal/version,与 --version 输出、查看器关于对话框同源。此前是写死的 "0.0.1",发版时没人跟着动,报告里长期显示着一个早已不存在的版本。

Functions

func RenderJSON

func RenderJSON(w io.Writer, report Report, pretty bool) error

RenderJSON 将报告编码为 JSON;英文枚举用于机器处理,*_zh 字段提供中文标签。

func RenderMarkdown

func RenderMarkdown(w io.Writer, report Report) error

RenderMarkdown 将报告输出为中文 Markdown。

func RenderPDF

func RenderPDF(w io.Writer, report Report, options PDFOptions) error

RenderPDF 将校验报告渲染为可搜索文本的 PDF 文档。

func RenderText

func RenderText(w io.Writer, report Report) error

RenderText 将报告输出为中文纯文本。

func RenderXLSX added in v0.1.2

func RenderXLSX(writer io.Writer, report Report) error

RenderXLSX 将校验报告输出为 Office Open XML 工作簿。

Types

type CheckResult

type CheckResult struct {
	// Name 是校验阶段的英文名称。
	Name string `json:"name"`
	// NameZh 是校验阶段的中文名称。
	NameZh string `json:"name_zh,omitempty"`
	// Status 是校验阶段的英文状态。
	Status string `json:"status"`
	// StatusZh 是校验阶段的中文状态。
	StatusZh string `json:"status_zh,omitempty"`
}

CheckResult 描述一个校验阶段的结果。

type InputInfo

type InputInfo struct {
	// Path 是被校验的输入文件路径或名称。
	Path string `json:"path"`
	// Size 是输入数据的字节数。
	Size int64 `json:"size"`
}

InputInfo 描述被校验的输入文件。

type Issue

type Issue struct {
	// Severity 是问题的英文严重程度。
	Severity Severity `json:"severity"`
	// SeverityZh 是问题严重程度的中文名称。
	SeverityZh string `json:"severity_zh,omitempty"`
	// Stage 是问题所属校验阶段的英文名称。
	Stage Stage `json:"stage"`
	// StageZh 是问题所属校验阶段的中文名称。
	StageZh string `json:"stage_zh,omitempty"`
	// Code 是问题的机器可读代码。
	Code string `json:"code"`
	// EngineCode 是底层校验引擎提供的问题代码。
	EngineCode string `json:"engine_code,omitempty"`
	// Clause 是本问题依据的标准条款,供档案系统按条款筛选与统计。
	//
	// 此前条款号只混在 Message 的自然语言里,JSON 输出无法按条款检索;同一判据
	// 在不同宿主下依据不同条款时列出全部条款。
	Clause []string `json:"clause,omitempty"`
	// Message 是问题的详细描述。
	Message string `json:"message"`
	// Hint 是针对问题的处理提示。
	Hint string `json:"hint,omitempty"`
	// File 是问题所在的包内文件路径。
	File string `json:"file,omitempty"`
	// Path 是问题所在的 XML 路径。
	Path string `json:"path,omitempty"`
	// Line 是问题所在的行号。
	Line int `json:"line,omitempty"`
	// Column 是问题所在的列号。
	Column int `json:"column,omitempty"`
}

Issue 描述一条可定位、可机器读取的校验问题。

type MissingReference added in v0.1.4

type MissingReference struct {
	// From 是引用所在条目。
	From string
	// Value 是引用处的原始文本或属性值。
	Value string
	// Path 是解析后的目标路径。
	Path string
}

MissingReference 描述一处指向包内不存在文件的引用。

func (MissingReference) String added in v0.1.4

func (m MissingReference) String() string

String 按单行输出,便于报告直接引用。

type Mode

type Mode string

Mode 表示校验严格程度。

const (
	ModeStrict     Mode = "strict"
	ModeCompat     Mode = "compat"
	ModeStructural Mode = "structural"
)

type Option

type Option func(*Options)

Option 修改校验器配置。

func WithCheckDigest

func WithCheckDigest(value bool) Option

WithCheckDigest 控制是否校验签名摘要。

func WithCheckProfile added in v0.1.4

func WithCheckProfile(value bool) Option

WithCheckProfile 控制是否执行 profile 校验。

func WithDocType added in v0.1.4

func WithDocType(value string) Option

WithDocType 指定要额外校验的 OFD profile,取值同 OFD.xml 根节点的 DocType 属性。留空表示按文档声明的值自动判定。显式指定可在不改写 DocType 的前提下 预检,例如用 WithDocType(spec.DocTypeOFDA) 检查一份基础 OFD 是否满足 GB/T 42133。

func WithFailOnWarning

func WithFailOnWarning(value bool) Option

WithFailOnWarning 控制是否将警告视为失败。

func WithMaxEntries

func WithMaxEntries(value int) Option

WithMaxEntries 设置 ZIP 条目数量上限。

func WithMaxErrors

func WithMaxErrors(value int) Option

WithMaxErrors 设置最多记录的错误数量;传入 0 表示不限制。

func WithMaxFileSize

func WithMaxFileSize(value int64) Option

WithMaxFileSize 设置单个 ZIP 条目解压后的大小上限。

func WithMaxInputSize

func WithMaxInputSize(value int64) Option

WithMaxInputSize 设置 ZIP 原始输入数据的大小上限;传入 0 表示不限制。

func WithMaxTotalSize

func WithMaxTotalSize(value int64) Option

WithMaxTotalSize 设置整个 OFD 包解压后的大小上限。

func WithMaxXMLBytes

func WithMaxXMLBytes(value int64) Option

WithMaxXMLBytes 设置单个 XML 文件的大小上限。

func WithMaxXMLDepth

func WithMaxXMLDepth(value int) Option

WithMaxXMLDepth 设置单个 XML 文档的嵌套深度上限。

func WithMaxXMLNodes

func WithMaxXMLNodes(value int) Option

WithMaxXMLNodes 设置单个 XML 文档的节点数量上限。

func WithMode

func WithMode(value Mode) Option

WithMode 设置校验模式。

func WithScanXML

func WithScanXML(value bool) Option

WithScanXML 控制是否扫描未被主引用链到达的 XML 文件。

func WithSchemas

func WithSchemas(value *schema.Set) Option

WithSchemas 使用调用方提供的 XSD 模式集合。

func WithSkipXSD

func WithSkipXSD(value bool) Option

WithSkipXSD 控制是否跳过 XSD 校验。

type Options

type Options struct {
	// Mode 指定校验严格程度。
	Mode Mode
	// MaxErrors 指定最多记录的错误数量,0 表示不限制。
	MaxErrors int
	// MaxInputSize 指定 ZIP 原始输入数据的大小上限,0 表示不限制。
	MaxInputSize int64
	// MaxFileSize 指定单个 ZIP 条目解压后的大小上限。
	MaxFileSize int64
	// MaxTotalSize 指定整个 OFD 包解压后的大小上限。
	MaxTotalSize int64
	// MaxEntries 指定 ZIP 条目数量上限。
	MaxEntries int
	// MaxXMLBytes 指定单个 XML 文件的大小上限。
	MaxXMLBytes int64
	// MaxXMLNodes 指定单个 XML 文档的节点数量上限。
	MaxXMLNodes int
	// MaxXMLDepth 指定单个 XML 文档的嵌套深度上限。
	MaxXMLDepth int
	// CheckXSD 指定是否执行 XSD 校验。
	CheckXSD bool
	// ScanXML 指定是否扫描未被主引用链到达的 XML 文件。
	ScanXML bool
	// CheckDigest 指定是否校验签名摘要。
	CheckDigest bool
	// FailOnWarning 指定是否将警告视为失败。
	FailOnWarning bool
	// Schemas 指定使用的 XSD 模式集合。
	Schemas *schema.Set
	// DocType 指定要额外校验的 OFD profile,取值同 OFD.xml 根节点的 DocType
	// 属性。留空表示按文档声明的值自动判定:声明 OFD-A 时应用 GB/T 42133 的
	// 规则,声明 OFD-H 时在其基础上叠加电子病历要求,基础 OFD 不做 profile
	// 校验。
	DocType string
	// CheckProfile 控制是否执行 profile 校验。Profile 非空时会自动启用,
	// 显式设为 false 可只做基础模式校验。
	CheckProfile bool
}

Options 定义校验器的资源限制和检查开关。

type PDFOptions

type PDFOptions struct {
	// Font 指定报告使用的字体文件;未指定时自动查找支持中文的系统字体。
	Font string
}

PDFOptions 配置 PDF 报告的字体。

type ReferenceIndex added in v0.1.4

type ReferenceIndex struct {
	// Reachable 是从 OFD.xml 出发、经嵌套引用可达的条目集合,含 OFD.xml 本身。
	// 目录条目不在其中。
	Reachable map[string]bool
	// Missing 是引用了但包内不存在的条目。它们不改变可达集合,但意味着
	// 输入本身有悬空引用,判断「某条目是否无人引用」时要留意这一前提。
	Missing []MissingReference
	// Escaped 是越过包根目录的非法路径引用。
	Escaped []string
	// Unparsed 是被引用到但无法解析的 XML。命名空间或根元素不符时引用识别
	// 不可信,该文件里的引用全部收集不到。
	//
	// 这一项直接决定能否删除条目:test/testdata/intro.ofd 的命名空间缺
	// "/2016" 后缀,解析失败后它引用的 74 个字体会全部落进「无人引用」,
	// 照单删除就是在真实文件上毁数据。
	Unparsed []string
}

ReferenceIndex 是包内文件引用关系的索引。

它由与文件引用校验同一套逻辑得出:collectReferences 负责识别引用, resolvePackagePath 负责解析路径。两者都不在此处另写一份,否则「校验通过」 与「闭包完整」两个结论会各自漂移,删除无人引用的条目就可能误删在用文件。

func PackageReferences added in v0.1.4

func PackageReferences(ctx context.Context, input any, options ...Option) (*ReferenceIndex, error)

PackageReferences 解析 input 的引用闭包。

遍历只解析引用,不做 XSD、语义与摘要校验,因此比 ValidateReader 便宜得多; 但引用识别与路径解析与校验完全一致。

func (*ReferenceIndex) Complete added in v0.1.4

func (r *ReferenceIndex) Complete() bool

Complete 报告引用闭包是否可据以删除条目。

三类情况都会让闭包不可信:引用缺失、路径越界、被引用文件无法解析。只有 三者皆空时,「某条目不在可达集合中」才等价于「确实无人引用它」。

func (*ReferenceIndex) IncompleteReason added in v0.1.4

func (r *ReferenceIndex) IncompleteReason() string

IncompleteReason 返回闭包不可信的首要原因,供报告直接展示;闭包完整时返回空串。

func (*ReferenceIndex) Unreachable added in v0.1.4

func (r *ReferenceIndex) Unreachable(archive *core.Package) []string

Unreachable 列出包内不在可达集合中的条目,按字典序返回。 目录条目被排除,删除它们没有意义。

type Report

type Report struct {
	// SchemaVersion 是报告模型的版本号。
	SchemaVersion string `json:"schema_version"`
	// Tool 是生成报告的工具信息。
	Tool ToolInfo `json:"tool"`
	// Input 是被校验的输入文件信息。
	Input InputInfo `json:"input"`
	// Status 是整个 OFD 包的校验结论。
	Status Status `json:"status"`
	// StatusZh 是校验结论的中文名称。
	StatusZh string `json:"status_zh,omitempty"`
	// Summary 是报告中的问题和文件数量汇总。
	Summary Summary `json:"summary"`
	// Checks 是各校验阶段的结果列表。
	Checks []CheckResult `json:"checks"`
	// Profile 是本次实际应用的 OFD profile(DocType 取值),为空表示未做
	// profile 校验。校验行为随文件声明变化,报告需显式给出依据。
	//
	// 它记录的是“按哪套规则校验的”,未必等于文件声明的值:用 --doc-type OFD-A
	// 预检一份基础 OFD 时此处为 OFD-A,而文件本身仍声明 OFD。因此这里用
	// Profile 而非 DocType,避免被误读成文件自身的 DocType。
	Profile string `json:"profile,omitempty"`
	// Issues 是校验发现的问题列表。
	Issues []Issue `json:"issues"`
	// StartedAt 是校验开始时间。
	StartedAt time.Time `json:"started_at"`
	// DurationMS 是校验耗时,单位为毫秒。
	DurationMS int64 `json:"duration_ms"`
}

Report 是校验器的统一报告模型。

func (Report) ExitCode

func (r Report) ExitCode(failOnWarning bool) int

ExitCode 返回适合命令行使用的退出码。

func (Report) HasErrors

func (r Report) HasErrors() bool

HasErrors 报告是否包含错误。

func (Report) HasWarnings

func (r Report) HasWarnings() bool

HasWarnings 报告是否包含警告。

type Severity

type Severity string

Severity 表示单条问题的严重程度。

const (
	SeverityError   Severity = "error"
	SeverityWarning Severity = "warning"
	SeverityInfo    Severity = "info"
)

type Stage

type Stage string

Stage 表示问题所属的校验阶段。

const (
	StageContainer Stage = "container"
	StageXML       Stage = "xml"
	StageXSD       Stage = "xsd"
	StageReference Stage = "reference"
	StageSemantic  Stage = "semantic"
	StageProfile   Stage = "profile"
	StageDigest    Stage = "digest"
)

type Status

type Status string

Status 表示整个 OFD 包的校验结论。

const (
	StatusValid   Status = "valid"
	StatusInvalid Status = "invalid"
	StatusPartial Status = "partial"
	StatusError   Status = "error"
)

type Summary

type Summary struct {
	// Errors 是错误数量。
	Errors int `json:"errors"`
	// Warnings 是警告数量。
	Warnings int `json:"warnings"`
	// Infos 是提示数量。
	Infos int `json:"infos"`
	// Files 是包内文件数量。
	Files int `json:"files"`
}

Summary 汇总报告中的问题和文件数量。

type ToolInfo

type ToolInfo struct {
	// Name 是生成报告的工具名称。
	Name string `json:"name"`
	// Version 是生成报告的工具版本。
	Version string `json:"version"`
}

ToolInfo 描述生成报告的工具版本。

type Validator

type Validator struct {
	// contains filtered or unexported fields
}

Validator 执行 OFD 容器、XML、XSD、引用和语义校验。

func New

func New(options ...Option) (*Validator, error)

New 创建一个使用内置 OFD XSD 的校验器。

func (*Validator) ValidatePath

func (v *Validator) ValidatePath(ctx context.Context, filename string) Report

ValidatePath 校验指定路径的 OFD 文件并返回完整报告。

func (*Validator) ValidateReader

func (v *Validator) ValidateReader(ctx context.Context, reader io.Reader, name string) Report

ValidateReader 校验读取器中的 OFD 数据;name 仅用于报告中的输入名称。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL