Module: Causalontology::Schema

Defined in:
lib/causalontology/schema.rb

Constant Summary collapse

SCHEMA_FILES =

kind -> schema file. Three token kinds keep their original 1.0.0-reserved file names (individual/token/state); the id scheme is the whole word.

{
  "occurrent"  => "occurrent.schema.json",
  "causal_relation_object" => "causal_relation_object.schema.json",
  "continuant" => "continuant.schema.json",
  "realizable" => "realizable.schema.json",
  "stratum"    => "stratum.schema.json",
  "bridge"     => "bridge.schema.json",
  "port"       => "port.schema.json",
  "conduit"    => "conduit.schema.json",
  "quality"    => "quality.schema.json",
  "token_individual"   => "individual.schema.json",
  "token_occurrence"   => "token.schema.json",
  "state_assertion"    => "state.schema.json",
  "token_causal_claim" => "token_causal_claim.schema.json",
  "assertion"  => "assertion.schema.json",
  "enrichment" => "enrichment.schema.json",
  "retraction" => "retraction.schema.json",
  "succession" => "succession.schema.json",
}.freeze
BASE =
"https://causalontology.org/schema/"

Class Method Summary collapse

Class Method Details

.check(value, schema, root, path, errors) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/causalontology/schema.rb', line 110

def check(value, schema, root, path, errors)
  schema, root = resolve_ref(schema, root)

  if schema.key?("oneOf")
    passing = 0
    schema["oneOf"].each do |sub|
      suberrs = []
      check(value, sub, root, path, suberrs)
      passing += 1 if suberrs.empty?
    end
    if passing != 1
      errors << "#{path}: matches #{passing} of the oneOf branches " \
                "(need exactly 1)"
    end
    return
  end

  t = schema["type"]
  if !t.nil? && !type_matches?(value, t)
    errors << "#{path}: expected #{t}"
    return
  end

  if schema.key?("const") && value != schema["const"]
    errors << "#{path}: must equal #{schema["const"].inspect}"
  end
  if schema.key?("enum") && !schema["enum"].include?(value)
    errors << "#{path}: #{value.inspect} not in enumeration"
  end
  if schema.key?("pattern") && value.is_a?(String)
    unless Regexp.new(schema["pattern"]).match?(value)
      errors << "#{path}: #{value.inspect} does not match " \
                "#{schema["pattern"]}"
    end
  end
  if schema.key?("minLength") && value.is_a?(String)
    if value.length < schema["minLength"]
      errors << "#{path}: shorter than minLength"
    end
  end
  if schema.key?("minimum") && numeric?(value)
    if value < schema["minimum"]
      errors << "#{path}: below minimum #{schema["minimum"]}"
    end
  end
  if schema.key?("maximum") && numeric?(value)
    if value > schema["maximum"]
      errors << "#{path}: above maximum #{schema["maximum"]}"
    end
  end

  if value.is_a?(Array)
    if schema.key?("minItems") && value.length < schema["minItems"]
      errors << "#{path}: fewer than #{schema["minItems"]} items"
    end
    if schema.key?("items")
      value.each_with_index do |item, i|
        check(item, schema["items"], root, "#{path}[#{i}]", errors)
      end
    end
  end

  if value.is_a?(Hash)
    props = schema["properties"] || {}
    (schema["required"] || []).each do |req|
      unless value.key?(req)
        errors << "#{path}: required property '#{req}' missing"
      end
    end
    if schema["additionalProperties"] == false
      value.each_key do |key|
        unless props.key?(key)
          errors << "#{path}: additional property '#{key}'"
        end
      end
    end
    props.each do |key, sub|
      if value.key?(key)
        check(value[key], sub, root, "#{path}.#{key}", errors)
      end
    end
  end
end

.load_file(filename) ⇒ Object

Load and cache a schema document by its file name.



52
53
54
55
# File 'lib/causalontology/schema.rb', line 52

def load_file(filename)
  @cache[filename] ||= JSON.parse(
    File.read(File.join(schema_dir, filename)))
end

.load_schema(kind) ⇒ Object



57
58
59
60
61
62
# File 'lib/causalontology/schema.rb', line 57

def load_schema(kind)
  unless SCHEMA_FILES.key?(kind)
    raise ArgumentError, "unknown kind: #{kind.inspect}"
  end
  load_file(SCHEMA_FILES[kind])
end

Navigate a JSON pointer (slash-separated) within a document.



65
66
67
68
69
70
71
72
# File 'lib/causalontology/schema.rb', line 65

def navigate(doc, pointer)
  node = doc
  pointer.split("/").each do |part|
    next if part == ""
    node = node[part]
  end
  node
end

.numeric?(value) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/causalontology/schema.rb', line 106

def numeric?(value)
  value.is_a?(Integer) || value.is_a?(Float)
end

.resolve_ref(schema, root) ⇒ Object

Resolve local and cross-file $refs to a concrete schema node plus the root document it lives in. Returns [schema, root].



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/causalontology/schema.rb', line 76

def resolve_ref(schema, root)
  while schema.is_a?(Hash) && schema.key?("$ref")
    ref = schema["$ref"]
    if ref.start_with?("#/")
      schema = navigate(root, ref[2..])
    elsif ref.start_with?(BASE)
      rest = ref[BASE.length..]
      filename, pointer = rest.split("#/", 2)
      root = load_file(filename)
      schema = pointer ? navigate(root, pointer) : root
    else
      raise ArgumentError, "unsupported $ref: #{ref.inspect}"
    end
  end
  [schema, root]
end

.schema_dirObject



44
45
46
47
48
49
# File 'lib/causalontology/schema.rb', line 44

def schema_dir
  env = ENV["CAUSALONTOLOGY_SPEC"]
  return File.join(env, "schema") if env && !env.empty?
  # lib/causalontology -> lib -> ruby -> bindings -> repository root
  File.expand_path("../../../../spec/schema", __dir__)
end

.type_matches?(value, t) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/causalontology/schema.rb', line 93

def type_matches?(value, t)
  case t
  when "object"  then value.is_a?(Hash)
  when "array"   then value.is_a?(Array)
  when "string"  then value.is_a?(String)
  when "number"  then value.is_a?(Integer) || value.is_a?(Float)
  when "integer" then value.is_a?(Integer)
  when "boolean" then value == true || value == false
  else
    raise ArgumentError, "unknown schema type: #{t.inspect}"
  end
end

.validate_schema(obj, kind = nil) ⇒ Object

[ok, reasons] - structural validity against the kind's JSON Schema.



195
196
197
198
199
200
201
# File 'lib/causalontology/schema.rb', line 195

def validate_schema(obj, kind = nil)
  kind ||= Canonical.infer_kind(obj)
  root = load_schema(kind)
  errors = []
  check(obj, root, root, "$", errors)
  [errors.empty?, errors]
end