frise 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 41db8000684dfb7ee23cc7f26a790a57bb38ad76
4
- data.tar.gz: 782cf2ff2d3866a754950fd8edd3735a1bb1f425
3
+ metadata.gz: 658ef151a4948ba72f32f7dc5b51f5ddb3e6fb69
4
+ data.tar.gz: 8b7498f11685e3c5d1a4bbce70c732d8a62e8eb6
5
5
  SHA512:
6
- metadata.gz: c37c19e1f9edbad815b30d52f867d4344fd3b3da8a376e30236814a47ddd579b889459b1a702190fe5d1c1d2b99afdc3abef685f54a062616ea632872640a5e2
7
- data.tar.gz: c9c87e93dbd3b361a5632431ac8f83650749c7d1de090c3397299b403cda7e1a986767356d8795dc3cf037f9461227a8287dd3870df79a573031ee5928d31931
6
+ metadata.gz: 998d82e9c1a58efb14b5edd44deb4c85c62ced21cec664f29cffa3753f3742fc9e876fad96c9c3902fa2fc6a55d57622695bfe579acb6abb246695f8f7fe8b91
7
+ data.tar.gz: f1a5e4ee436e6081a93fd204cfd7ee434aeb3d6ada5e90853f42467cf6339ca8a6c4dbc0b8d4b335d4e1d75e211e965736f84010cc3ffb31e6c37b668fafce9c
data/CHANGELOG.md ADDED
@@ -0,0 +1,12 @@
1
+ ### 0.2.0 (unreleased)
2
+
3
+ - New features
4
+ - New schema types `$enum` and `$one_of` for specifying enumerations and values with multiple
5
+ possible schemas ([#5](https://github.com/ShiftForward/frise/pull/5)).
6
+ - Bug fixes
7
+ - Deal correctly with non-existing schema files in the load path
8
+ ([#4](https://github.com/ShiftForward/frise/pull/4)).
9
+
10
+ ### 0.1.0 (August 10, 2017)
11
+
12
+ Initial version.
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # Frise
2
2
  [![Build Status](https://travis-ci.org/ShiftForward/frise.svg?branch=master)](https://travis-ci.org/ShiftForward/frise)
3
3
  [![Coverage Status](https://coveralls.io/repos/github/ShiftForward/frise/badge.svg?branch=master)](https://coveralls.io/github/ShiftForward/frise?branch=master)
4
+ [![Gem Version](https://badge.fury.io/rb/frise.svg)](https://badge.fury.io/rb/frise)
4
5
 
5
6
  Frise is a library for loading configuration files as native Ruby structures. Besides reading and
6
7
  parsing the files themselves, it also:
@@ -56,12 +56,12 @@ module Frise
56
56
  end
57
57
 
58
58
  def merge_defaults(config, defaults_file, symbol_table = config)
59
- defaults = Parser.parse(defaults_file, symbol_table)
59
+ defaults = Parser.parse(defaults_file, symbol_table) || {}
60
60
  merge_defaults_obj(config, defaults)
61
61
  end
62
62
 
63
63
  def merge_defaults_at(config, at_path, defaults_file, symbol_table = config)
64
- defaults = Parser.parse(defaults_file, symbol_table)
64
+ defaults = Parser.parse(defaults_file, symbol_table) || {}
65
65
  merge_defaults_obj_at(config, at_path, defaults)
66
66
  end
67
67
  end
data/lib/frise/loader.rb CHANGED
@@ -16,7 +16,7 @@ module Frise
16
16
  end
17
17
 
18
18
  def load(config_file, exit_on_fail = true, symbol_table = {})
19
- config = Parser.parse(config_file, symbol_table)
19
+ config = Parser.parse(config_file, symbol_table) || {}
20
20
  config_name = File.basename(config_file)
21
21
 
22
22
  @pre_loaders.each do |pre_loader|
data/lib/frise/parser.rb CHANGED
@@ -7,7 +7,7 @@ module Frise
7
7
  module Parser
8
8
  class << self
9
9
  def parse(file, symbol_table = nil)
10
- return {} unless File.file? file
10
+ return nil unless File.file? file
11
11
  content = File.open(file).read
12
12
  content = Liquid::Template.parse(content).render symbol_table if symbol_table
13
13
  YAML.safe_load(content, [], [], true) || {}
@@ -32,7 +32,9 @@ module Frise
32
32
 
33
33
  def get_full_schema(schema)
34
34
  case schema
35
- when Hash then schema
35
+ when Hash then
36
+ default_type = schema[:enum] || schema[:one_of] ? 'Object' : 'Hash'
37
+ { type: default_type }.merge(schema)
36
38
  when Symbol then { type: 'Object', validate: schema }
37
39
  when Array then
38
40
  if schema.size == 1
@@ -87,6 +89,28 @@ module Frise
87
89
  true
88
90
  end
89
91
 
92
+ def validate_enum(full_schema, obj, path)
93
+ if full_schema[:enum] && !full_schema[:enum].include?(obj)
94
+ add_validation_error(path, "invalid value #{obj.inspect}. " \
95
+ "Accepted values are #{full_schema[:enum].map(&:inspect).join(', ')}")
96
+ return false
97
+ end
98
+ true
99
+ end
100
+
101
+ def validate_one_of(full_schema, obj, path)
102
+ if full_schema[:one_of]
103
+ full_schema[:one_of].each do |schema_opt|
104
+ opt_validator = Validator.new(@root, @validators)
105
+ opt_validator.validate_object(path, obj, schema_opt)
106
+ return true if opt_validator.errors.empty?
107
+ end
108
+ add_validation_error(path, "#{obj.inspect} does not match any of the possible schemas")
109
+ return false
110
+ end
111
+ true
112
+ end
113
+
90
114
  def validate_spec_keys(full_schema, obj, path, processed_keys)
91
115
  full_schema.each do |spec_key, spec_value|
92
116
  next if spec_key.is_a?(Symbol)
@@ -122,6 +146,8 @@ module Frise
122
146
  return unless validate_optional(full_schema, obj, path)
123
147
  return unless validate_type(full_schema, obj, path)
124
148
  return unless validate_custom(full_schema, obj, path)
149
+ return unless validate_enum(full_schema, obj, path)
150
+ return unless validate_one_of(full_schema, obj, path)
125
151
 
126
152
  processed_keys = Set.new
127
153
  return unless validate_spec_keys(full_schema, obj, path, processed_keys)
@@ -156,12 +182,12 @@ module Frise
156
182
  end
157
183
 
158
184
  def self.validate(config, schema_file, options = {})
159
- schema = parse_symbols(Parser.parse(schema_file))
185
+ schema = parse_symbols(Parser.parse(schema_file) || { allow_unknown_keys: true })
160
186
  validate_obj(config, schema, options)
161
187
  end
162
188
 
163
189
  def self.validate_at(config, at_path, schema_file, options = {})
164
- schema = parse_symbols(Parser.parse(schema_file))
190
+ schema = parse_symbols(Parser.parse(schema_file) || { allow_unknown_keys: true })
165
191
  at_path.reverse.each { |key| schema = { key => schema, :allow_unknown_keys => true } }
166
192
  validate_obj(config, schema, options)
167
193
  end
data/lib/frise/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Frise
2
- VERSION = '0.1.0'.freeze
2
+ VERSION = '0.2.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: frise
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ShiftForward
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-08-10 00:00:00.000000000 Z
11
+ date: 2017-08-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: liquid
@@ -119,6 +119,7 @@ files:
119
119
  - ".rspec"
120
120
  - ".rubocop.yml"
121
121
  - ".travis.yml"
122
+ - CHANGELOG.md
122
123
  - Gemfile
123
124
  - LICENSE.txt
124
125
  - README.md