metanorma-standoc 3.5.17 → 3.5.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e82d9420a36975e6794ba44562cc468210ff4194b07d8026f700ea203cccc058
|
|
4
|
+
data.tar.gz: 389ff5715f3c6ea34c1f797f5dcdeef0d6b7a1afbcde003ae4b3abf588db38d3
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3d6e926ffb7557c6f4324dc8a0b4c586611d7954d52e9a258ea539a391448637f74dad55bee3e01c18ac697605bcc00c44437097cbf6cb8a68ca849b8972abf9
|
|
7
|
+
data.tar.gz: b313ea4a227f849f70c2012a2611f15a4d0ab2a284f5ff1f4443d93c95c46a76432b12e9c0a242af8b5c85b0573292acca6ee831c6a5cc0f0336bd33bd6e9768
|
|
@@ -10,6 +10,7 @@ module Metanorma
|
|
|
10
10
|
init_misc(node)
|
|
11
11
|
init_processing(node) # feeds init_i18n
|
|
12
12
|
init_log(node)
|
|
13
|
+
init_suppression(node)
|
|
13
14
|
init_image(node)
|
|
14
15
|
init_reqt(node)
|
|
15
16
|
init_toc(node)
|
|
@@ -32,6 +33,7 @@ module Metanorma
|
|
|
32
33
|
end
|
|
33
34
|
|
|
34
35
|
def init_vars
|
|
36
|
+
@files_to_delete ||= [] # early init failures must not mask in clean_exit
|
|
35
37
|
@fn_number ||= 0
|
|
36
38
|
@refids = Set.new
|
|
37
39
|
@anchors = {}
|
|
@@ -104,6 +106,159 @@ module Metanorma
|
|
|
104
106
|
end
|
|
105
107
|
end
|
|
106
108
|
|
|
109
|
+
# Declarative validation suppression (metanorma-standoc#1369).
|
|
110
|
+
# Resolution chain: flavor config < taste config < document config.
|
|
111
|
+
# Absent layers are skipped silently; a malformed layer raises
|
|
112
|
+
# naming the layer. The resolved config is injected into the log
|
|
113
|
+
# (utils Log suppression hook), and one Suppression entry
|
|
114
|
+
# (STANDOC_68) summarizes the effective configuration.
|
|
115
|
+
def init_suppression(node)
|
|
116
|
+
@log or return
|
|
117
|
+
flavor_cfg, flavor_src = gem_suppression_config(
|
|
118
|
+
"metanorma-#{validation_flavor_name}", validation_flavor_name,
|
|
119
|
+
"flavor",
|
|
120
|
+
)
|
|
121
|
+
taste_cfg, taste_src = taste_suppression_config(node)
|
|
122
|
+
doc_cfg, doc_src = document_suppression_config(node)
|
|
123
|
+
|
|
124
|
+
layers = [flavor_cfg, taste_cfg, doc_cfg].compact
|
|
125
|
+
return if layers.empty?
|
|
126
|
+
|
|
127
|
+
config = layers.reduce { |base, higher| base.merge(higher) }
|
|
128
|
+
return if config.empty?
|
|
129
|
+
|
|
130
|
+
@log.suppression = config
|
|
131
|
+
log_suppression_dump([flavor_src, taste_src, doc_src].compact,
|
|
132
|
+
config)
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
# The flavor identity, derived from the most-derived Validate
|
|
136
|
+
# subclass declaring validation_flavor (metanorma-iso declares
|
|
137
|
+
# "iso", metanorma-bsi "bsi", standoc "standoc").
|
|
138
|
+
def validation_flavor_name
|
|
139
|
+
m = validate_class.instance_method(:validation_flavor)
|
|
140
|
+
m.owner.name.sub(/\AMetanorma::/, "").sub(/::Validate\z/, "")
|
|
141
|
+
.downcase
|
|
142
|
+
rescue NameError
|
|
143
|
+
"standoc"
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def taste_suppression_config(node)
|
|
147
|
+
taste = node.attr("taste") || ENV["METANORMA_TASTE"] or return
|
|
148
|
+
gem_suppression_config("metanorma-taste-#{taste}", taste, "taste")
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
# A gem layer: +gem_name+ ships
|
|
152
|
+
# lib/metanorma/<dir>/validation_policy/metanorma.yml. Absent gem
|
|
153
|
+
# (Gem::LoadError) or absent file: layer skipped silently; a
|
|
154
|
+
# malformed file raises naming the layer.
|
|
155
|
+
def gem_suppression_config(gem_name, dir, layer)
|
|
156
|
+
spec = Gem::Specification.find_by_name(gem_name)
|
|
157
|
+
path = File.join(spec.gem_dir, "lib", "metanorma", dir,
|
|
158
|
+
"validation_policy", "metanorma.yml")
|
|
159
|
+
return unless File.exist?(path)
|
|
160
|
+
|
|
161
|
+
[load_suppression_config(path, layer), path]
|
|
162
|
+
rescue Gem::LoadError
|
|
163
|
+
nil
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
def load_suppression_config(path, layer)
|
|
167
|
+
Validate::Suppression::Config.from_yaml(path)
|
|
168
|
+
rescue StandardError => e
|
|
169
|
+
raise ArgumentError,
|
|
170
|
+
"invalid validation suppression config " \
|
|
171
|
+
"(#{layer} layer): #{e.message}"
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
def validation_config_path(node)
|
|
175
|
+
if (ret = node.attr("validation-config"))
|
|
176
|
+
(Pathname.new ret).absolute? or
|
|
177
|
+
ret = File.join(@localdir, ret)
|
|
178
|
+
end
|
|
179
|
+
ret
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
# Document layer: :validation-config: file, then the
|
|
183
|
+
# :validation-off: / :validation-warning: sugars, then the
|
|
184
|
+
# log-filter-* aliases (same layer: later parts override earlier
|
|
185
|
+
# parts per code).
|
|
186
|
+
def document_suppression_config(node)
|
|
187
|
+
parts = []
|
|
188
|
+
if (path = validation_config_path(node))
|
|
189
|
+
parts << [load_suppression_config(path, "document"), path]
|
|
190
|
+
end
|
|
191
|
+
sugar = suppression_sugar_config(node)
|
|
192
|
+
unless sugar.empty?
|
|
193
|
+
parts << [sugar, ":validation-off:/:validation-warning:"]
|
|
194
|
+
end
|
|
195
|
+
legacy = legacy_suppression_config(node)
|
|
196
|
+
unless legacy.empty?
|
|
197
|
+
parts << [legacy, "log-filter-* (legacy aliases)"]
|
|
198
|
+
end
|
|
199
|
+
return [nil, nil] if parts.empty?
|
|
200
|
+
|
|
201
|
+
config = parts.map(&:first)
|
|
202
|
+
.reduce { |base, higher| base.merge(higher) }
|
|
203
|
+
[config, parts.map(&:last).join(", ")]
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
# `:validation-off:` codes drop; `:validation-warning:` codes
|
|
207
|
+
# reclassify to warning.
|
|
208
|
+
def suppression_sugar_config(node)
|
|
209
|
+
cfg = Validate::Suppression::Config.new
|
|
210
|
+
cfg.rules =
|
|
211
|
+
Array(csv_split(node.attr("validation-off"), ",")).map do |code|
|
|
212
|
+
Validate::Suppression::RuleEntry.new(code: code.strip,
|
|
213
|
+
severity: "off")
|
|
214
|
+
end +
|
|
215
|
+
Array(csv_split(node.attr("validation-warning"), ",")).map do |code|
|
|
216
|
+
Validate::Suppression::RuleEntry.new(code: code.strip,
|
|
217
|
+
severity: "warning")
|
|
218
|
+
end
|
|
219
|
+
cfg
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
# Alias migration (behavior-preserving): the legacy
|
|
223
|
+
# log-filter-error-ids and log-filter-category attributes translate
|
|
224
|
+
# 1:1 onto Config rules. Their enforcement remains with the
|
|
225
|
+
# untouched suppress_log path, which drops those entries before the
|
|
226
|
+
# suppression hook is consulted, so existing behavior is
|
|
227
|
+
# bit-identical; the translation surfaces them in the
|
|
228
|
+
# effective-config dump and prepares the deprecation-notice
|
|
229
|
+
# release. log-filter-severity, log-filter-error-regex and
|
|
230
|
+
# log-filter-error-loc have no v1 Config equivalent and stay
|
|
231
|
+
# suppress_log-only.
|
|
232
|
+
def legacy_suppression_config(node)
|
|
233
|
+
cfg = Validate::Suppression::Config.new
|
|
234
|
+
ids = Array(csv_split(node.attr("log-filter-error-ids"), ","))
|
|
235
|
+
cats = Array(csv_split(node.attr("log-filter-category"), ","))
|
|
236
|
+
cfg.rules = ids.map do |c|
|
|
237
|
+
Validate::Suppression::RuleEntry.new(code: c.strip,
|
|
238
|
+
severity: "off")
|
|
239
|
+
end
|
|
240
|
+
unless cats.empty?
|
|
241
|
+
cfg.category_rules = { "category" => cats.each_with_object({}) do |c, m|
|
|
242
|
+
m[c.strip] = { "severity" => "off" }
|
|
243
|
+
end }
|
|
244
|
+
end
|
|
245
|
+
cfg
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
# One Suppression entry (STANDOC_68, info severity, display off)
|
|
249
|
+
# summarizing the effective configuration.
|
|
250
|
+
def log_suppression_dump(sources, config)
|
|
251
|
+
counts = config.rules_count
|
|
252
|
+
parts = ["sources: #{sources.join(', ')}",
|
|
253
|
+
"code rules: #{counts[:total]} " \
|
|
254
|
+
"(off: #{counts[:off]}, " \
|
|
255
|
+
"reclassified: #{counts[:reclassified]})"]
|
|
256
|
+
parts << "allow patterns: #{counts[:allow]}" if counts[:allow].positive?
|
|
257
|
+
parts << "exclude patterns: #{counts[:exclude]}" if counts[:exclude].positive?
|
|
258
|
+
parts << "family rules: #{counts[:family]}" if counts[:family].positive?
|
|
259
|
+
@log.add("STANDOC_68", nil, display: false, params: [parts.join("; ")])
|
|
260
|
+
end
|
|
261
|
+
|
|
107
262
|
def init_image(node)
|
|
108
263
|
@datauriimage = node.attr("data-uri-image") != "false"
|
|
109
264
|
@datauriattachment = node.attr("data-uri-attachment") != "false"
|
|
@@ -210,6 +210,9 @@ module Metanorma
|
|
|
210
210
|
"blank-line-flanked comment or conditional " \
|
|
211
211
|
"directive had ended the first list",
|
|
212
212
|
severity: 2 },
|
|
213
|
+
STANDOC_68: { category: "Suppression",
|
|
214
|
+
error: "Validation suppression active: %s",
|
|
215
|
+
severity: 3 },
|
|
213
216
|
RELATON_1: { category: "Relaton",
|
|
214
217
|
error: "(Error from Relaton) %s",
|
|
215
218
|
severity: 0 },
|
|
@@ -0,0 +1,344 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "lutaml/model"
|
|
4
|
+
|
|
5
|
+
module Metanorma
|
|
6
|
+
module Standoc
|
|
7
|
+
class Validate
|
|
8
|
+
# Declarative validation suppression (metanorma-standoc#1369).
|
|
9
|
+
module Suppression
|
|
10
|
+
# One suppression rule, keyed by log code: a severity action
|
|
11
|
+
# (off / reclassify) optionally narrowed by message-text content
|
|
12
|
+
# matching. `allow:` patterns whitelist matching message text
|
|
13
|
+
# (routed to the audited suppressed store); `exclude:` patterns
|
|
14
|
+
# drop matching message text the same way. Line-level and
|
|
15
|
+
# element-scope (xpath/anchor) matching remain out for the
|
|
16
|
+
# coradoc era.
|
|
17
|
+
class RuleEntry < Lutaml::Model::Serializable
|
|
18
|
+
SEVERITY_VOCABULARY = %w(off info warning error).freeze
|
|
19
|
+
|
|
20
|
+
# Log severity scale (utils Log): 0 abort; 1 serious;
|
|
21
|
+
# 2 not serious; 3 info only.
|
|
22
|
+
RECLASSIFY_LEVELS = { "info" => 3, "warning" => 2,
|
|
23
|
+
"error" => 1 }.freeze
|
|
24
|
+
|
|
25
|
+
attribute :code, :string
|
|
26
|
+
attribute :severity, :string, values: SEVERITY_VOCABULARY
|
|
27
|
+
attribute :allow, :string, collection: true
|
|
28
|
+
attribute :exclude, :string, collection: true
|
|
29
|
+
|
|
30
|
+
yaml do
|
|
31
|
+
map "code", to: :code
|
|
32
|
+
map "severity", to: :severity
|
|
33
|
+
map "allow", to: :allow
|
|
34
|
+
map "exclude", to: :exclude
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# YAML 1.1 parses a bare `off` as false (lutaml-model casts it
|
|
38
|
+
# to "false"); normalize the keyword back so `severity: off`
|
|
39
|
+
# (the frozen schema's spelling) works.
|
|
40
|
+
def normalized_severity
|
|
41
|
+
s = severity
|
|
42
|
+
s = "off" if s == false || s == "false"
|
|
43
|
+
s.to_s.downcase
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def content_rule?
|
|
47
|
+
Array(allow).any? || Array(exclude).any?
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# The Log hook action for an entry of this code. Content
|
|
51
|
+
# matching first: an `allow` pattern matching the message text
|
|
52
|
+
# yields :allow, an `exclude` pattern yields :exclude (both
|
|
53
|
+
# audited in the suppressed store with the pattern as reason).
|
|
54
|
+
# Otherwise the configured severity action applies, or nil
|
|
55
|
+
# when the rule carries no severity.
|
|
56
|
+
def action(message: nil)
|
|
57
|
+
if (pattern = matching_pattern(compiled_allow, message))
|
|
58
|
+
return { action: :allow,
|
|
59
|
+
reason: "allowed by config: #{pattern}" }
|
|
60
|
+
end
|
|
61
|
+
if (pattern = matching_pattern(compiled_exclude, message))
|
|
62
|
+
return { action: :exclude,
|
|
63
|
+
reason: "excluded by config: #{pattern}" }
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
severity_action
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def severity_action
|
|
70
|
+
case normalized_severity
|
|
71
|
+
when "" then nil
|
|
72
|
+
when "off" then { action: :off,
|
|
73
|
+
reason: "suppressed by validation " \
|
|
74
|
+
"rule (#{code})" }
|
|
75
|
+
else { action: :reclassify,
|
|
76
|
+
severity: RECLASSIFY_LEVELS.fetch(normalized_severity),
|
|
77
|
+
reason: "reclassified by validation rule " \
|
|
78
|
+
"(#{code}: #{normalized_severity})" }
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def matching_pattern(compiled, message)
|
|
83
|
+
return if message.nil?
|
|
84
|
+
|
|
85
|
+
compiled.each do |(pattern, regexp)|
|
|
86
|
+
regexp.match?(message) and return pattern
|
|
87
|
+
end
|
|
88
|
+
nil
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def compiled_allow
|
|
92
|
+
@compiled_allow ||= compile_patterns(allow, "allow")
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def compiled_exclude
|
|
96
|
+
@compiled_exclude ||= compile_patterns(exclude, "exclude")
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def compile_patterns(patterns, kind)
|
|
100
|
+
Array(patterns).map do |p|
|
|
101
|
+
[p.to_s, Regexp.new(p.to_s)]
|
|
102
|
+
rescue RegexpError, StandardError => e
|
|
103
|
+
raise ArgumentError,
|
|
104
|
+
"invalid #{kind} pattern #{p.inspect} for code " \
|
|
105
|
+
"#{code}: #{e.message}"
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def valid_rule!
|
|
110
|
+
normalized_severity.empty? && !content_rule? and
|
|
111
|
+
raise ArgumentError,
|
|
112
|
+
"rule for code #{code} carries no severity, " \
|
|
113
|
+
"allow or exclude"
|
|
114
|
+
valid_severity!
|
|
115
|
+
compiled_allow
|
|
116
|
+
compiled_exclude
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def valid_severity!
|
|
120
|
+
return if normalized_severity.empty? && content_rule?
|
|
121
|
+
return if SEVERITY_VOCABULARY.include?(normalized_severity)
|
|
122
|
+
|
|
123
|
+
raise ArgumentError,
|
|
124
|
+
"invalid severity #{severity.inspect} for code #{code} " \
|
|
125
|
+
"(allowed: #{SEVERITY_VOCABULARY.join(', ')})"
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
# The validation-suppression configuration document
|
|
130
|
+
# (schema metanorma-validation-suppression/1):
|
|
131
|
+
#
|
|
132
|
+
# schema: metanorma-validation-suppression/1
|
|
133
|
+
# rules:
|
|
134
|
+
# ISO_53:
|
|
135
|
+
# severity: off
|
|
136
|
+
# STANDOC_48:
|
|
137
|
+
# severity: warning
|
|
138
|
+
# allow:
|
|
139
|
+
# - 'a PDF\b'
|
|
140
|
+
# category_rules: # optional, whole-family selection
|
|
141
|
+
# category:
|
|
142
|
+
# Style:
|
|
143
|
+
# severity: off
|
|
144
|
+
#
|
|
145
|
+
# All (de)serialization goes through lutaml-model (attribute +
|
|
146
|
+
# mapping); category_rules stays a framework-parsed hash because
|
|
147
|
+
# lutaml-model 0.8.x child_mappings are single-level and the
|
|
148
|
+
# family map nests two levels.
|
|
149
|
+
class Config < Lutaml::Model::Serializable
|
|
150
|
+
SCHEMA_VERSION = "metanorma-validation-suppression/1"
|
|
151
|
+
FAMILIES = %w(mn_category category source).freeze
|
|
152
|
+
|
|
153
|
+
attribute :schema_version, :string, default: SCHEMA_VERSION
|
|
154
|
+
attribute :rules, RuleEntry, collection: true
|
|
155
|
+
attribute :category_rules, :hash
|
|
156
|
+
|
|
157
|
+
yaml do
|
|
158
|
+
map "schema", to: :schema_version
|
|
159
|
+
map "rules", to: :rules,
|
|
160
|
+
child_mappings: { code: :key, severity: :severity,
|
|
161
|
+
allow: :allow,
|
|
162
|
+
exclude: :exclude }
|
|
163
|
+
map "category_rules", to: :category_rules
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
class << self
|
|
167
|
+
# Path form: reads the YAML file at +path+. The framework's
|
|
168
|
+
# text-parsing entry point stays available under its declared
|
|
169
|
+
# name, from_yaml_string — here from_yaml is deliberately
|
|
170
|
+
# repurposed to the file form the suppression layers need.
|
|
171
|
+
def from_yaml(path, options = {})
|
|
172
|
+
from_yaml_string(File.read(path), options)
|
|
173
|
+
rescue StandardError => e
|
|
174
|
+
raise e.class,
|
|
175
|
+
"validation suppression config #{path}: #{e.message}"
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def from_yaml_string(text, options = {})
|
|
179
|
+
from(:yaml, text, options).tap(&:validate_config!)
|
|
180
|
+
end
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
# Optional family-metadata callback injected by the converter:
|
|
184
|
+
# ->(family, id) { ... } returning the code's registered policy
|
|
185
|
+
# metadata (e.g. its mn_category) for the mn_category/source
|
|
186
|
+
# families. Without it, only `category` family rules match
|
|
187
|
+
# (against the message's log category).
|
|
188
|
+
attr_accessor :registry_lookup
|
|
189
|
+
|
|
190
|
+
def validate_config!
|
|
191
|
+
version = schema_version || SCHEMA_VERSION
|
|
192
|
+
unless version == SCHEMA_VERSION
|
|
193
|
+
raise ArgumentError,
|
|
194
|
+
"unsupported validation-suppression schema version " \
|
|
195
|
+
"#{version.inspect} (supported: #{SCHEMA_VERSION.inspect})"
|
|
196
|
+
end
|
|
197
|
+
(rules || []).each(&:valid_rule!)
|
|
198
|
+
validate_category_rules!
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
# The Log hook. Code rules first, then whole-family rules.
|
|
202
|
+
# severity and location are part of the frozen hook contract
|
|
203
|
+
# but v1 matches on code, family, and message text (allow /
|
|
204
|
+
# exclude patterns) alone.
|
|
205
|
+
def action_for(id:, message: nil, category: nil, severity: nil,
|
|
206
|
+
location: nil)
|
|
207
|
+
rule = code_rule(id)
|
|
208
|
+
return rule.action(message: message) if rule
|
|
209
|
+
|
|
210
|
+
family_action(id, category)
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
def code_rule(id)
|
|
214
|
+
(rules || []).find { |r| r.code.to_s.casecmp(id.to_s).zero? }
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
def family_action(id, category)
|
|
218
|
+
(category_rules || {}).each do |family, names|
|
|
219
|
+
family_names(family.to_s, id, category).each do |name|
|
|
220
|
+
entry = names[name] or next
|
|
221
|
+
return family_result(family.to_s, name, entry)
|
|
222
|
+
end
|
|
223
|
+
end
|
|
224
|
+
nil
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
def family_names(family, id, category)
|
|
228
|
+
case family
|
|
229
|
+
when "category"
|
|
230
|
+
[category].compact.map(&:to_s)
|
|
231
|
+
when "mn_category", "source"
|
|
232
|
+
if registry_lookup
|
|
233
|
+
[registry_lookup.call(family, id)].compact.map(&:to_s)
|
|
234
|
+
else
|
|
235
|
+
[]
|
|
236
|
+
end
|
|
237
|
+
else []
|
|
238
|
+
end
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
def normalized_family_severity(entry)
|
|
242
|
+
sev = entry.is_a?(::Hash) ? entry["severity"] : entry
|
|
243
|
+
sev = "off" if sev == false || sev == "false"
|
|
244
|
+
sev.to_s.downcase
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
def family_result(family, name, entry)
|
|
248
|
+
sev = normalized_family_severity(entry)
|
|
249
|
+
unless RuleEntry::SEVERITY_VOCABULARY.include?(sev)
|
|
250
|
+
raise ArgumentError,
|
|
251
|
+
"invalid severity #{entry.inspect} in category_rules " \
|
|
252
|
+
"#{family}/#{name} " \
|
|
253
|
+
"(allowed: #{RuleEntry::SEVERITY_VOCABULARY.join(', ')})"
|
|
254
|
+
end
|
|
255
|
+
rule = RuleEntry.new(code: name, severity: sev)
|
|
256
|
+
{ **rule.severity_action,
|
|
257
|
+
reason: "#{rule.severity_action[:reason]} [#{family}=#{name}]" }
|
|
258
|
+
end
|
|
259
|
+
|
|
260
|
+
# validates each family entry and normalizes the stored
|
|
261
|
+
# severities (YAML-1.1 `off` arrives as false)
|
|
262
|
+
def validate_category_rules!
|
|
263
|
+
normalized = {}
|
|
264
|
+
(category_rules || {}).each do |family, names|
|
|
265
|
+
names.is_a?(::Hash) or raise ArgumentError,
|
|
266
|
+
"category_rules #{family} must " \
|
|
267
|
+
"be a map of name to action"
|
|
268
|
+
normalized[family] = names.each_with_object({}) do |(name, e), m|
|
|
269
|
+
family_result(family.to_s, name, e)
|
|
270
|
+
m[name] = { "severity" => normalized_family_severity(e) }
|
|
271
|
+
end
|
|
272
|
+
end
|
|
273
|
+
@category_rules = normalized
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
# Layered resolution: +higher+ (document over taste over flavor)
|
|
277
|
+
# wins the severity action per code and per family name; the
|
|
278
|
+
# content-matching lists (allow, exclude) concatenate across
|
|
279
|
+
# layers.
|
|
280
|
+
def merge(higher)
|
|
281
|
+
cfg = Config.new
|
|
282
|
+
cfg.schema_version = SCHEMA_VERSION
|
|
283
|
+
cfg.rules = merged_rules(higher)
|
|
284
|
+
cfg.category_rules = merged_category_rules(higher)
|
|
285
|
+
cfg.registry_lookup = higher.registry_lookup || registry_lookup
|
|
286
|
+
cfg
|
|
287
|
+
end
|
|
288
|
+
|
|
289
|
+
def merged_rules(higher)
|
|
290
|
+
lower_rules = rules || []
|
|
291
|
+
higher_rules = higher.rules || []
|
|
292
|
+
codes = (lower_rules + higher_rules).map { |r| r.code.to_s }.uniq
|
|
293
|
+
codes.map do |code|
|
|
294
|
+
lower = lower_rules.find { |r| r.code.to_s == code }
|
|
295
|
+
higher_rule = higher_rules.find { |r| r.code.to_s == code }
|
|
296
|
+
merged_rule(lower, higher_rule)
|
|
297
|
+
end
|
|
298
|
+
end
|
|
299
|
+
|
|
300
|
+
def merged_rule(lower, higher)
|
|
301
|
+
return higher if lower.nil?
|
|
302
|
+
return lower if higher.nil?
|
|
303
|
+
|
|
304
|
+
RuleEntry.new(
|
|
305
|
+
code: lower.code,
|
|
306
|
+
severity: higher.severity || lower.severity,
|
|
307
|
+
allow: uniq_patterns(Array(lower.allow) + Array(higher.allow)),
|
|
308
|
+
exclude: uniq_patterns(Array(lower.exclude) +
|
|
309
|
+
Array(higher.exclude)),
|
|
310
|
+
)
|
|
311
|
+
end
|
|
312
|
+
|
|
313
|
+
def uniq_patterns(patterns)
|
|
314
|
+
patterns.map(&:to_s).uniq
|
|
315
|
+
end
|
|
316
|
+
|
|
317
|
+
def merged_category_rules(higher)
|
|
318
|
+
merged = (category_rules || {}).dup
|
|
319
|
+
(higher.category_rules || {}).each do |family, names|
|
|
320
|
+
merged[family] = (merged[family] || {}).merge(names)
|
|
321
|
+
end
|
|
322
|
+
merged.empty? ? nil : merged
|
|
323
|
+
end
|
|
324
|
+
|
|
325
|
+
def empty?
|
|
326
|
+
(rules.nil? || rules.empty?) &&
|
|
327
|
+
(category_rules.nil? || category_rules.empty?)
|
|
328
|
+
end
|
|
329
|
+
|
|
330
|
+
def rules_count
|
|
331
|
+
off = (rules || []).count { |r| r.normalized_severity == "off" }
|
|
332
|
+
{ total: (rules || []).size, off: off,
|
|
333
|
+
reclassified: (rules || []).count do |r|
|
|
334
|
+
r.normalized_severity != "" && r.normalized_severity != "off"
|
|
335
|
+
end,
|
|
336
|
+
allow: (rules || []).sum { |r| Array(r.allow).size },
|
|
337
|
+
exclude: (rules || []).sum { |r| Array(r.exclude).size },
|
|
338
|
+
family: (category_rules || {}).values.sum(&:size) }
|
|
339
|
+
end
|
|
340
|
+
end
|
|
341
|
+
end
|
|
342
|
+
end
|
|
343
|
+
end
|
|
344
|
+
end
|
|
@@ -12,6 +12,12 @@ module Metanorma
|
|
|
12
12
|
class Validate
|
|
13
13
|
extend Forwardable
|
|
14
14
|
|
|
15
|
+
# Declarative validation suppression (metanorma-standoc#1369).
|
|
16
|
+
module Suppression
|
|
17
|
+
autoload :Config, "metanorma/validate/suppression/config"
|
|
18
|
+
autoload :RuleEntry, "metanorma/validate/suppression/config"
|
|
19
|
+
end
|
|
20
|
+
|
|
15
21
|
# The flavor identity of this validator; flavors override
|
|
16
22
|
# (metanorma-iso "iso", metanorma-bsi "bsi"). Names the flavor's
|
|
17
23
|
# profile directories and the metanorma-<flavor> gem for the
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: metanorma-standoc
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 3.5.
|
|
4
|
+
version: 3.5.18
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Ribose Inc.
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-11-
|
|
11
|
+
date: 2026-11-12 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: addressable
|
|
@@ -791,6 +791,7 @@ files:
|
|
|
791
791
|
- lib/metanorma/validate/section.rb
|
|
792
792
|
- lib/metanorma/validate/standoc-compile.rng
|
|
793
793
|
- lib/metanorma/validate/standoc.rng
|
|
794
|
+
- lib/metanorma/validate/suppression/config.rb
|
|
794
795
|
- lib/metanorma/validate/table.rb
|
|
795
796
|
- lib/metanorma/validate/term.rb
|
|
796
797
|
- lib/metanorma/validate/validate.rb
|