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", "cross_stratal_seam" => "cross_stratal_seam.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", "attitude" => "attitude.schema.json", "predicted_occurrence" => "predicted_occurrence.schema.json", "prediction_error" => "prediction_error.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
- .check(value, schema, root, path, errors) ⇒ Object
-
.load_file(filename) ⇒ Object
Load and cache a schema document by its file name.
- .load_schema(kind) ⇒ Object
-
.navigate(doc, pointer) ⇒ Object
Navigate a JSON pointer (slash-separated) within a document.
- .numeric?(value) ⇒ Boolean
-
.resolve_ref(schema, root) ⇒ Object
Resolve local and cross-file $refs to a concrete schema node plus the root document it lives in.
-
.schema_dir ⇒ Object
Where the twenty-one *.schema.json files live, in strict precedence:.
- .type_matches?(value, t) ⇒ Boolean
-
.validate_schema(obj, kind = nil) ⇒ Object
[ok, reasons] - structural validity against the kind's JSON Schema.
Class Method Details
.check(value, schema, root, path, errors) ⇒ Object
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 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/causalontology/schema.rb', line 123 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.
65 66 67 68 |
# File 'lib/causalontology/schema.rb', line 65 def load_file(filename) @cache[filename] ||= JSON.parse( File.read(File.join(schema_dir, filename))) end |
.load_schema(kind) ⇒ Object
70 71 72 73 74 75 |
# File 'lib/causalontology/schema.rb', line 70 def load_schema(kind) unless SCHEMA_FILES.key?(kind) raise ArgumentError, "unknown kind: #{kind.inspect}" end load_file(SCHEMA_FILES[kind]) end |
.navigate(doc, pointer) ⇒ Object
Navigate a JSON pointer (slash-separated) within a document.
78 79 80 81 82 83 84 85 |
# File 'lib/causalontology/schema.rb', line 78 def navigate(doc, pointer) node = doc pointer.split("/").each do |part| next if part == "" node = node[part] end node end |
.numeric?(value) ⇒ Boolean
119 120 121 |
# File 'lib/causalontology/schema.rb', line 119 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].
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/causalontology/schema.rb', line 89 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_dir ⇒ Object
Where the twenty-one *.schema.json files live, in strict precedence:
(a) $CAUSALONTOLOGY_SPEC/schema - explicit override, always wins;
(b) the copy vendored inside the gem - what an installed consumer
uses, so validation works standalone with no repository present;
(c) the repository-relative path - last resort, for working directly
in a checkout where (b) has not been vendored yet.
55 56 57 58 59 60 61 62 |
# File 'lib/causalontology/schema.rb', line 55 def schema_dir env = ENV["CAUSALONTOLOGY_SPEC"] return File.join(env, "schema") if env && !env.empty? bundled = File.("spec/schema", __dir__) return bundled if File.directory?(bundled) # lib/causalontology -> lib -> ruby -> bindings -> repository root File.("../../../../spec/schema", __dir__) end |
.type_matches?(value, t) ⇒ Boolean
106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/causalontology/schema.rb', line 106 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.
208 209 210 211 212 213 214 |
# File 'lib/causalontology/schema.rb', line 208 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 |