Class: Lutaml::Jsonschema::SchemaSet
- Inherits:
-
Object
- Object
- Lutaml::Jsonschema::SchemaSet
- Defined in:
- lib/lutaml/jsonschema/schema_set.rb
Defined Under Namespace
Classes: ValidationError
Instance Attribute Summary collapse
-
#base_dir ⇒ Object
readonly
Returns the value of attribute base_dir.
-
#schemas ⇒ Object
readonly
Returns the value of attribute schemas.
Class Method Summary collapse
- .infer_base_dir(paths) ⇒ Object
- .load_from_directory(dir) ⇒ Object
- .load_from_files(*paths, base_dir: nil) ⇒ Object
Instance Method Summary collapse
- #add(name, schema, file_path = nil, source_json = nil) ⇒ Object
- #all_definitions ⇒ Object
- #all_properties ⇒ Object
-
#initialize(base_dir: nil) ⇒ SchemaSet
constructor
A new instance of SchemaSet.
- #resolve_ref(ref_string, context_schema = nil) ⇒ Object
- #source_json(name) ⇒ Object
- #valid? ⇒ Boolean
- #validate! ⇒ Object
- #validation_errors ⇒ Object
Constructor Details
#initialize(base_dir: nil) ⇒ SchemaSet
Returns a new instance of SchemaSet.
8 9 10 11 12 |
# File 'lib/lutaml/jsonschema/schema_set.rb', line 8 def initialize(base_dir: nil) @schemas = {} @base_dir = base_dir @resolver = ReferenceResolver.new(@schemas) end |
Instance Attribute Details
#base_dir ⇒ Object (readonly)
Returns the value of attribute base_dir.
6 7 8 |
# File 'lib/lutaml/jsonschema/schema_set.rb', line 6 def base_dir @base_dir end |
#schemas ⇒ Object (readonly)
Returns the value of attribute schemas.
6 7 8 |
# File 'lib/lutaml/jsonschema/schema_set.rb', line 6 def schemas @schemas end |
Class Method Details
.infer_base_dir(paths) ⇒ Object
101 102 103 104 105 |
# File 'lib/lutaml/jsonschema/schema_set.rb', line 101 def self.infer_base_dir(paths) return nil if paths.empty? File.dirname(paths.first) end |
.load_from_directory(dir) ⇒ Object
25 26 27 28 |
# File 'lib/lutaml/jsonschema/schema_set.rb', line 25 def self.load_from_directory(dir) paths = Dir.glob(File.join(dir, "*.json")) load_from_files(*paths, base_dir: dir) end |
.load_from_files(*paths, base_dir: nil) ⇒ Object
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/lutaml/jsonschema/schema_set.rb', line 14 def self.load_from_files(*paths, base_dir: nil) set = new(base_dir: base_dir || infer_base_dir(paths)) paths.each do |path| data = File.read(path) schema = Schema.from_json(data) name = File.basename(path, ".*") set.add(name, schema, path, data) end set end |
Instance Method Details
#add(name, schema, file_path = nil, source_json = nil) ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/lutaml/jsonschema/schema_set.rb', line 30 def add(name, schema, file_path = nil, source_json = nil) @schemas[name] = schema return unless file_path @file_paths ||= {} @file_paths[File.basename(file_path)] = file_path @source_jsons ||= {} @source_jsons[name] = source_json if source_json end |
#all_definitions ⇒ Object
79 80 81 82 83 |
# File 'lib/lutaml/jsonschema/schema_set.rb', line 79 def all_definitions @schemas.flat_map do |_name, schema| schema.definition_entries end end |
#all_properties ⇒ Object
85 86 87 88 89 |
# File 'lib/lutaml/jsonschema/schema_set.rb', line 85 def all_properties @schemas.flat_map do |_name, schema| schema.property_entries end end |
#resolve_ref(ref_string, context_schema = nil) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/lutaml/jsonschema/schema_set.rb', line 44 def resolve_ref(ref_string, context_schema = nil) return nil unless ref_string if ref_string.start_with?("./") resolve_file_ref(ref_string, context_schema) elsif ref_string.start_with?("http://", "https://") resolve_remote_ref(ref_string) elsif ref_string.start_with?("#/") @resolver.resolve(ref_string, context_schema) elsif ref_string.start_with?("#") && !ref_string.start_with?("#/") resolve_anchor_ref(ref_string.delete_prefix("#"), context_schema) else @resolver.resolve(ref_string, context_schema) end end |
#source_json(name) ⇒ Object
40 41 42 |
# File 'lib/lutaml/jsonschema/schema_set.rb', line 40 def source_json(name) @source_jsons&.dig(name) end |
#valid? ⇒ Boolean
73 74 75 76 77 |
# File 'lib/lutaml/jsonschema/schema_set.rb', line 73 def valid? validate! rescue StandardError false end |
#validate! ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/lutaml/jsonschema/schema_set.rb', line 60 def validate! errors = [] seen_refs = Set.new @schemas.each do |name, schema| collect_refs(schema, name, errors, seen_refs, "") end raise ValidationError, errors.join("\n") if errors.any? true end |
#validation_errors ⇒ Object
91 92 93 94 95 96 97 |
# File 'lib/lutaml/jsonschema/schema_set.rb', line 91 def validation_errors errors = [] @schemas.each do |name, schema| collect_refs(schema, name, errors, Set.new, "") end errors end |