dotenv_validator 1.1.0 → 1.3.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
  SHA256:
3
- metadata.gz: 6ee3d73f77ffe3a5d1a050afcd957d1cda68522963de3c4196f0e8fb22f0b9f3
4
- data.tar.gz: b957b41bde61f0dd24e3ab4a896ea4bdd063804ede8417271ca40d3e0fa101b7
3
+ metadata.gz: '03040841959fe965027d57672d55726640199bb217b52a169c40b3bf465c7d69'
4
+ data.tar.gz: 80f1543f7719e2f3758bcf69b6294469c22033a56e96f03358d1627350e266b7
5
5
  SHA512:
6
- metadata.gz: 9893692a6ecd009c7bc728cec6c0cd0f66f4d07dde10c32b5dffcb817675af3f5cba3cee750bf3b331f88e05d97db2ed4b417b6755d49e3e6ff50e36aa7e241f
7
- data.tar.gz: 48821a901dd6ffc6d86081d0a72cc652a1d83659eb663ac71165c307eeff88b0af4bfcb76a1699dd9f8ddb2859843d15b26595f3ae8f6d94398df50657c595cd
6
+ metadata.gz: aee0d70d03f541d3d30171f1eafc5481df165a702b60bb0426b21db52af3c561100213765b0df20f80aeaa8b866b1fb47019b0bf5600aa8d181c825a64df63cf
7
+ data.tar.gz: a0e9a2bedb35d6cd82b5ebb3872544b009d849fa6de34044e060aeadbea12f77cf53a98b6a8ee7e95ef865bf7ecd40ab9e6583e7db99e6065434278090b67f59
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DotenvValidator # :nodoc: all
4
+ class SampleFileNotFoundError < StandardError
5
+ end
6
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DotenvValidator
4
- VERSION = '1.1.0'
4
+ VERSION = "1.3.0"
5
5
  end
@@ -1,5 +1,8 @@
1
- require 'fast_blank'
2
- require 'pathname'
1
+ # frozen_string_literal: true
2
+
3
+ require "fast_blank"
4
+ require "pathname"
5
+ require "dotenv_validator/errors"
3
6
 
4
7
  # Knows how to check the environment variables and compares it to .env.sample
5
8
  # and the comments in every line of your .env.sample file.
@@ -15,8 +18,8 @@ module DotenvValidator
15
18
  invalid_format = []
16
19
 
17
20
  open_sample_file.each do |line|
18
- variable, config = line.split(' #')
19
- variable_name, _sample = variable.split('=')
21
+ variable, config = line.split(" #")
22
+ variable_name, _sample = variable.split("=")
20
23
  value = ENV[variable_name]
21
24
 
22
25
  if value.nil? || value.blank?
@@ -26,18 +29,22 @@ module DotenvValidator
26
29
 
27
30
  next unless config =~ /format=(.*)/
28
31
 
32
+ format = Regexp.last_match(1)
33
+
29
34
  valid =
30
- case Regexp.last_match(1)
31
- when 'int', 'integer' then integer?(value)
32
- when 'float' then float?(value)
33
- when 'str', 'string' then true
34
- when 'email' then email?(value)
35
- when 'url' then url?(value)
35
+ case format
36
+ when "int", "integer", "Integer" then integer?(value)
37
+ when "float", "Float" then float?(value)
38
+ when "str", "string", "String" then true
39
+ when "email" then email?(value)
40
+ when "url" then url?(value)
41
+ when "bool", "boolean", "Boolean" then boolean?(value)
42
+ when "uuid", "UUID" then uuid?(value)
36
43
  else
37
44
  value.match?(Regexp.new(Regexp.last_match(1)))
38
45
  end
39
46
 
40
- invalid_format << variable_name unless valid
47
+ invalid_format << {name: variable_name, value: value, format: format} unless valid
41
48
  end
42
49
 
43
50
  [missing_variables, invalid_format]
@@ -51,12 +58,13 @@ module DotenvValidator
51
58
 
52
59
  missing_variables, invalid_format = analyze_variables
53
60
  if missing_variables.any?
54
- puts("WARNING - Missing environment variables: #{missing_variables.join(', ')}")
61
+ puts("WARNING - Missing environment variables: #{missing_variables.join(", ")}")
55
62
  result = false
56
63
  end
57
64
 
58
65
  if invalid_format.any?
59
- puts("WARNING - Environment variables with invalid format: #{invalid_format.join(', ')}")
66
+ puts "WARNING - Environment variables with invalid format:"
67
+ puts invalid_format_list(invalid_format)
60
68
  result = false
61
69
  end
62
70
 
@@ -69,53 +77,72 @@ module DotenvValidator
69
77
  def self.check!
70
78
  missing_variables, invalid_format = analyze_variables
