paramnoia
Version, currently 0.1.11 version
- 0.1.1latestSep 14, 2020
github.com/repomaa/paramnoia
Params for the paranoid
3 stars
0 dependents
License: MIT
Installation
# Add this to your shard.yml
dependencies:
paramnoia:
github: repomaa/paramnoia
version: ~> 0.1.1Then run:
shards installshard.yml
- Crystal
0.35.1- License
- MIT
- Author
- Joakim Reinert
- Target
specfrom spec/paramnoia_spec.cr
Dependencies
Development Dependencies
- ameba*github: veelenga/amebadev
- paladin*github: jreinert/paladindev
README
Paramnoia - Params for the paranoid
Paramnoia is a library that allows for parameter coercion into type-safe crystal structures.
Installation
Add this to your application's shard.yml:
dependencies:
paramnoia:
github: jreinert/paramnoia
Usage
tldr; Works exactly like JSON::Serializable. Hint: run make in the cloned
project, start up crystal play and play around! The following
examples are runnable in the Workbook tab!
Basic flat params
require "paramnoia"
struct Params
include Paramnoia::Params
getter foo : String
getter bar : Int32
end
pp Params.from_urlencoded("foo=hello&bar=3")
Arrays
require "paramnoia"
struct Params
include Paramnoia::Params
getter foo : Array(Float64)
end
pp Params.from_urlencoded("foo[]=1&foo[]=2.3&foo[]=-3")
Booleans
require "paramnoia"
struct Params
include Paramnoia::Params
getter foo : Bool
getter bar : Bool
getter baz : Bool
getter foobar : Bool
getter foobaz : Bool
end
pp Params.from_urlencoded("foo=1&bar=2&baz=0&foobar=FALSE&foobaz=false")
Enums
require "paramnoia"
enum Foobar
Foo
Bar
Baz
end
struct Params
include Paramnoia::Params
getter foo : Foobar
end
pp Params.from_urlencoded("foo=Bar")
Defaults and nilable
require "paramnoia"
struct Params
include Paramnoia::Params
getter foo : String?
getter bar : String = "bar"
getter baz : Int32
end
pp Params.from_urlencoded("baz=3")
Nested
require "paramnoia"
struct Nested
include Paramnoia::Params
getter bar : String
getter baz : Array(Int32)
end
struct Params
include Paramnoia::Params
getter foo : Nested
getter bar : String
end
pp Params.from_urlencoded("foo[bar]=nested%20bar&foo[baz][]=1&foo[baz][]=2&bar=bar")
Key renaming and converters
require "paramnoia"
module BazConverter
# You will be passed all matching param values from the HTTP::Params
def self.from_params(values : Array(String))
values.last.split(",").map { |i| Int32.new(i) }
end
end
struct Params
include Paramnoia::Params
@[Paramnoia::Params::Field(key: "fooBar")]
getter foo_bar : String
@[Paramnoia::Params::Field(converter: BazConverter)]
getter baz : Array(Int32)
end
pp Params.from_urlencoded("fooBar=foo&baz=1,2,3,4")
Strict parsing
require "paramnoia"
@[Paramnoia::Settings(strict: true)]
struct Params
include Paramnoia::Params
getter foo : String
end
begin
Params.from_urlencoded("foo=bar&baz=1")
rescue ex : Exception
puts ex.message
end
From JSON
require "paramnoia"
struct Params
include Paramnoia::Params
getter foo : String
getter bar : Int32
end
pp Params.from_json(%|{"foo":"foo","bar":1}|)
Development
TODO
- [x] Parsing from HTTP::Params
- [x] Parsing from JSON
- [ ] Parsing from Form Data
- [ ] Parsing from Path Params
- [ ] Specs
- [ ] CI
- [ ] Refactoring/splitting up macro code
Contributing
- Fork it (https://github.com/jreinert/paramnoia/fork)
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request
Contributors
- jreinert Joakim Reinert - creator, maintainer
Documentation
Built from the current release. The first visit to a release nobody has asked for starts its build.
Links
This release
- Version
0.1.1- Tagged
- Sep 14, 2020
- Commit
54ae966e4100- Crystal
0.35.1- Indexed
- yes
Dependents
No indexed shard depends on this one yet.
Repository
github.com/repomaa/paramnoia
Metadata
- Created
- Aug 12, 2026
- Updated
- Aug 14, 2026
- Synced
- Aug 14, 2026
- Versions
- 1