Class: JSON::Validator
- Inherits:
-
Object
- Object
- JSON::Validator
- Defined in:
- lib/json-schema/validator.rb
Constant Summary collapse
- @@schemas =
{}
- @@cache_schemas =
false
- @@default_opts =
{ :list => false }
- @@validators =
{}
- @@default_validator =
nil
Class Method Summary collapse
- .add_schema(schema) ⇒ Object
- .cache_schemas=(val) ⇒ Object
- .clear_cache ⇒ Object
- .default_validator ⇒ Object
- .register_default_validator(v) ⇒ Object
- .register_validator(v) ⇒ Object
- .schemas ⇒ Object
- .validate(schema, data, opts = {}) ⇒ Object
- .validate!(schema, data, opts = {}) ⇒ Object (also: validate2)
- .validators ⇒ Object
Instance Method Summary collapse
-
#build_schemas(parent_schema) ⇒ Object
Build all schemas with IDs, mapping out the namespace.
-
#handle_schema(parent_schema, obj) ⇒ Object
Either load a reference schema or create a new schema.
-
#initialize(schema_data, data, opts = {}) ⇒ Validator
constructor
A new instance of Validator.
- #load_ref_schema(parent_schema, ref) ⇒ Object
-
#validate ⇒ Object
Run a simple true/false validation of data against a schema.
Constructor Details
#initialize(schema_data, data, opts = {}) ⇒ Validator
Returns a new instance of Validator.
77 78 79 80 81 82 |
# File 'lib/json-schema/validator.rb', line 77 def initialize(schema_data, data, opts={}) @options = @@default_opts.clone.merge(opts) @base_schema = initialize_schema(schema_data) @data = initialize_data(data) build_schemas(@base_schema) end |
Class Method Details
.add_schema(schema) ⇒ Object
216 217 218 |
# File 'lib/json-schema/validator.rb', line 216 def add_schema(schema) @@schemas[schema.uri.to_s] = schema if @@schemas[schema.uri.to_s].nil? end |
.cache_schemas=(val) ⇒ Object
220 221 222 |
# File 'lib/json-schema/validator.rb', line 220 def cache_schemas=(val) @@cache_schemas = val == true ? true : false end |
.clear_cache ⇒ Object
208 209 210 |
# File 'lib/json-schema/validator.rb', line 208 def clear_cache @@schemas = {} if @@cache_schemas == false end |
.default_validator ⇒ Object
228 229 230 |
# File 'lib/json-schema/validator.rb', line 228 def default_validator @@default_validator end |
.register_default_validator(v) ⇒ Object
236 237 238 |
# File 'lib/json-schema/validator.rb', line 236 def register_default_validator(v) @@default_validator = v end |
.register_validator(v) ⇒ Object
232 233 234 |
# File 'lib/json-schema/validator.rb', line 232 def register_validator(v) @@validators[v.to_s] = v end |
.schemas ⇒ Object
212 213 214 |
# File 'lib/json-schema/validator.rb', line 212 def schemas @@schemas end |
.validate(schema, data, opts = {}) ⇒ Object
192 193 194 195 196 197 198 199 200 |
# File 'lib/json-schema/validator.rb', line 192 def validate(schema, data,opts={}) begin validator = JSON::Validator.new(schema, data, opts) validator.validate return true rescue JSON::Schema::ValidationError, JSON::Schema::SchemaError return false end end |
.validate!(schema, data, opts = {}) ⇒ Object Also known as: validate2
202 203 204 205 |
# File 'lib/json-schema/validator.rb', line 202 def validate!(schema, data,opts={}) validator = JSON::Validator.new(schema, data, opts) validator.validate end |
.validators ⇒ Object
224 225 226 |
# File 'lib/json-schema/validator.rb', line 224 def validators @@validators end |
Instance Method Details
#build_schemas(parent_schema) ⇒ Object
Build all schemas with IDs, mapping out the namespace
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 |
# File 'lib/json-schema/validator.rb', line 135 def build_schemas(parent_schema) # Check for schemas in union types ["type", "disallow"].each do |key| if parent_schema.schema[key] && parent_schema.schema[key].is_a?(Array) parent_schema.schema[key].each_with_index do |type,i| if type.is_a?(Hash) handle_schema(parent_schema, type) end end end end # All properties are schemas if parent_schema.schema["properties"] parent_schema.schema["properties"].each do |k,v| handle_schema(parent_schema, v) end end # Items are always schemas if parent_schema.schema["items"] items = parent_schema.schema["items"].clone single = false if !items.is_a?(Array) items = [items] single = true end items.each_with_index do |item,i| handle_schema(parent_schema, item) end end # Each of these might be schemas ["additionalProperties", "additionalItems", "dependencies", "extends"].each do |key| if parent_schema.schema[key].is_a?(Hash) handle_schema(parent_schema, parent_schema.schema[key]) end end end |
#handle_schema(parent_schema, obj) ⇒ Object
Either load a reference schema or create a new schema
177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/json-schema/validator.rb', line 177 def handle_schema(parent_schema, obj) if obj['$ref'] load_ref_schema(parent_schema, obj['$ref']) else schema_uri = parent_schema.uri.clone schema = JSON::Schema.new(obj,schema_uri) if obj['id'] Validator.add_schema(schema) end build_schemas(schema) end end |
#load_ref_schema(parent_schema, ref) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/json-schema/validator.rb', line 97 def load_ref_schema(parent_schema,ref) uri = URI.parse(ref) if uri.relative? uri = parent_schema.uri.clone # Check for absolute path path = ref.split("#")[0] # This is a self reference and thus the schema does not need to be re-loaded if path.nil? || path == '' return end if path && path[0,1] == '/' uri.path = Pathname.new(path).cleanpath.to_s else uri.path = (Pathname.new(parent_schema.uri.path).parent + path).cleanpath.to_s end uri.fragment = nil end if Validator.schemas[uri.to_s].nil? begin schema = JSON::Schema.new(JSON.parse(open(uri.to_s).read), uri) Validator.add_schema(schema) build_schemas(schema) rescue JSON::ParserError # Don't rescue this error, we want JSON formatting issues to bubble up raise $! rescue # Failures will occur when this URI cannot be referenced yet. Don't worry about it, # the proper error will fall out if the ref isn't ever defined end end end |
#validate ⇒ Object
Run a simple true/false validation of data against a schema
86 87 88 89 90 91 92 93 94 |
# File 'lib/json-schema/validator.rb', line 86 def validate() begin @base_schema.validate(@data,[]) Validator.clear_cache rescue JSON::Schema::ValidationError Validator.clear_cache raise $! end end |