Class: Lutaml::Jsonschema::SchemaSet

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/jsonschema/schema_set.rb

Defined Under Namespace

Classes: ValidationError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_dirObject (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

#schemasObject (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



93
94
95
96
97
# File 'lib/lutaml/jsonschema/schema_set.rb', line 93

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)
  end
  set
end

Instance Method Details

#add(name, schema, file_path = nil) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/lutaml/jsonschema/schema_set.rb', line 30

def add(name, schema, file_path = nil)
  @schemas[name] = schema
  return unless file_path

  @file_paths ||= {}
  @file_paths[File.basename(file_path)] = file_path
end

#all_definitionsObject



71
72
73
74
75
# File 'lib/lutaml/jsonschema/schema_set.rb', line 71

def all_definitions
  @schemas.flat_map do |_name, schema|
    schema.definition_entries
  end
end

#all_propertiesObject



77
78
79
80
81
# File 'lib/lutaml/jsonschema/schema_set.rb', line 77

def all_properties
  @schemas.flat_map do |_name, schema|
    schema.property_entries
  end
end

#resolve_ref(ref_string, context_schema = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/lutaml/jsonschema/schema_set.rb', line 38

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

#valid?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/lutaml/jsonschema/schema_set.rb', line 67

def valid?
  validate! rescue false
end

#validate!Object

Raises:



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/lutaml/jsonschema/schema_set.rb', line 54

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_errorsObject



83
84
85
86
87
88
89
# File 'lib/lutaml/jsonschema/schema_set.rb', line 83

def validation_errors
  errors = []
  @schemas.each do |name, schema|
    collect_refs(schema, name, errors, Set.new, "")
  end
  errors
end