validator

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2026 License: Apache-2.0 Imports: 29 Imported by: 0

Documentation

Overview

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

Index

Constants

View Source
const ToolVersion = "0.0.1"

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

Variables

This section is empty.

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"`
	// 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 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 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
}

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

type PDFOptions

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

PDFOptions 配置 PDF 报告的字体。

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"`
	// 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"
	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