defra_ruby_validators 2.5.0 → 2.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/defra_ruby/validators/companies_house_number_validator.rb +2 -1
- data/lib/defra_ruby/validators/version.rb +1 -1
- metadata +22 -175
- data/spec/defra_ruby/validators/business_type_validator_spec.rb +0 -56
- data/spec/defra_ruby/validators/companies_house_number_validator_spec.rb +0 -124
- data/spec/defra_ruby/validators/companies_house_service_spec.rb +0 -98
- data/spec/defra_ruby/validators/configuration_spec.rb +0 -49
- data/spec/defra_ruby/validators/email_validator_spec.rb +0 -46
- data/spec/defra_ruby/validators/grid_reference_validator_spec.rb +0 -57
- data/spec/defra_ruby/validators/location_validator_spec.rb +0 -56
- data/spec/defra_ruby/validators/past_date_validator_spec.rb +0 -35
- data/spec/defra_ruby/validators/phone_number_validator_spec.rb +0 -59
- data/spec/defra_ruby/validators/position_validator_spec.rb +0 -47
- data/spec/defra_ruby/validators/token_validator_spec.rb +0 -45
- data/spec/defra_ruby/validators/true_false_validator_spec.rb +0 -37
- data/spec/defra_ruby/validators_spec.rb +0 -12
- data/spec/examples.txt +0 -189
- data/spec/spec_helper.rb +0 -87
- data/spec/support/defra_ruby_validators.rb +0 -16
- data/spec/support/helpers/text_generator.rb +0 -17
- data/spec/support/helpers/translator.rb +0 -15
- data/spec/support/i18n.rb +0 -8
- data/spec/support/pry.rb +0 -7
- data/spec/support/shared_examples/validators/characters_validator.rb +0 -29
- data/spec/support/shared_examples/validators/invalid_record.rb +0 -29
- data/spec/support/shared_examples/validators/length_validator.rb +0 -29
- data/spec/support/shared_examples/validators/presence_validator.rb +0 -29
- data/spec/support/shared_examples/validators/selection_validator.rb +0 -40
- data/spec/support/shared_examples/validators/valid_record.rb +0 -12
- data/spec/support/shared_examples/validators/validator.rb +0 -8
- data/spec/support/simplecov.rb +0 -17
data/spec/spec_helper.rb
DELETED
@@ -1,87 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "bundler/setup"
|
4
|
-
|
5
|
-
# Require and run our simplecov initializer as the very first thing we do.
|
6
|
-
# This is as per its docs https://github.com/colszowka/simplecov#getting-started
|
7
|
-
require "./spec/support/simplecov"
|
8
|
-
|
9
|
-
# Load env vars from a text file. This must be done before we load the other
|
10
|
-
# support files as some of them rely on the env vars being set.
|
11
|
-
require "dotenv/load"
|
12
|
-
|
13
|
-
# Requires supporting ruby files with custom matchers and macros, etc, in
|
14
|
-
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
|
15
|
-
# run as spec files by default. This means that files in spec/support that end
|
16
|
-
# in _spec.rb will both be required and run as specs, causing the specs to be
|
17
|
-
# run twice. It is recommended that you do not name files matching this glob to
|
18
|
-
# end with _spec.rb. You can configure this pattern with the --pattern
|
19
|
-
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
|
20
|
-
#
|
21
|
-
# We make an exception for simplecov because that will already have been
|
22
|
-
# required and run at the very top of spec_helper.rb
|
23
|
-
support_files = Dir["./spec/support/**/*.rb"].reject { |file| file == "./spec/support/simplecov.rb" }
|
24
|
-
support_files.each { |f| require f }
|
25
|
-
|
26
|
-
RSpec.configure do |config|
|
27
|
-
# rspec-expectations config goes here. You can use an alternate
|
28
|
-
# assertion/expectation library such as wrong or the stdlib/minitest
|
29
|
-
# assertions if you prefer.
|
30
|
-
config.expect_with :rspec do |expectations|
|
31
|
-
# This option will default to `true` in RSpec 4. It makes the `description`
|
32
|
-
# and `failure_message` of custom matchers include text for helper methods
|
33
|
-
# defined using `chain`, e.g.:
|
34
|
-
# be_bigger_than(2).and_smaller_than(4).description
|
35
|
-
# # => "be bigger than 2 and smaller than 4"
|
36
|
-
# ...rather than:
|
37
|
-
# # => "be bigger than 2"
|
38
|
-
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
39
|
-
end
|
40
|
-
|
41
|
-
# rspec-mocks config goes here. You can use an alternate test double
|
42
|
-
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
43
|
-
config.mock_with :rspec do |mocks|
|
44
|
-
# Prevents you from mocking or stubbing a method that does not exist on
|
45
|
-
# a real object. This is generally recommended, and will default to
|
46
|
-
# `true` in RSpec 4.
|
47
|
-
mocks.verify_partial_doubles = true
|
48
|
-
end
|
49
|
-
|
50
|
-
# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
|
51
|
-
# have no way to turn it off -- the option exists only for backwards
|
52
|
-
# compatibility in RSpec 3). It causes shared context metadata to be
|
53
|
-
# inherited by the metadata hash of host groups and examples, rather than
|
54
|
-
# triggering implicit auto-inclusion in groups with matching metadata.
|
55
|
-
config.shared_context_metadata_behavior = :apply_to_host_groups
|
56
|
-
|
57
|
-
# This allows you to limit a spec run to individual examples or groups
|
58
|
-
# you care about by tagging them with `:focus` metadata. When nothing
|
59
|
-
# is tagged with `:focus`, all examples get run. RSpec also provides
|
60
|
-
# aliases for `it`, `describe`, and `context` that include `:focus`
|
61
|
-
# metadata: `fit`, `fdescribe` and `fcontext`, respectively.
|
62
|
-
config.filter_run_when_matching :focus
|
63
|
-
|
64
|
-
# Allows RSpec to persist some state between runs in order to support
|
65
|
-
# the `--only-failures` and `--next-failure` CLI options. We recommend
|
66
|
-
# you configure your source control system to ignore this file.
|
67
|
-
config.example_status_persistence_file_path = "spec/examples.txt"
|
68
|
-
|
69
|
-
# Limits the available syntax to the non-monkey patched syntax that is
|
70
|
-
# recommended. For more details, see:
|
71
|
-
# - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
|
72
|
-
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
73
|
-
# - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
|
74
|
-
config.disable_monkey_patching!
|
75
|
-
|
76
|
-
# Run specs in random order to surface order dependencies. If you find an
|
77
|
-
# order dependency and want to debug it, you can fix the order by providing
|
78
|
-
# the seed, which is printed after each run.
|
79
|
-
# --seed 1234
|
80
|
-
config.order = :random
|
81
|
-
|
82
|
-
# Seed global randomization in this process using the `--seed` CLI option.
|
83
|
-
# Setting this allows you to use `--seed` to deterministically reproduce
|
84
|
-
# test failures related to randomization by passing the same `--seed` value
|
85
|
-
# as the one that triggered the failure.
|
86
|
-
Kernel.srand config.seed
|
87
|
-
end
|
@@ -1,16 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
# Need to require our actual code files. We don't just require everything in
|
4
|
-
# lib/defra_ruby because it contains the engine file which has a dependency on
|
5
|
-
# rails. We don't have that as a dependency of this project because it is
|
6
|
-
# a given this will be used in a rails project. So instead we require the
|
7
|
-
# validators file directly to load the content covered by our tests.
|
8
|
-
require "defra_ruby/validators"
|
9
|
-
|
10
|
-
DefraRuby::Validators.configure do |c|
|
11
|
-
def raise_missing_env_var(variable)
|
12
|
-
raise("Environment variable #{variable} has not been set")
|
13
|
-
end
|
14
|
-
|
15
|
-
c.companies_house_api_key = (ENV["COMPANIES_HOUSE_API_KEY"] || raise_missing_env_var("COMPANIES_HOUSE_API_KEY"))
|
16
|
-
end
|
@@ -1,17 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Helpers
|
4
|
-
module TextGenerator
|
5
|
-
def self.random_string(length)
|
6
|
-
random_string_from_sequence([("a".."z"), ("A".."Z")].map(&:to_a).flatten, length)
|
7
|
-
end
|
8
|
-
|
9
|
-
def self.random_number_string(length)
|
10
|
-
random_string_from_sequence(("0".."9").to_a, length)
|
11
|
-
end
|
12
|
-
|
13
|
-
def self.random_string_from_sequence(sequence, length)
|
14
|
-
(0...length).map { sequence[rand(sequence.length)] }.join
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
@@ -1,15 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Helpers
|
4
|
-
module Translator
|
5
|
-
def self.error_message(klass, error)
|
6
|
-
class_name = klass_name(klass)
|
7
|
-
|
8
|
-
I18n.t("defra_ruby.validators.#{class_name}.#{error}")
|
9
|
-
end
|
10
|
-
|
11
|
-
def self.klass_name(klass)
|
12
|
-
klass.name.split("::").last
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
data/spec/support/i18n.rb
DELETED
@@ -1,8 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
# Support using the locale files within our tests (else we'd rely on loading the
|
4
|
-
# gem in a rails app to confirm these are being pulled through correctly.)
|
5
|
-
require "i18n"
|
6
|
-
|
7
|
-
I18n.load_path << Dir["#{File.expand_path('config/locales')}/**/*.yml"]
|
8
|
-
I18n.default_locale = :en
|
data/spec/support/pry.rb
DELETED
@@ -1,7 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
# Support debugging in the tests. Add `binding.pry` wherever you want execution
|
4
|
-
# to stop and the debugger to kick in.
|
5
|
-
# Details on the debugging commands can be found here
|
6
|
-
# https://github.com/deivid-rodriguez/pry-byebug#commands
|
7
|
-
require "pry-byebug"
|
@@ -1,29 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.shared_examples "a characters validator" do |validator, validatable_class, attribute, values|
|
4
|
-
it "includes CanValidateCharacters" do
|
5
|
-
included_modules = described_class.ancestors.select { |ancestor| ancestor.instance_of?(Module) }
|
6
|
-
|
7
|
-
expect(included_modules)
|
8
|
-
.to include(DefraRuby::Validators::CanValidateCharacters)
|
9
|
-
end
|
10
|
-
|
11
|
-
describe "#validate_each" do
|
12
|
-
context "when the #{attribute} is valid" do
|
13
|
-
it_behaves_like "a valid record", validatable_class.new(values[:valid])
|
14
|
-
end
|
15
|
-
|
16
|
-
context "when the #{attribute} is not valid" do
|
17
|
-
context "because the #{attribute} is not correctly formatted" do
|
18
|
-
validatable = validatable_class.new(values[:invalid])
|
19
|
-
error_message = Helpers::Translator.error_message(validator, :invalid_format)
|
20
|
-
|
21
|
-
it_behaves_like "an invalid record",
|
22
|
-
validatable: validatable,
|
23
|
-
attribute: attribute,
|
24
|
-
error: :invalid_format,
|
25
|
-
error_message: error_message
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
@@ -1,29 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.shared_examples "an invalid record" do |validatable:, attribute:, error:, error_message:|
|
4
|
-
it "confirms the object is invalid" do
|
5
|
-
expect(validatable).to_not be_valid
|
6
|
-
end
|
7
|
-
|
8
|
-
it "adds a single validation error to the record" do
|
9
|
-
validatable.valid?
|
10
|
-
expect(validatable.errors[attribute].count).to eq(1)
|
11
|
-
end
|
12
|
-
|
13
|
-
it "adds an appropriate validation error" do
|
14
|
-
validatable.valid?
|
15
|
-
expect(error_message).to_not include("translation missing:")
|
16
|
-
expect(validatable.errors[attribute]).to eq([error_message])
|
17
|
-
end
|
18
|
-
|
19
|
-
context "when there are custom error messages" do
|
20
|
-
let(:custom_message) { "something is wrong (in a customised way)" }
|
21
|
-
let(:messages) { { error => custom_message } }
|
22
|
-
before { allow_any_instance_of(DefraRuby::Validators::BaseValidator).to receive(:options).and_return(messages: messages) }
|
23
|
-
|
24
|
-
it "uses the custom message instead of the default" do
|
25
|
-
validatable.valid?
|
26
|
-
expect(validatable.errors[attribute]).to eq([custom_message])
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
@@ -1,29 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.shared_examples "a length validator" do |validator, validatable_class, attribute, values|
|
4
|
-
it "includes CanValidateLength" do
|
5
|
-
included_modules = described_class.ancestors.select { |ancestor| ancestor.instance_of?(Module) }
|
6
|
-
|
7
|
-
expect(included_modules)
|
8
|
-
.to include(DefraRuby::Validators::CanValidateLength)
|
9
|
-
end
|
10
|
-
|
11
|
-
describe "#validate_each" do
|
12
|
-
context "when the #{attribute} is valid" do
|
13
|
-
it_behaves_like "a valid record", validatable_class.new(values[:valid])
|
14
|
-
end
|
15
|
-
|
16
|
-
context "when the #{attribute} is not valid" do
|
17
|
-
context "because the #{attribute} is too long" do
|
18
|
-
validatable = validatable_class.new(values[:invalid])
|
19
|
-
error_message = Helpers::Translator.error_message(validator, :too_long)
|
20
|
-
|
21
|
-
it_behaves_like "an invalid record",
|
22
|
-
validatable: validatable,
|
23
|
-
attribute: attribute,
|
24
|
-
error: :too_long,
|
25
|
-
error_message: error_message
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
@@ -1,29 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.shared_examples "a presence validator" do |validator, validatable_class, attribute, values|
|
4
|
-
it "includes CanValidatePresence" do
|
5
|
-
included_modules = described_class.ancestors.select { |ancestor| ancestor.instance_of?(Module) }
|
6
|
-
|
7
|
-
expect(included_modules)
|
8
|
-
.to include(DefraRuby::Validators::CanValidatePresence)
|
9
|
-
end
|
10
|
-
|
11
|
-
describe "#validate_each" do
|
12
|
-
context "when the #{attribute} is valid" do
|
13
|
-
it_behaves_like "a valid record", validatable_class.new(values[:valid])
|
14
|
-
end
|
15
|
-
|
16
|
-
context "when the #{attribute} is not valid" do
|
17
|
-
context "because the #{attribute} is not present" do
|
18
|
-
validatable = validatable_class.new
|
19
|
-
error_message = Helpers::Translator.error_message(validator, :blank)
|
20
|
-
|
21
|
-
it_behaves_like "an invalid record",
|
22
|
-
validatable: validatable,
|
23
|
-
attribute: attribute,
|
24
|
-
error: :blank,
|
25
|
-
error_message: error_message
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
@@ -1,40 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.shared_examples "a selection validator" do |validator, validatable_class, attribute, values|
|
4
|
-
it "includes CanValidateSelection" do
|
5
|
-
included_modules = described_class.ancestors.select { |ancestor| ancestor.instance_of?(Module) }
|
6
|
-
|
7
|
-
expect(included_modules)
|
8
|
-
.to include(DefraRuby::Validators::CanValidateSelection)
|
9
|
-
end
|
10
|
-
|
11
|
-
describe "#validate_each" do
|
12
|
-
context "when the #{attribute} is valid" do
|
13
|
-
it_behaves_like "a valid record", validatable_class.new(values[:valid])
|
14
|
-
end
|
15
|
-
|
16
|
-
context "when the #{attribute} is not valid" do
|
17
|
-
context "because the #{attribute} is not present" do
|
18
|
-
validatable = validatable_class.new
|
19
|
-
error_message = Helpers::Translator.error_message(validator, :inclusion)
|
20
|
-
|
21
|
-
it_behaves_like "an invalid record",
|
22
|
-
validatable: validatable,
|
23
|
-
attribute: attribute,
|
24
|
-
error: :inclusion,
|
25
|
-
error_message: error_message
|
26
|
-
end
|
27
|
-
|
28
|
-
context "because the #{attribute} is not from an approved list" do
|
29
|
-
validatable = validatable_class.new(values[:invalid])
|
30
|
-
error_message = Helpers::Translator.error_message(validator, :inclusion)
|
31
|
-
|
32
|
-
it_behaves_like "an invalid record",
|
33
|
-
validatable: validatable,
|
34
|
-
attribute: attribute,
|
35
|
-
error: :inclusion,
|
36
|
-
error_message: error_message
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
@@ -1,12 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.shared_examples "a valid record" do |validatable|
|
4
|
-
it "confirms the object is valid" do
|
5
|
-
expect(validatable).to be_valid
|
6
|
-
end
|
7
|
-
|
8
|
-
it "the errors are empty" do
|
9
|
-
validatable.valid?
|
10
|
-
expect(validatable.errors).to be_empty
|
11
|
-
end
|
12
|
-
end
|
data/spec/support/simplecov.rb
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "simplecov"
|
4
|
-
|
5
|
-
# We start it with the rails param to ensure it includes coverage for all code
|
6
|
-
# started by the rails app, and not just the files touched by our unit tests.
|
7
|
-
# This gives us the most accurate assessment of our unit test coverage
|
8
|
-
# https://github.com/colszowka/simplecov#getting-started
|
9
|
-
SimpleCov.start do
|
10
|
-
# We filter the spec folder, mainly to ensure that any dummy apps don't get
|
11
|
-
# included in the coverage report. However our intent is that nothing in the
|
12
|
-
# spec folder should be included
|
13
|
-
add_filter "/spec/"
|
14
|
-
# The version file is simply just that, so we do not feel the need to ensure
|
15
|
-
# we have a test for it
|
16
|
-
add_filter "lib/defra_ruby/validators/version"
|
17
|
-
end
|