json_schema 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 55a7249029791e99fb8fc7ad76a76edeab610705
4
- data.tar.gz: 71e49d15e5ad71b23f353bc713634763f21c2f0c
3
+ metadata.gz: 9bfb85015a2b3334dfbcdcd269fd7031f74f3b42
4
+ data.tar.gz: 1d2050d6970717a4a083470ed3250a87321bdf3b
5
5
  SHA512:
6
- metadata.gz: 4dd1b0c124657f130648484c90b5cce1f7f86deb01eaa6b8f1d9aec40060b4b6d52a546e33a987186b732c12b66f666f5c71391b33df98f1b7ac766becfd69c9
7
- data.tar.gz: d0745914ef1acfaa8a039577a8712beb3020827237979deb836dfb59464ce6cdc3424ec9970aa9c9fa8f3c2aae595acff44d819f797e1cc18ad2a808f6315917
6
+ metadata.gz: 95c592714aaab7510d4faa0fa94ee82253ade1cae378e5712b872a013ab66e9e525c42208376c13d9cef35a686859db5a748337b5d30841ec3c6a9dab8ddd88e
7
+ data.tar.gz: 203d25e5c27568618d4c51494ed915cbc87dc8da2362caacb3d7a0a59e938e74510bc94df81587269e4043138eb2f4ed601371f5cb33012c3e52bdf474fcf44e
data/lib/json_schema.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require_relative "json_schema/configuration"
1
2
  require_relative "json_schema/document_store"
2
3
  require_relative "json_schema/parser"
3
4
  require_relative "json_schema/reference_expander"
@@ -6,6 +7,14 @@ require_relative "json_schema/schema_error"
6
7
  require_relative "json_schema/validator"
7
8
 
8
9
  module JsonSchema
10
+ def self.configure
11
+ yield configuration
12
+ end
13
+
14
+ def self.configuration
15
+ @configuration ||= Configuration.new
16
+ end
17
+
9
18
  def self.parse(data)
10
19
  parser = Parser.new
11
20
  if schema = parser.parse(data)
@@ -0,0 +1,16 @@
1
+ module JsonSchema
2
+ class Configuration
3
+ attr_reader :validate_regex_with
4
+
5
+ def validate_regex_with=(validator)
6
+ @validate_regex_with = validator
7
+ end
8
+
9
+ private
10
+
11
+ def initialize
12
+ @validate_regex_with = nil
13
+ end
14
+
15
+ end
16
+ end
@@ -211,12 +211,23 @@ module JsonSchema
211
211
  # leave the original data reference intact
212
212
  properties = schema.pattern_properties.dup
213
213
  properties = properties.map do |k, s|
214
- [Regexp.new(k), parse_data(s, schema, "patternProperties/#{k}")]
214
+ [parse_regex(schema, k), parse_data(s, schema, "patternProperties/#{k}")]
215
215
  end
216
216
  schema.pattern_properties = Hash[*properties.flatten]
217
217
  end
218
218
  end
219
219
 
220
+ def parse_regex(schema, regex)
221
+ case JsonSchema.configuration.validate_regex_with
222
+ when :'ecma-re-validator'
223
+ unless EcmaReValidator.valid?(regex)
224
+ message = %{#{regex.inspect} is not an ECMA-262 regular expression.}
225
+ @errors << SchemaError.new(schema, message, :regex_failed)
226
+ end
227
+ end
228
+ Regexp.new(regex)
229
+ end
230
+
220
231
  def parse_properties(schema)
221
232
  # leave the original data reference intact
222
233
  schema.properties = schema.properties.dup
@@ -287,7 +298,7 @@ module JsonSchema
287
298
  schema.max_length = validate_type(schema, [Integer], "maxLength")
288
299
  schema.min_length = validate_type(schema, [Integer], "minLength")
289
300
  schema.pattern = validate_type(schema, [String], "pattern")
290
- schema.pattern = Regexp.new(schema.pattern) if schema.pattern
301
+ schema.pattern = parse_regex(schema, schema.pattern) if schema.pattern
291
302
  validate_format(schema, schema.format) if schema.format
292
303
 
293
304
  # hyperschema
@@ -444,6 +444,7 @@ module JsonSchema
444
444
 
445
445
  def validate_pattern(schema, data, errors, path)
446
446
  return true unless schema.pattern
447
+
447
448
  if data =~ schema.pattern
448
449
  true
449
450
  else
@@ -3,6 +3,12 @@ require "test_helper"
3
3
  require "json_schema"
4
4
 
5
5
  describe JsonSchema::Parser do
6
+ after do
7
+ JsonSchema.configure do |c|
8
+ c.validate_regex_with = nil
9
+ end
10
+ end
11
+
6
12
  it "parses the basic attributes of a schema" do
7
13
  schema = parse
8
14
  assert_nil schema.id
@@ -266,6 +272,22 @@ describe JsonSchema::Parser do
266
272
  assert_includes error_types, :unknown_format
267
273
  end
268
274
 
275
+ it "passes for an invalid regex when not asked to check" do
276
+ schema_sample["pattern"] = "\\Ameow"
277
+ assert parse
278
+ end
279
+
280
+ it "errors for an invalid regex when asked to check" do
281
+ require 'ecma-re-validator'
282
+ JsonSchema.configure do |c|
283
+ c.validate_regex_with = :'ecma-re-validator'
284
+ end
285
+ schema_sample["pattern"] = "\\Ameow"
286
+ refute parse
287
+ assert_includes error_messages, '"\\\\Ameow" is not an ECMA-262 regular expression.'
288
+ assert_includes error_types, :regex_failed
289
+ end
290
+
269
291
  def error_messages
270
292
  @parser.errors.map { |e| e.message }
271
293
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandur
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-03 00:00:00.000000000 Z
11
+ date: 2015-03-31 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ecma-re-validator
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.1'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.1'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: minitest
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -53,6 +67,7 @@ files:
53
67
  - lib/json_pointer/evaluator.rb
54
68
  - lib/json_reference.rb
55
69
  - lib/json_schema.rb
70
+ - lib/json_schema/configuration.rb
56
71
  - lib/json_schema/document_store.rb
57
72
  - lib/json_schema/parser.rb
58
73
  - lib/json_schema/reference_expander.rb