Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor #25

Merged
merged 32 commits into from
Jan 9, 2025
Merged
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
56be468
func builder
sergiobayona Jun 4, 2024
bd04bf7
fix spec
sergiobayona Jan 2, 2025
acd8912
allow newer gem versions
sergiobayona Jan 2, 2025
f21b3b5
move builder specs to their own folder
sergiobayona Jan 2, 2025
31da813
boolean specs
sergiobayona Jan 2, 2025
a55d19e
fix misspelling
sergiobayona Jan 2, 2025
1f07793
method rename
sergiobayona Jan 2, 2025
fe0dbdc
spec for schema composition
sergiobayona Jan 2, 2025
2dc3555
fix hash for nested schema
sergiobayona Jan 3, 2025
5d496c4
refactor optional/required properties
sergiobayona Jan 3, 2025
c82f3e3
initialize object builder with the properties
sergiobayona Jan 3, 2025
05809ad
renamed method for clarity
sergiobayona Jan 3, 2025
888aa9f
formatting
sergiobayona Jan 3, 2025
1c8d452
validate property name
sergiobayona Jan 3, 2025
1206e1d
custom type example
sergiobayona Jan 3, 2025
0321336
use Hash instead of :object
sergiobayona Jan 3, 2025
f1e06e1
remove json schemer dependency
sergiobayona Jan 3, 2025
3738896
refactor with comments
sergiobayona Jan 3, 2025
f9db7a4
not used
sergiobayona Jan 5, 2025
9cd6a78
pending specs
sergiobayona Jan 6, 2025
cd44c6c
fix misspelling
sergiobayona Jan 6, 2025
d713ce1
type validation
sergiobayona Jan 7, 2025
66b130f
remove validators for now
sergiobayona Jan 7, 2025
e1d6c26
do not use :object for inline block style objects
sergiobayona Jan 7, 2025
9631eef
fix typo
sergiobayona Jan 7, 2025
79ee8ac
specs for activemodel validations
sergiobayona Jan 7, 2025
d13bb03
prep for 1.0.0 release
sergiobayona Jan 7, 2025
7a63540
updated README
sergiobayona Jan 7, 2025
a890990
readme update
sergiobayona Jan 8, 2025
25fcc44
readme update
sergiobayona Jan 8, 2025
91da99c
changelog fix
sergiobayona Jan 9, 2025
c97b0ec
more readme updates
sergiobayona Jan 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
move builder specs to their own folder
sergiobayona committed Jan 2, 2025
commit f21b3b5e27fbcfc702208fd381a17a9be7338c86
File renamed without changes.
File renamed without changes.
168 changes: 168 additions & 0 deletions spec/easy_talk/builders/string_builder_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
require 'spec_helper'

RSpec.describe EasyTalk::Builders::StringBuilder do
describe '#build' do
context 'with basic configuration' do
it 'returns type string with no options' do
builder = described_class.new(:name)
expect(builder.build).to eq({ type: 'string' })
end

it 'includes title when provided' do
builder = described_class.new(:name, title: 'Full Name')
expect(builder.build).to eq({ type: 'string', title: 'Full Name' })
end

it 'includes description when provided' do
builder = described_class.new(:name, description: 'Person\'s full name')
expect(builder.build).to eq({ type: 'string', description: 'Person\'s full name' })
end
end

context 'with string-specific validations' do
it 'includes format constraint' do
builder = described_class.new(:email, format: 'email')
expect(builder.build).to eq({ type: 'string', format: 'email' })
end

it 'includes pattern constraint' do
builder = described_class.new(:zip, pattern: '^\d{5}(-\d{4})?$')
expect(builder.build).to eq({ type: 'string', pattern: '^\d{5}(-\d{4})?$' })
end

it 'includes minLength constraint' do
builder = described_class.new(:password, min_length: 8)
expect(builder.build).to eq({ type: 'string', minLength: 8 })
end

it 'includes maxLength constraint' do
builder = described_class.new(:username, max_length: 20)
expect(builder.build).to eq({ type: 'string', maxLength: 20 })
end

it 'includes enum constraint' do
builder = described_class.new(:status, enum: ['active', 'inactive', 'pending'])
expect(builder.build).to eq({ type: 'string', enum: ['active', 'inactive', 'pending'] })
end

it 'includes const constraint' do
builder = described_class.new(:type, const: 'user')
expect(builder.build).to eq({ type: 'string', const: 'user' })
end

it 'includes default value' do
builder = described_class.new(:role, default: 'member')
expect(builder.build).to eq({ type: 'string', default: 'member' })
end

it 'combines multiple constraints' do
builder = described_class.new(:password,
min_length: 8,
max_length: 32,
pattern: '^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$',
description: 'Must contain letters and numbers'
)

expect(builder.build).to eq({
type: 'string',
minLength: 8,
maxLength: 32,
pattern: '^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$',
description: 'Must contain letters and numbers'
})
end
end

context 'with invalid configurations' do
it 'raises ArgumentError for unknown constraints' do
expect {
described_class.new(:name, invalid_option: 'value').build
}.to raise_error(ArgumentError, /Unknown key: :invalid_option/)
end

it 'raises TypeError when format is not a string' do
expect {
described_class.new(:email, format: 123).build
}.to raise_error(TypeError)
end

it 'raises TypeError when pattern is not a string' do
expect {
described_class.new(:zip, pattern: 123).build
}.to raise_error(TypeError)
end

it 'raises TypeError when minLength is not an integer' do
expect {
described_class.new(:name, min_length: '8').build
}.to raise_error(TypeError)
end

it 'raises TypeError when maxLength is not an integer' do
expect {
described_class.new(:name, max_length: '20').build
}.to raise_error(TypeError)
end

it 'raises TypeError when enum contains non-string values' do
expect {
described_class.new(:status, enum: ['active', 123, 'pending']).build
}.to raise_error(TypeError)
end

it 'raises TypeError when const is not a string' do
expect {
described_class.new(:type, const: 123).build
}.to raise_error(TypeError)
end
end

context 'with nil values' do
it 'excludes constraints with nil values' do
builder = described_class.new(:name,
min_length: nil,
max_length: nil,
pattern: nil,
format: nil
)
expect(builder.build).to eq({ type: 'string' })
end
end

context 'with empty values on lenght validators' do
it 'raises a type error' do
builder = described_class.new(:name, min_length: '')
expect {
builder.build
}.to raise_error(TypeError)
end

it 'raises a type error' do
builder = described_class.new(:name, max_length: '')
expect {
builder.build
}.to raise_error(TypeError)
end
end

context "with empty values on pattern" do
it 'returns empty pattern' do
# this is invalid in json schema but there is not practical way to validate non empty strings.
builder = described_class.new(:name, pattern: '')
expect(builder.build).to eq({ type: 'string', pattern: '' })
end
end

context 'with optional flag' do
it 'includes optional flag when true' do
builder = described_class.new(:middle_name, optional: true)
expect(builder.build).to eq({ type: 'string', optional: true })
end

it 'includes optional flag when false' do
builder = described_class.new(:name, optional: false)
expect(builder.build).to eq({ type: 'string', optional: false })
end
end
end
end
File renamed without changes.
89 changes: 0 additions & 89 deletions spec/easy_talk/string_builder_spec.rb

This file was deleted.