ea-validation 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a379bb01041037ee305bf378b726d07b479126f0
4
+ data.tar.gz: 8d03132732b65747ca9d2eaeb8266dacb6afba1c
5
+ SHA512:
6
+ metadata.gz: 18aee70ad6eba15e46640b97222a268ce48415acae8249d98c7eebd94bd54ebfec3ecf00ff7959eddcb4cd09874bb4dd071836bf3b255443f0dffda9775872de
7
+ data.tar.gz: d91eb8c5fd2a43e4f209d4d75597e2460682109019eacefa5836d1e03000a954a55cad054f938f8f28cbd9c0ba3c89c286e258e647ac5942f4e1dbc2cc1bfd98
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # Overcommit configuration files
12
+ .overcommit*
13
+ .git-hooks/
14
+ rubocop/
15
+ .rubocop.yml
16
+ .htmlhintrc
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.4
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in ea-validation.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,8 @@
1
+ The Open Government Licence (OGL) Version 3
2
+
3
+ Copyright (c) 2014 Environment Agency
4
+
5
+ This source code is licensed under the Open Government Licence v3.0. To view this
6
+ licence, visit www.nationalarchives.gov.uk/doc/open-government-licence/version/3
7
+ or write to the Information Policy Team, The National Archives, Kew, Richmond,
8
+ Surrey, TW9 4DU.
data/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # Ea::Validation
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'ea-validation'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install ea-validation
18
+
19
+ ## Usage
20
+
21
+ With this gem installed, a number of active model based validators become
22
+ available:
23
+
24
+ ### Companies House Name
25
+
26
+ There are a number of restrictions as to what comprises a company name valid
27
+ for Companies House registration. This validator checks that the name complies
28
+ to these restrictions.
29
+
30
+ ```ruby
31
+ validates :company_name, "ea/validation/companies_house_name" => true
32
+ ```
33
+ ### Companies House number
34
+
35
+ As with company names, there are also restrictions to the form of a company
36
+ registration number. This validator checks that the company registration
37
+ number complies to these restrictions.
38
+
39
+ ```ruby
40
+ validates :number, "ea/validation/companies_house_number" => true
41
+ ```
42
+
43
+ ### Grid Reference
44
+
45
+ A grid reference should be of the form AA 12345 67890 (spaces optional).
46
+ The validator checks the format, and the the grid reference is valid.
47
+
48
+ ```ruby
49
+ validates :grid_reference, "ea/validation/grid_reference" => true
50
+ ```
51
+
52
+ ## Modifications
53
+
54
+ Options can be passed into validation by replacing `true` with an options
55
+ hash. For example:
56
+
57
+ ```ruby
58
+ validates(
59
+ :grid_reference,
60
+ "ea/validation/grid_reference" => {
61
+ message: "Custom",
62
+ allow_blank: true
63
+ }
64
+ )
65
+ ```
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ require "before_commit"
7
+ spec = Gem::Specification.find_by_name "before_commit"
8
+ load "#{spec.gem_dir}/lib/tasks/before_commit.rake"
9
+
10
+ task test: :spec
11
+
12
+ task :rubocop do
13
+ sh 'rubocop -D'
14
+ end
15
+
16
+ task default: [:rubocop, :test]
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ea/validation"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "ea/validation/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ea-validation"
8
+ spec.version = Ea::Validation::VERSION
9
+ spec.authors = ["Rob Nichols"]
10
+ spec.email = ["git@nicholshayes.co.uk"]
11
+ spec.license = "LICENSE"
12
+ spec.summary = "Package of validations commonly used in EA projects"
13
+ spec.description = "Package containing validations: ."
14
+ spec.homepage = "https://github.com/reggieb/ea-validation"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "os_map_ref"
22
+ spec.add_dependency "activemodel"
23
+ spec.add_development_dependency "bundler", "~> 1.11"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+ spec.add_development_dependency "before_commit"
27
+ spec.add_development_dependency "virtus"
28
+ spec.add_development_dependency "rubocop"
29
+ end
@@ -0,0 +1,34 @@
1
+ # Validates a company name.
2
+ # Allowed characters in legislation:
3
+ # http://www.legislation.gov.uk/uksi/2009/1085/schedule/1/made'
4
+ #
5
+ # rubocop:disable Style/AsciiComments
6
+ #
7
+ # Illegal characters - the company name cannot contain any of these characters: ^ | _ ~ ¬ or `
8
+ #
9
+ module Ea
10
+ module Validation
11
+ class CompaniesHouseNameValidator < ActiveModel::EachValidator
12
+
13
+ VALID_COMPANIES_HOUSE_NAME_REGEX = Regexp.new(/[\^|_~¬`]/).freeze
14
+
15
+ def validate_each(record, attribute, value)
16
+ if VALID_COMPANIES_HOUSE_NAME_REGEX.match(value)
17
+ record.errors.add(
18
+ attribute,
19
+ (options[:message] || I18n.t("ea.validation.errors.companies_house_name.#{attribute}.invalid"))
20
+ )
21
+ end
22
+ end
23
+
24
+ def self.name_max_length
25
+ 170
26
+ end
27
+
28
+ def self.disallowed_chars
29
+ @disallowed_chars ||= ["^", "|", "_", "~", "¬", "`"]
30
+ end
31
+ end
32
+ end
33
+ end
34
+ # rubocop:enable Style/AsciiComments
@@ -0,0 +1,30 @@
1
+ # Validates a company number.
2
+ # Valid numbers are:
3
+ # - two characters and 6 digits
4
+ # - 8 digits
5
+ #
6
+ # A company number consists of eight digits.
7
+ # The first of these digits is usually a zero and because of this, in most cases only
8
+ # the last seven numbers are usually required to be noted.
9
+ # The certificate of incorporation of a new company will show only
10
+ # the last seven numbers although the company number is actually eight digits and has a leading zero which is omitted.
11
+ #
12
+ module Ea
13
+ module Validation
14
+ class CompaniesHouseNumberValidator < ActiveModel::EachValidator
15
+
16
+ VALID_COMPANIES_HOUSE_REGISTRATION_NUMBER_REGEX = Regexp.new(/\A(\d{8,8}$)|([a-zA-Z]{2}\d{6}$)\z/i).freeze
17
+
18
+ def validate_each(record, attribute, value)
19
+ value.strip!
20
+ unless value =~ VALID_COMPANIES_HOUSE_REGISTRATION_NUMBER_REGEX
21
+ record.errors.add(
22
+ attribute,
23
+ (options[:message] || I18n.t("ea.validation.errors.companies_house_number.#{attribute}.invalid_html"))
24
+ )
25
+ end
26
+ end
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,55 @@
1
+ # Validate that a grid reference can be processed.
2
+ module Ea
3
+ module Validation
4
+ class GridReferenceValidator < ActiveModel::EachValidator
5
+ require "os_map_ref"
6
+
7
+ attr_reader :message, :allow_blank, :value
8
+ def initialize(options)
9
+ @message = options[:message]
10
+ @allow_blank = options[:allow_blank]
11
+ super options
12
+ end
13
+
14
+ def validate_each(record, attribute, value)
15
+ @value = value
16
+ return true if allow_blank && value.blank?
17
+ return true unless os_map_ref_detects_error? || invalid_pattern?
18
+ record.errors.add attribute, message
19
+ end
20
+
21
+ private
22
+
23
+ def os_map_ref_detects_error?
24
+ OsMapRef::Location.for(value).easting
25
+ false
26
+ rescue OsMapRef::Error => e
27
+ @message ||= e.message
28
+ end
29
+
30
+ # Note that OsMapRef will work with less stringent coordinates than are
31
+ # specified for this application - so need to add an additional check.
32
+ def invalid_pattern?
33
+ /\A#{grid_reference_pattern}\z/ !~ value.strip
34
+ end
35
+
36
+ def grid_reference_pattern
37
+ [
38
+ two_letters, optional_space, five_digits, optional_space, five_digits
39
+ ].join
40
+ end
41
+
42
+ def two_letters
43
+ "[A-Za-z]{2}"
44
+ end
45
+
46
+ def five_digits
47
+ '\d{5}'
48
+ end
49
+
50
+ def optional_space
51
+ '\s*'
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,5 @@
1
+ module Ea
2
+ module Validation
3
+ VERSION = "0.1.0".freeze
4
+ end
5
+ end
@@ -0,0 +1,10 @@
1
+ require "active_model"
2
+ require "ea/validation/version"
3
+ require "ea/validation/companies_house_name_validator"
4
+ require "ea/validation/companies_house_number_validator"
5
+ require "ea/validation/grid_reference_validator"
6
+
7
+ module Ea
8
+ module Validation
9
+ end
10
+ end
metadata ADDED
@@ -0,0 +1,171 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ea-validation
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Rob Nichols
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-07-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: os_map_ref
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activemodel
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.11'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.11'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: before_commit
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: virtus
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: 'Package containing validations: .'
126
+ email:
127
+ - git@nicholshayes.co.uk
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - ".rspec"
134
+ - ".travis.yml"
135
+ - Gemfile
136
+ - LICENSE
137
+ - README.md
138
+ - Rakefile
139
+ - bin/console
140
+ - bin/setup
141
+ - ea-validation.gemspec
142
+ - lib/ea/validation.rb
143
+ - lib/ea/validation/companies_house_name_validator.rb
144
+ - lib/ea/validation/companies_house_number_validator.rb
145
+ - lib/ea/validation/grid_reference_validator.rb
146
+ - lib/ea/validation/version.rb
147
+ homepage: https://github.com/reggieb/ea-validation
148
+ licenses:
149
+ - LICENSE
150
+ metadata: {}
151
+ post_install_message:
152
+ rdoc_options: []
153
+ require_paths:
154
+ - lib
155
+ required_ruby_version: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ required_rubygems_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ requirements: []
166
+ rubyforge_project:
167
+ rubygems_version: 2.4.8
168
+ signing_key:
169
+ specification_version: 4
170
+ summary: Package of validations commonly used in EA projects
171
+ test_files: []