Commit d594af0f by stantoncbradley

add documentation for big decimal, nested hashes, and array validation

add tests for required arrays and hashes
parent 5e75a26a
......@@ -47,7 +47,7 @@ end
### Parameter Types
By declaring parameter types, incoming parameters will automatically be transformed into an object of that type. For instance, if a param is `:boolean`, values of `'1'`, `'true'`, `'t'`, `'yes'`, and `'y'` will be automatically transformed into `true`.
By declaring parameter types, incoming parameters will automatically be transformed into an object of that type. For instance, if a param is `:boolean`, values of `'1'`, `'true'`, `'t'`, `'yes'`, and `'y'` will be automatically transformed into `true`. `BigDecimal` defaults to a precision of 14, but this can but changed by passing in the optional `precision:` argument. Any `$` and `,` are automatically stripped when converting to `BigDecimal`.
- `String`
- `Integer`
......@@ -56,6 +56,7 @@ By declaring parameter types, incoming parameters will automatically be transfor
- `Array` _("1,2,3,4,5")_
- `Hash` _("key1:value1,key2:value2")_
- `Date`, `Time`, & `DateTime`
- `BigDecimal` _("$1,000,000")_
### Validations
......@@ -85,6 +86,44 @@ param! :order, String, in: ["ASC", "DESC"], transform: :upcase, default: "ASC"
param! :offset, Integer, min: 0, transform: lambda {|n| n - (n % 10)}
```
### Nested Attributes
rails_param allows you to apply any of the above mentioned validations to attributes nested in hashes:
```ruby
param! :book, Hash do |b|
b.param! :title, String, blank: false
b.param! :price, BigDecimal, precision: 4, required: true
b.param! :author, Hash, required: true do |a|
a.param! :first_name, String
a.param! :last_name, String, blank: false
end
end
```
### Arrays
Validate every element of your array, including nested hashes and arrays:
```ruby
# primitive datatype syntax
param! :integer_array, Array do |array,index|
array.param! index, Integer, required: true
end
# complex array
param! :books_array, Array, required: true do |b|
b.param! :title, String, blank: false
b.param! :author, Hash, required: true, do |a|
a.param! :first_name, String
a.param! :last_name, String, required: true
end
b.param! :subjects, Array do |s,i|
s.param! i, String, blank: false
end
end
```
## Thank you
Many thanks to:
......
......@@ -181,7 +181,7 @@ describe RailsParam::Param do
end
describe 'validating nested hash' do
it 'typecasts nested attribtues' do
it 'typecasts nested attributes' do
allow(controller).to receive(:params).and_return({'foo' => {'bar' => 1, 'baz' => 2}})
controller.param! :foo, Hash do |p|
p.param! :bar, BigDecimal
......@@ -190,6 +190,35 @@ describe RailsParam::Param do
expect(controller.params['foo']['bar']).to be_instance_of BigDecimal
expect(controller.params['foo']['baz']).to be_instance_of Float
end
it 'does not raise exception if hash is not required but nested attributes are, and no hash is provided' do
allow(controller).to receive(:params).and_return(foo: nil)
controller.param! :foo, Hash do |p|
p.param! :bar, BigDecimal, required: true
p.param! :baz, Float, required: true
end
expect(controller.params['foo']).to be_nil
end
it 'raises exception if hash is required, nested attributes are not required, and no hash is provided' do
allow(controller).to receive(:params).and_return(foo: nil)
expect {
controller.param! :foo, Hash, required: true do |p|
p.param! :bar, BigDecimal
p.param! :baz, Float
end
}.to raise_exception
end
it 'raises exception if hash is not required but nested attributes are, and hash has missing attributes' do
allow(controller).to receive(:params).and_return({'foo' => {'bar' => 1, 'baz' => nil}})
expect {
controller.param! :foo, Hash do |p|
p.param! :bar, BigDecimal, required: true
p.param! :baz, Float, required: true
end
}.to raise_exception
end
end
describe 'validating arrays' do
......@@ -264,6 +293,25 @@ describe RailsParam::Param do
end
}.to raise_exception
end
it 'does not raise exception if array is not required but nested attributes are, and no array is provided' do
allow(controller).to receive(:params).and_return(foo: nil)
controller.param! :foo, Array do |p|
p.param! :bar, BigDecimal, required: true
p.param! :baz, Float, required: true
end
expect(controller.params['foo']).to be_nil
end
it 'raises exception if array is required, nested attributes are not required, and no array is provided' do
allow(controller).to receive(:params).and_return(foo: nil)
expect {
controller.param! :foo, Array, required: true do |p|
p.param! :bar, BigDecimal
p.param! :baz, Float
end
}.to raise_exception
end
end
describe "validation" do
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment