coridoc 3.0.0

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.
Files changed (148) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +3 -0
  3. data/Rakefile +8 -0
  4. data/exe/coridoc +24 -0
  5. data/lib/coridoc/binding_inflater.rb +118 -0
  6. data/lib/coridoc/cli.rb +311 -0
  7. data/lib/coridoc/configurable.rb +530 -0
  8. data/lib/coridoc/core_model/abstract_block.rb +11 -0
  9. data/lib/coridoc/core_model/annotation_block.rb +57 -0
  10. data/lib/coridoc/core_model/attribute_reference_resolver.rb +155 -0
  11. data/lib/coridoc/core_model/base.rb +281 -0
  12. data/lib/coridoc/core_model/bibliography.rb +41 -0
  13. data/lib/coridoc/core_model/bibliography_entry.rb +48 -0
  14. data/lib/coridoc/core_model/block.rb +69 -0
  15. data/lib/coridoc/core_model/callout.rb +32 -0
  16. data/lib/coridoc/core_model/callout_text.rb +35 -0
  17. data/lib/coridoc/core_model/children_content.rb +72 -0
  18. data/lib/coridoc/core_model/comment_block.rb +16 -0
  19. data/lib/coridoc/core_model/comment_line.rb +23 -0
  20. data/lib/coridoc/core_model/definition_item.rb +68 -0
  21. data/lib/coridoc/core_model/definition_list.rb +28 -0
  22. data/lib/coridoc/core_model/element_attribute.rb +14 -0
  23. data/lib/coridoc/core_model/example_block.rb +12 -0
  24. data/lib/coridoc/core_model/footnote.rb +92 -0
  25. data/lib/coridoc/core_model/frontmatter/codec.rb +151 -0
  26. data/lib/coridoc/core_model/frontmatter/field_transform.rb +75 -0
  27. data/lib/coridoc/core_model/frontmatter/frontmatter_entry.rb +22 -0
  28. data/lib/coridoc/core_model/frontmatter/frontmatter_value.rb +78 -0
  29. data/lib/coridoc/core_model/frontmatter/schema_resolver.rb +57 -0
  30. data/lib/coridoc/core_model/frontmatter/text_splitter.rb +75 -0
  31. data/lib/coridoc/core_model/frontmatter.rb +72 -0
  32. data/lib/coridoc/core_model/has_children.rb +23 -0
  33. data/lib/coridoc/core_model/horizontal_rule_block.rb +16 -0
  34. data/lib/coridoc/core_model/id_generator.rb +23 -0
  35. data/lib/coridoc/core_model/image.rb +69 -0
  36. data/lib/coridoc/core_model/include.rb +43 -0
  37. data/lib/coridoc/core_model/include_level_offset.rb +71 -0
  38. data/lib/coridoc/core_model/include_options.rb +101 -0
  39. data/lib/coridoc/core_model/inline_content.rb +78 -0
  40. data/lib/coridoc/core_model/inline_element.rb +198 -0
  41. data/lib/coridoc/core_model/list_block.rb +137 -0
  42. data/lib/coridoc/core_model/list_item.rb +129 -0
  43. data/lib/coridoc/core_model/listing_block.rb +19 -0
  44. data/lib/coridoc/core_model/literal_block.rb +16 -0
  45. data/lib/coridoc/core_model/metadata.rb +71 -0
  46. data/lib/coridoc/core_model/open_block.rb +12 -0
  47. data/lib/coridoc/core_model/output_artifact.rb +48 -0
  48. data/lib/coridoc/core_model/paragraph_block.rb +16 -0
  49. data/lib/coridoc/core_model/partintro_block.rb +11 -0
  50. data/lib/coridoc/core_model/pass_block.rb +16 -0
  51. data/lib/coridoc/core_model/quote_block.rb +14 -0
  52. data/lib/coridoc/core_model/raw_inline_element.rb +16 -0
  53. data/lib/coridoc/core_model/reviewer_block.rb +12 -0
  54. data/lib/coridoc/core_model/sidebar_block.rb +12 -0
  55. data/lib/coridoc/core_model/source_block.rb +16 -0
  56. data/lib/coridoc/core_model/stem_block.rb +25 -0
  57. data/lib/coridoc/core_model/structural_element.rb +157 -0
  58. data/lib/coridoc/core_model/table.rb +148 -0
  59. data/lib/coridoc/core_model/term.rb +53 -0
  60. data/lib/coridoc/core_model/text_content.rb +26 -0
  61. data/lib/coridoc/core_model/toc.rb +99 -0
  62. data/lib/coridoc/core_model/toc_generator.rb +151 -0
  63. data/lib/coridoc/core_model/verse_block.rb +14 -0
  64. data/lib/coridoc/core_model.rb +92 -0
  65. data/lib/coridoc/coridoc.rb +164 -0
  66. data/lib/coridoc/dispatch.rb +96 -0
  67. data/lib/coridoc/errors.rb +387 -0
  68. data/lib/coridoc/format_catalog.rb +102 -0
  69. data/lib/coridoc/format_module.rb +78 -0
  70. data/lib/coridoc/hooks.rb +176 -0
  71. data/lib/coridoc/include_resolver/filesystem.rb +84 -0
  72. data/lib/coridoc/include_resolver.rb +67 -0
  73. data/lib/coridoc/include_selectors/indent.rb +54 -0
  74. data/lib/coridoc/include_selectors/level_offset.rb +84 -0
  75. data/lib/coridoc/include_selectors/lines.rb +60 -0
  76. data/lib/coridoc/include_selectors/tags.rb +136 -0
  77. data/lib/coridoc/include_selectors.rb +26 -0
  78. data/lib/coridoc/introspection/element_counter.rb +48 -0
  79. data/lib/coridoc/introspection.rb +72 -0
  80. data/lib/coridoc/link_rewriter/identity.rb +13 -0
  81. data/lib/coridoc/link_rewriter/visitor.rb +148 -0
  82. data/lib/coridoc/link_rewriter.rb +37 -0
  83. data/lib/coridoc/lint/registry.rb +33 -0
  84. data/lib/coridoc/lint/rule.rb +41 -0
  85. data/lib/coridoc/lint/runner.rb +67 -0
  86. data/lib/coridoc/lint/template.rb +190 -0
  87. data/lib/coridoc/lint/violation.rb +16 -0
  88. data/lib/coridoc/lint.rb +44 -0
  89. data/lib/coridoc/logger.rb +54 -0
  90. data/lib/coridoc/performance_regression.rb +121 -0
  91. data/lib/coridoc/pipeline.rb +169 -0
  92. data/lib/coridoc/query.rb +486 -0
  93. data/lib/coridoc/reference/address/anchor.rb +48 -0
  94. data/lib/coridoc/reference/address/doi.rb +40 -0
  95. data/lib/coridoc/reference/address/isbn.rb +39 -0
  96. data/lib/coridoc/reference/address/path.rb +66 -0
  97. data/lib/coridoc/reference/address/scoped_path.rb +47 -0
  98. data/lib/coridoc/reference/address/url.rb +41 -0
  99. data/lib/coridoc/reference/address.rb +153 -0
  100. data/lib/coridoc/reference/catalog/composite.rb +59 -0
  101. data/lib/coridoc/reference/catalog/local.rb +97 -0
  102. data/lib/coridoc/reference/catalog/memory_index.rb +59 -0
  103. data/lib/coridoc/reference/catalog.rb +33 -0
  104. data/lib/coridoc/reference/edge/citation_options.rb +16 -0
  105. data/lib/coridoc/reference/edge/footnote_ref_options.rb +13 -0
  106. data/lib/coridoc/reference/edge/image_ref_options.rb +15 -0
  107. data/lib/coridoc/reference/edge/include_options.rb +15 -0
  108. data/lib/coridoc/reference/edge/kind.rb +72 -0
  109. data/lib/coridoc/reference/edge/link_options.rb +12 -0
  110. data/lib/coridoc/reference/edge/navigation_options.rb +12 -0
  111. data/lib/coridoc/reference/edge/options.rb +13 -0
  112. data/lib/coridoc/reference/edge.rb +80 -0
  113. data/lib/coridoc/reference/edge_search.rb +128 -0
  114. data/lib/coridoc/reference/materializer/base.rb +44 -0
  115. data/lib/coridoc/reference/materializer/passthrough.rb +31 -0
  116. data/lib/coridoc/reference/materializer/registry.rb +116 -0
  117. data/lib/coridoc/reference/materializer.rb +14 -0
  118. data/lib/coridoc/reference/presentation/base.rb +47 -0
  119. data/lib/coridoc/reference/presentation/custom_hierarchy.rb +104 -0
  120. data/lib/coridoc/reference/presentation/page.rb +20 -0
  121. data/lib/coridoc/reference/presentation/single_document.rb +35 -0
  122. data/lib/coridoc/reference/presentation/split_pages.rb +118 -0
  123. data/lib/coridoc/reference/presentation.rb +16 -0
  124. data/lib/coridoc/reference/resolution.rb +184 -0
  125. data/lib/coridoc/reference/resolver/base.rb +15 -0
  126. data/lib/coridoc/reference/resolver/caching.rb +37 -0
  127. data/lib/coridoc/reference/resolver/catalog.rb +69 -0
  128. data/lib/coridoc/reference/resolver/chain.rb +33 -0
  129. data/lib/coridoc/reference/resolver.rb +20 -0
  130. data/lib/coridoc/reference/result/ambiguous.rb +23 -0
  131. data/lib/coridoc/reference/result/base.rb +46 -0
  132. data/lib/coridoc/reference/result/missing.rb +14 -0
  133. data/lib/coridoc/reference/result/resolved.rb +22 -0
  134. data/lib/coridoc/reference/result.rb +20 -0
  135. data/lib/coridoc/reference.rb +65 -0
  136. data/lib/coridoc/registry.rb +156 -0
  137. data/lib/coridoc/relative_path.rb +32 -0
  138. data/lib/coridoc/resolve_includes.rb +202 -0
  139. data/lib/coridoc/source_graph/file_node.rb +147 -0
  140. data/lib/coridoc/source_graph/graph.rb +165 -0
  141. data/lib/coridoc/source_graph/include_edges.rb +33 -0
  142. data/lib/coridoc/source_graph/sidecar.rb +97 -0
  143. data/lib/coridoc/source_graph.rb +36 -0
  144. data/lib/coridoc/validation.rb +686 -0
  145. data/lib/coridoc/version.rb +5 -0
  146. data/lib/coridoc/visitor.rb +283 -0
  147. data/lib/coridoc.rb +41 -0
  148. metadata +221 -0
