Skip to content

Commit

Permalink
Add format mailbox to str node for Schemacop3 schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian-Hirt committed Dec 13, 2023
1 parent 178f86f commit e5e436a
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change log

## 3.0.27 (2023-12-13)

* Add format `mailbox` to `str` node for Schemacop3 schemas

## 3.0.26 (2023-11-16)

* Lock dependency `ruby2_keywords` to the more open `>= 0.0.4`
Expand Down
5 changes: 5 additions & 0 deletions README_V3.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,11 @@ transformed into various types.
Validates for a valid email address. There is no casting involved since email
addresses do not have their own ruby type.

* `mailbox`
Validates for a valid mailbox, which is defined as a valid email enclosed in
brackets (`< >`), with an optional name before the email address. There is no
casting involved.

* `boolean`
The string must be either `true`, `false`, `0` or `1`. This value will be
casted to Ruby's `TrueClass` or `FalseClass`. Please note that the strings
Expand Down
6 changes: 6 additions & 0 deletions lib/schemacop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ def self.register_string_formatter(name, pattern:, handler:)
handler: ->(value) { value }
)

register_string_formatter(
:mailbox,
pattern: %r{^.*\s?<[a-zA-Z0-9.!\#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*>$},
handler: ->(value) { value }
)

register_string_formatter(
:boolean,
pattern: /^(true|false|0|1)$/i,
Expand Down
44 changes: 44 additions & 0 deletions test/unit/schemacop/v3/string_node_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,50 @@ def test_format_email
assert_cast('[email protected]', '[email protected]')
end

def test_format_mailbox
schema :string, format: :mailbox

assert_json(type: :string, format: :mailbox)

assert_validation '[email protected]' do
error '/', 'String does not match format "mailbox".'
end

assert_validation '[email protected]' do
error '/', 'String does not match format "mailbox".'
end

assert_validation '[email protected]' do
error '/', 'String does not match format "mailbox".'
end

assert_validation 'someemail' do
error '/', 'String does not match format "mailbox".'
end

assert_validation 'john [email protected]' do
error '/', 'String does not match format "mailbox".'
end

assert_validation '@[email protected]' do
error '/', 'String does not match format "mailbox".'
end

assert_validation 'John Doe <[email protected]>'
assert_validation 'John Doe <[email protected]>'
assert_validation 'John Doe <[email protected]>'

assert_validation 'John <[email protected]>'
assert_validation 'John Doe 123 <[email protected]>'
assert_validation 'John_Doe <[email protected]>'
assert_validation 'John-Doe ÖÄ <[email protected]>'
assert_validation '"John Doe" <[email protected]>'
assert_validation '<[email protected]>'

assert_cast(nil, nil)
assert_cast('John Doe <[email protected]>', 'John Doe <[email protected]>')
end

def test_format_boolean
schema :string, format: :boolean

Expand Down

0 comments on commit e5e436a

Please sign in to comment.