71
79
 
72
- raise("Missing environment variables: #{missing_variables.join(', ')}") if missing_variables.any?
80
+ raise("Missing environment variables: #{missing_variables.join(", ")}") if missing_variables.any?
73
81
 
74
- raise("Environment variables with invalid format: #{invalid_format.join(', ')}") if invalid_format.any?
82
+ raise("Environment variables with invalid format:\n#{invalid_format_list(invalid_format)}") if invalid_format.any?
75
83
  end
76
84
 
77
- # It checks the value to check if it is a float or not.
85
+ # It checks if the value is float or not.
78
86
  #
79
87
  # @param [String] A string
80
88
  # @return [Boolean] True if it is a float value. False otherwise.
81
89
  def self.float?(string)
82
90
  true if Float(string)
83
- rescue StandardError
91
+ rescue
84
92
  false
85
93
  end
86
94
 
87
- # It checks the value to check if it is an integer or not.
95
+ # It checks if the value is an integer or not.
88
96
  #
89
97
  # @param [String] A string
90
98
  # @return [Boolean] True if it is an integer value. False otherwise.
91
99
  def self.integer?(string)
92
100
  true if Integer(string)
93
- rescue StandardError
101
+ rescue
94
102
  false
95
103
  end
96
104
 
97
- # It checks the value to check if it is an email or not.
105
+ # It checks if the value is an email or not.
98
106
  #
99
107
  # @param [String] A string
100
108
  # @return [Boolean] True if it is an email value. False otherwise.
101
109
  def self.email?(string)
102
- string.match?(/[\w@]+@[\w@]+\.[\w@]+/)
110
+ string.match?(URI::MailTo::EMAIL_REGEXP)
103
111
  end
104
112
 
105
- # It checks the value to check if it is a URL or not.
113
+ # It checks if the value is a URL or not.
106
114
  #
107
115
  # @param [String] A string
108
116
  # @return [Boolean] True if it is an URL value. False otherwise.
109
117
  def self.url?(string)
110
- string.match?(%r{https?://.+})
118
+ string.match?(/\A#{URI::DEFAULT_PARSER.make_regexp(%w[http https])}\z/)
119
+ end
120
+
121
+ # It checks if the value is a boolean or not.
122
+ #
123
+ # @param [String] A string
124
+ # @return [Boolean] True if it is a boolean value. False otherwise.
125
+ def self.boolean?(string)
126
+ string.match?(/(true|false)/)
127
+ end
128
+
129
+ # It checks if the value is a uuid or not.
130
+ #
131
+ # @param [String] A string
132
+ # @return [Boolean] True if it is a UUID value. False otherwise.
133
+ def self.uuid?(string)
134
+ string.match?(/\A[\da-f]{32}\z/i) ||
135
+ string.match?(/\A[\da-f]{8}-([\da-f]{4}-){3}[\da-f]{12}\z/i)
111
136
  end
112
137
 
113
138
  def self.open_sample_file
114
139
  File.open(sample_file)
140
+ rescue Errno::ENOENT
141
+ raise DotenvValidator::SampleFileNotFoundError, "#{sample_file} was not found!"
115
142
  end
116
143
 
117
144
  def self.sample_file
118
- File.join(root, '.env.sample')
145
+ File.join(root, ".env.sample")
119
146
  end
120
147
 
121
148
  # Internal: `Rails.root` is nil in Rails 4.1 before the application is
@@ -132,4 +159,10 @@ module DotenvValidator
132
159
  root_or_pwd
133
160
  end
134
161
  end
162
+
163
+ def self.invalid_format_list(invalid_format)
164
+ invalid_format.map do |var|
165
+ %(- #{var[:name]}: expected "#{var[:value]}" to match "#{var[:format]}" format)
166
+ end.join("\n")
167
+ end
135
168
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dotenv_validator
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ariel Juodziukynas
@@ -12,7 +12,7 @@ authors:
12
12
  autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
- date: 2021-09-14 00:00:00.000000000 Z
15
+ date: 2023-01-04 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: fast_blank
@@ -176,6 +176,7 @@ extensions: []
176
176
  extra_rdoc_files: []
177
177
  files:
178
178
  - lib/dotenv_validator.rb
179
+ - lib/dotenv_validator/errors.rb
179
180
  - lib/dotenv_validator/version.rb
180
181
  homepage: https://github.com/fastruby/dotenv_validator
181
182
  licenses:
@@ -196,7 +197,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
196
197
  - !ruby/object:Gem::Version
197
198
  version: '0'
198
199
  requirements: []
199
- rubygems_version: 3.1.2
200
+ rubygems_version: 3.3.7
200
201
  signing_key:
201
202
  specification_version: 4
202
203
  summary: Checks required env variables and its format using .env.sample