@@ -0,0 +1,686 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ # Document validation framework for schema-based validation.
5
+ #
6
+ # This module provides a flexible validation framework for ensuring
7
+ # documents conform to expected structures and rules.
8
+ #
9
+ # @example Creating a validation schema
10
+ # schema = Coridoc::Validation::Schema.define do
11
+ # required :title, type: String, min_length: 1
12
+ # required :sections, type: Array, min_count: 1
13
+ # optional :author, type: String
14
+ #
15
+ # rule :check_references do |doc|
16
+ # refs = doc.query('xref')
17
+ # missing = refs.reject { |r| doc.resolve_reference(r) }
18
+ # missing.map { |r| "Unresolved reference: #{r.target}" }
19
+ # end
20
+ # end
21
+ #
22
+ # @example Validating a document
23
+ # result = schema.validate(document)
24
+ # if result.valid?
25
+ # puts "Document is valid"
26
+ # else
27
+ # result.errors.each { |e| puts e.message }
28
+ # end
29
+ #
30
+ module Validation
31
+ module_function
32
+
33
+ # Targets of every unresolved +include::+ edge in the tree
34
+ # (deduplicated, in document order). A document is "unresolved"
35
+ # when it was parsed in graph mode and never went through
36
+ # +Coridoc.resolve_includes+.
37
+ def unresolved_include_targets(model)
38
+ targets = []
39
+ walk_nodes(model) do |node|
40
+ targets << node.target if node.is_a?(Coridoc::CoreModel::Include)
41
+ end
42
+ targets.uniq
43
+ end
44
+
45
+ # Serialization boundary check: formats that cannot represent an
46
+ # unresolved include edge (FormatModule::Interface
47
+ # #preserves_unresolved_includes? == false) must not silently drop
48
+ # the content — raise UnresolvedIncludesError instead, pointing at
49
+ # the hydration step (resolve_includes).
50
+ def guard_unresolved_includes!(model, format_module)
51
+ return if format_module.preserves_unresolved_includes?
52
+
53
+ targets = unresolved_include_targets(model)
54
+ return if targets.empty?
55
+
56
+ raise Coridoc::UnresolvedIncludesError, targets
57
+ end
58
+
59
+ def walk_nodes(node, &block)
60
+ return unless node.is_a?(Coridoc::CoreModel::Base)
61
+
62
+ yield node
63
+ return unless node.is_a?(Coridoc::CoreModel::HasChildren)
64
+
65
+ children = node.children
66
+ return unless children
67
+
68
+ children.each { |child| walk_nodes(child, &block) }
69
+ end
70
+
71
+ # A single validation error
72
+ class Error
73
+ attr_reader :path, :message, :code, :element
74
+
75
+ # Create a validation error
76
+ #
77
+ # @param message [String] Error message
78
+ # @param path [String, nil] Path to the error location
79
+ # @param code [Symbol, nil] Error code for programmatic handling
80
+ # @param element [Object, nil] The element that failed validation
81
+ def initialize(message, path: nil, code: nil, element: nil)
82
+ @message = message
83
+ @path = path
84
+ @code = code
85
+ @element = element
86
+ end
87
+
88
+ # Format error as string
89
+ #
90
+ # @return [String]
91
+ def to_s
92
+ if path
93
+ "#{path}: #{message}"
94
+ else
95
+ message
96
+ end
97
+ end
98
+
99
+ # Convert to hash
100
+ #
101
+ # @return [Hash]
102
+ def to_h
103
+ { message: message, path: path, code: code }
104
+ end
105
+ end
106
+
107
+ # Validation result containing errors
108
+ class Result
109
+ attr_reader :errors, :warnings
110
+
111
+ # Create a validation result
112
+ #
113
+ # @param errors [Array<Error>] Validation errors
114
+ # @param warnings [Array<Error>] Validation warnings
115
+ def initialize(errors: [], warnings: [])
116
+ @errors = Array(errors)
117
+ @warnings = Array(warnings)
118
+ end
119
+
120
+ # Check if validation passed
121
+ #
122
+ # @return [Boolean]
123
+ def valid?
124
+ @errors.empty?
125
+ end
126
+
127
+ # Check if there are any warnings
128
+ #
129
+ # @return [Boolean]
130
+ def warnings?
131
+ @warnings.any?
132
+ end
133
+
134
+ # Get error count
135
+ #
136
+ # @return [Integer]
137
+ def error_count
138
+ @errors.size
139
+ end
140
+
141
+ # Get warning count
142
+ #
143
+ # @return [Integer]
144
+ def warning_count
145
+ @warnings.size
146
+ end
147
+
148
+ # Add an error
149
+ #
150
+ # @param message [String] Error message
151
+ # @param path [String, nil] Error path
152
+ # @param code [Symbol, nil] Error code
153
+ # @param element [Object, nil] Failed element
154
+ # @return [Error] The added error
155
+ def add_error(message, path: nil, code: nil, element: nil)
156
+ error = Error.new(message, path: path, code: code, element: element)
157
+ @errors << error
158
+ error
159
+ end
160
+
161
+ # Add a warning
162
+ #
163
+ # @param message [String] Warning message
164
+ # @param path [String, nil] Warning path
165
+ # @param code [Symbol, nil] Warning code
166
+ # @param element [Object, nil] Related element
167
+ # @return [Error] The added warning
168
+ def add_warning(message, path: nil, code: nil, element: nil)
169
+ warning = Error.new(message, path: path, code: code, element: element)
170
+ @warnings << warning
171
+ warning
172
+ end
173
+
174
+ # Merge another result into this one
175
+ #
176
+ # @param other [Result] Another validation result
177
+ # @return [void]
178
+ def merge!(other)
179
+ @errors.concat(other.errors)
180
+ @warnings.concat(other.warnings)
181
+ end
182
+
183
+ # Format errors as a human-readable string
184
+ #
185
+ # @return [String]
186
+ def to_s
187
+ return 'Valid' if valid?
188
+
189
+ lines = ["#{error_count} validation error(s):"]
190
+ @errors.each { |err| lines << " - #{err.path}: #{err.message}" }
191
+ lines.join("\n")
192
+ end
193
+
194
+ # Get errors for a specific path
195
+ #
196
+ # @param path [String] The path to filter by
197
+ # @return [Array<Error>]
198
+ def errors_at(path)
199
+ @errors.select { |e| e.path == path }
200
+ end
201
+
202
+ # Convert to hash
203
+ #
204
+ # @return [Hash]
205
+ def to_h
206
+ {
207
+ valid: valid?,
208
+ error_count: error_count,
209
+ warning_count: warning_count,
210
+ errors: @errors.map(&:to_h),
211
+ warnings: @warnings.map(&:to_h)
212
+ }
213
+ end
214
+ end
215
+
216
+ # Base class for validation rules
217
+ class Rule
218
+ attr_reader :name, :options
219
+
220
+ def initialize(name, **options)
221
+ @name = name
222
+ @options = options
223
+ end
224
+
225
+ def validate(element, context = {})
226
+ raise NotImplementedError, 'Subclasses must implement #validate'
227
+ end
228
+
229
+ private
230
+
231
+ def field_value(element, field)
232
+ if element.is_a?(CoreModel::Base)
233
+ element.public_send(field) if element.class.attributes.key?(field)
234
+ else
235
+ element.public_send(field)
236
+ end
237
+ rescue NoMethodError
238
+ nil
239
+ end
240
+ end
241
+
242
+ # Built-in validation rules
243
+ module Rules
244
+ # Required field validation
245
+ class Required < Rule
246
+ def validate(element, _context = {})
247
+ field = options[:field]
248
+ value = get_value(element, field)
249
+
250
+ return [] unless value.nil?
251
+
252
+ ["#{field} is required"]
253
+ end
254
+
255
+ private
256
+
257
+ def get_value(element, field)
258
+ if element.is_a?(CoreModel::Base)
259
+ element.public_send(field) if element.class.attributes.key?(field)
260
+ else
261
+ element.public_send(field)
262
+ end
263
+ rescue NoMethodError
264
+ nil
265
+ end
266
+ end
267
+
268
+ # Type validation
269
+ class Type < Rule
270
+ def validate(element, _context = {})
271
+ field = options[:field]
272
+ expected_type = options[:type]
273
+ value = field_value(element, field)
274
+
275
+ return [] if value.nil? && !options[:required]
276
+ return [] if value.nil?
277
+
278
+ return ["#{field} must be #{expected_type.name}, got #{value.class.name}"] unless value.is_a?(expected_type)
279
+
280
+ []
281
+ end
282
+ end
283
+
284
+ # Length validation
285
+ class Length < Rule
286
+ def validate(element, _context = {})
287
+ field = options[:field]
288
+ value = field_value(element, field)
289
+
290
+ return [] if value.nil?
291
+
292
+ errors = []
293
+ length = value.is_a?(String) ? value.length : 0
294
+
295
+ errors << "#{field} must have at least #{options[:min]} characters/items" if options[:min] && length < options[:min]
296
+
297
+ errors << "#{field} must have at most #{options[:max]} characters/items" if options[:max] && length > options[:max]
298
+
299
+ errors
300
+ end
301
+ end
302
+
303
+ # Count validation for arrays/collections
304
+ class Count < Rule
305
+ def validate(element, _context = {})
306
+ field = options[:field]
307
+ value = field_value(element, field)
308
+
309
+ return [] if value.nil?
310
+
311
+ errors = []
312
+ count = value.is_a?(Enumerable) ? value.count : 0
313
+
314
+ errors << "#{field} must have at least #{options[:min]} items" if options[:min] && count < options[:min]
315
+
316
+ errors << "#{field} must have at most #{options[:max]} items" if options[:max] && count > options[:max]
317
+
318
+ errors
319
+ end
320
+ end
321
+
322
+ # Format validation with regex
323
+ class Format < Rule
324
+ def validate(element, _context = {})
325
+ field = options[:field]
326
+ pattern = options[:pattern]
327
+ value = field_value(element, field)
328
+
329
+ return [] if value.nil?
330
+
331
+ return ["#{field} has invalid format"] unless pattern.match?(value.to_s)
332
+
333
+ []
334
+ end
335
+ end
336
+
337
+ # Custom block validation
338
+ class Custom < Rule
339
+ def validate(element, context = {})
340
+ block = options[:block]
341
+ return [] unless block
342
+
343
+ result = block.call(element, context)
344
+ Array(result)
345
+ end
346
+ end
347
+ end
348
+
349
+ # Validation schema definition
350
+ class Schema
351
+ attr_reader :fields, :rules
352
+
353
+ # Define a new schema
354
+ #
355
+ # @yield Block for schema definition
356
+ # @return [Schema] The defined schema
357
+ def self.define(&block)
358
+ schema = new
359
+ schema.instance_eval(&block) if block
360
+ schema
361
+ end
362
+
363
+ def initialize
364
+ @fields = {}
365
+ @rules = []
366
+ end
367
+
368
+ # Define a required field
369
+ #
370
+ # @param name [Symbol] Field name
371
+ # @param type [Class, nil] Expected type
372
+ # @param options [Hash] Additional options
373
+ # @return [void]
374
+ def required(name, type: nil, **options)
375
+ @fields[name] = { required: true, type: type, **options }
376
+ end
377
+
378
+ # Define an optional field
379
+ #
380
+ # @param name [Symbol] Field name
381
+ # @param type [Class, nil] Expected type
382
+ # @param options [Hash] Additional options
383
+ # @return [void]
384
+ def optional(name, type: nil, **options)
385
+ @fields[name] = { required: false, type: type, **options }
386
+ end
387
+
388
+ # Add a custom validation rule
389
+ #
390
+ # @param name [Symbol] Rule name
391
+ # @yield Block for validation
392
+ # @return [void]
393
+ def rule(name, &block)
394
+ @rules << Rules::Custom.new(name, block: block)
395
+ end
396
+
397
+ # Add a pre-built rule
398
+ #
399
+ # @param rule [Rule] The rule to add
400
+ # @return [void]
401
+ def add_rule(rule)
402
+ @rules << rule
403
+ end
404
+
405
+ # Validate a document
406
+ #
407
+ # @param document [Object] Document to validate
408
+ # @return [Result] Validation result
409
+ def validate(document)
410
+ result = Result.new
411
+
412
+ # Validate fields
413
+ @fields.each do |name, config|
414
+ validate_field(document, name, config, result)
415
+ end
416
+
417
+ # Run custom rules
418
+ @rules.each do |rule|
419
+ errors = rule.validate(document, schema: self)
420
+ errors.each { |e| result.add_error(e, code: rule.name) }
421
+ end
422
+
423
+ result
424
+ end
425
+
426
+ private
427
+
428
+ def validate_field(document, name, config, result)
429
+ value = field_value(document, name)
430
+ path = name.to_s
431
+
432
+ # Check required
433
+ if config[:required] && value.nil?
434
+ result.add_error("#{name} is required", path: path, code: :required)
435
+ return
436
+ end
437
+
438
+ return if value.nil?
439
+
440
+ # Check type
441
+ if config[:type] && !value.is_a?(config[:type])
442
+ result.add_error(
443
+ "#{name} must be #{config[:type].name}, got #{value.class.name}",
444
+ path: path,
445
+ code: :type
446
+ )
447
+ end
448
+
449
+ # Check min_length
450
+ if config[:min_length] && value.is_a?(String) && (value.length < config[:min_length])
451
+ result.add_error(
452
+ "#{name} must have at least #{config[:min_length]} characters",
453
+ path: path,
454
+ code: :min_length
455
+ )
456
+ end
457
+
458
+ # Check max_length
459
+ if config[:max_length] && value.is_a?(String) && (value.length > config[:max_length])
460
+ result.add_error(
461
+ "#{name} must have at most #{config[:max_length]} characters",
462
+ path: path,
463
+ code: :max_length
464
+ )
465
+ end
466
+
467
+ # Check min_count
468
+ if config[:min_count] && value.is_a?(Enumerable) && (value.count < config[:min_count])
469
+ result.add_error(
470
+ "#{name} must have at least #{config[:min_count]} items",
471
+ path: path,
472
+ code: :min_count
473
+ )
474
+ end
475
+
476
+ # Check format
477
+ return unless config[:format].is_a?(Regexp) && !config[:format].match?(value.to_s)
478
+
479
+ result.add_error(
480
+ "#{name} has invalid format",
481
+ path: path,
482
+ code: :format
483
+ )
484
+ end
485
+
486
+ def field_value(document, field)
487
+ if document.is_a?(CoreModel::Base)
488
+ document.public_send(field) if document.class.attributes.key?(field)
489
+ else
490
+ document.public_send(field)
491
+ end
492
+ rescue NoMethodError
493
+ nil
494
+ end
495
+ end
496
+
497
+ # Schema generator from CoreModel types
498
+ #
499
+ # Automatically generates validation schemas from Lutaml::Model classes.
500
+ # This enables automatic validation based on model structure.
501
+ #
502
+ # @example Generate schema from CoreModel class
503
+ # schema = Coridoc::Validation::SchemaGenerator.generate(Coridoc::CoreModel::StructuralElement)
504
+ # result = schema.validate(document)
505
+ #
506
+ # @example Customize generated schema
507
+ # schema = Coridoc::Validation::SchemaGenerator.generate(
508
+ # Coridoc::CoreModel::Block,
509
+ # required: [:content],
510
+ # ignored: [:metadata]
511
+ # )
512
+ #
513
+ class SchemaGenerator
514
+ class << self
515
+ # Generate a validation schema from a CoreModel class
516
+ #
517
+ # @param model_class [Class] The CoreModel class to generate schema from
518
+ # @param required [Array<Symbol>] Attributes to mark as required
519
+ # @param ignored [Array<Symbol>] Attributes to skip in schema
520
+ # @param custom_rules [Hash] Additional validation rules per attribute
521
+ # @return [Schema] Generated validation schema
522
+ #
523
+ # @example Basic generation
524
+ # schema = SchemaGenerator.generate(Coridoc::CoreModel::Block)
525
+ #
526
+ # @example With required fields
527
+ # schema = SchemaGenerator.generate(
528
+ # Coridoc::CoreModel::Block,
529
+ # required: [:content, :delimiter_type]
530
+ # )
531
+ #
532
+ # @example With custom rules
533
+ # schema = SchemaGenerator.generate(
534
+ # Coridoc::CoreModel::StructuralElement,
535
+ # custom_rules: {
536
+ # level: { min: 1, max: 6 }
537
+ # }
538
+ # )
539
+ #
540
+ def generate(model_class, required: [], ignored: [], custom_rules: {})
541
+ return nil unless model_class.is_a?(Class) && model_class < CoreModel::Base
542
+
543
+ # Pre-compute attribute definitions before the schema block
544
+ attribute_defs = compute_attribute_definitions(
545
+ model_class, required, ignored, custom_rules
546
+ )
547
+
548
+ Schema.define do
549
+ attribute_defs.each do |name, type, options, is_required|
550
+ if is_required
551
+ required name, type: type, **options
552
+ else
553
+ optional name, type: type, **options
554
+ end
555
+ end
556
+ end
557
+ end
558
+
559
+ # Map Lutaml::Model type to Ruby class
560
+ #
561
+ # @param type [Symbol, Class] The Lutaml::Model type
562
+ # @return [Class, Array<Class>] Ruby class(es)
563
+ def map_type(type)
564
+ # Handle Lutaml::Model type classes by name
565
+ type_name = type.to_s
566
+
567
+ case type_name
568
+ when 'Lutaml::Model::Type::String'
569
+ String
570
+ when 'Lutaml::Model::Type::Integer'
571
+ Integer
572
+ when 'Lutaml::Model::Type::Float'
573
+ Float
574
+ when 'Lutaml::Model::Type::Boolean'
575
+ [TrueClass, FalseClass]
576
+ when 'Lutaml::Model::Type::Date'
577
+ Date
578
+ when 'Lutaml::Model::Type::Time'
579
+ Time
580
+ when 'Lutaml::Model::Type::DateTime'
581
+ Time
582
+ when 'Lutaml::Model::Type::Hash'
583
+ Hash
584
+ when 'Lutaml::Model::Type::Array'
585
+ Array
586
+ else
587
+ # For non-Lutaml types (like CoreModel::Base), return the type itself
588
+ type.is_a?(Class) ? type : Object
589
+ end
590
+ end
591
+
592
+ private
593
+
594
+ # Compute attribute definitions for the schema
595
+ #
596
+ # @return [Array<Array>] Array of [name, type, options, required] tuples
597
+ def compute_attribute_definitions(model_class, required, ignored, custom_rules)
598
+ model_class.attributes.filter_map do |name, attr|
599
+ next if ignored.include?(name)
600
+
601
+ type = collection?(attr) ? Array : map_type(attr.type)
602
+ options = build_options(attr, custom_rules[name])
603
+ # By default, all fields are optional unless explicitly required
604
+ is_required = required.include?(name)
605
+
606
+ [name, type, options, is_required]
607
+ end
608
+ end
609
+
610
+ # Check if attribute is a collection
611
+ #
612
+ # @param attr [Lutaml::Model::Attribute] The attribute
613
+ # @return [Boolean]
614
+ def collection?(attr)
615
+ attr.options[:collection] == true
616
+ end
617
+
618
+ # Build validation options for an attribute
619
+ #
620
+ # @param attr [Lutaml::Model::Attribute] The attribute
621
+ # @param custom [Hash, nil] Custom rules for this attribute
622
+ # @return [Hash] Validation options
623
+ def build_options(attr, custom = nil)
624
+ options = {}
625
+
626
+ # Add collection validation
627
+ options[:min_count] = 0 if collection?(attr)
628
+
629
+ # Merge custom rules
630
+ options.merge!(custom) if custom
631
+
632
+ options
633
+ end
634
+ end
635
+ end
636
+
637
+ # Module-level methods
638
+ class << self
639
+ # Define a validation schema
640
+ #
641
+ # @yield Schema definition block
642
+ # @return [Schema] The defined schema
643
+ def define(&)
644
+ Schema.define(&)
645
+ end
646
+
647
+ # Generate a validation schema from a CoreModel class
648
+ #
649
+ # @param model_class [Class] The CoreModel class
650
+ # @param options [Hash] Options passed to SchemaGenerator.generate
651
+ # @return [Schema] Generated validation schema
652
+ #
653
+ # @example
654
+ # schema = Coridoc::Validation.auto_schema(Coridoc::CoreModel::Block)
655
+ # result = schema.validate(document)
656
+ #
657
+ def auto_schema(model_class, **)
658
+ SchemaGenerator.generate(model_class, **)
659
+ end
660
+
661
+ # Validate a document with default schema
662
+ #
663
+ # @param document [Object] Document to validate
664
+ # @return [Result] Validation result
665
+ def validate(document)
666
+ default_schema.validate(document)
667
+ end
668
+
669
+ # Get the default validation schema
670
+ #
671
+ # @return [Schema]
672
+ def default_schema
673
+ @default_schema ||= Schema.define do
674
+ optional :id, type: String
675
+ optional :title, type: String
676
+ end
677
+ end
678
+
679
+ # Set the default validation schema
680
+ #
681
+ # @param schema [Schema] The schema to use
682
+ # @return [void]
683
+ attr_writer :default_schema
684
+ end
685
+ end
686
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ VERSION = '3.0.0'
5
+ end