README.md in schemacop-2.3.2 vs README.md in schemacop-2.4.0

- old
+ new

@@ -122,16 +122,44 @@ We will see Type and Field lines in more detail below. ### `validate` vs `validate!` vs `valid?` The method `validate` will return a `Collector` object that contains all -validation errors (if any), whereas `validate!` will accumulate all violations -and finally throw an exception describing them. +validation errors (if any) as well as a deep copy of your data with applied +defaults and castings, whereas `validate!` will accumulate all violations +and finally throw an exception describing them or, if the validation was +successful, a deep-copy of your supplied data with defaults and castings +applied. For simply querying the validity of some data, use the methods `valid?` or `invalid?`. +Examples: + +```ruby +# validate! returns your modified data or throws a validation error +s = Schema.new do + req :foo, default: 42 +end +s.validate!({}) # => { foo: 42 } + +# validate returns a collector +s = Schema.new do + req :foo, default: 42 +end + +collector = s.validate({}) +collector.valid? # true +collector.data # => { foo: 42 } + +collector = s.validate({ foo: 'invalid' }) +collector.valid? # false +collector.data # => nil +collector.exceptions # => Validation error +``` + + ## Schemacop's DSL In this section, we will ignore [short forms](#short-forms) and explicitly write out everything. @@ -487,10 +515,14 @@ # ... end end ``` +Note that this does not allow you to specify any options for the hash itself. +You still need to specify `:hash` as a type if you want to pass any options to +the hash (i.e. a `default`). + ### Shortform for subtypes In case of nested arrays, you can group all Type Lines to a single one. Note that any options or block passed to the grouped Type Line will be given to @@ -545,10 +577,153 @@ This example accepts a hash with exactly one String key 'nutrition' with value of type Array with children of type Array with children of type Hash in which at least one of the Symbol keys `:food` and `:drink` (with any non-nil value type) is present. +## Defaults + +Starting from version 2.4.0, Schemacop allows you to define default values at +any point in your schema. If the validated data contains a nil value, it will be +substituted by the given default value. + +Note that Schemacop never modifies the data you pass to it. If you want to +benefit from Schemacop-applied defaults, you need to access the cloned, modified +data returned by `validate` or `validate!`. + +Applying defaults is done before validating the substructure and before any type +casting. The provided default will be validated same as user-supplied data, so +if your given default does not validate properly, a validation error is thrown. +Make sure your default values always match the underlying schema. + +Defaults can be specified at any point: + + +```ruby +# Basic usage +Schema.new do + type :string, default: 'Hello World' +end + +# The default given for the first type will match +Schema.new do + type :string, default: 'Hello World' # This will always be applied of no value is supplied + type :integer, default: 42 +end + +# You can also pass entire hashes or arrays to your defaults +Schema.new do + req :foo, :hash, default: { foo: :bar } do + req :foo, :symbol + end + req :bar, :array, :integer, default: [1, 2, 3] +end + +# Defaults must match the given schema. The following will fail. +Schema.new do + req :foo, default: { bar: :baz } do + req :foo + end +end +``` + +### Required data points + +Note that any *required* validation is done before applying the defaults. If you +specify a `req` field, it must always be given, no matter if you have specified +a default or not. Therefore, specifying `req` fields do not make sense in +conjunction with defaults, as the default is always ignored. + +## Type casting + +Starting from version 2.4.0, Schemacop allows you to specify type castings that +can alter the validated data. Consider the following: + +```ruby +s = Schema.new do + req :id, :integer, cast: [String] +end + +data = s.validate!(id: '42') +data # => { id: 42 } +``` + +Note that Schemacop never modifies the data you pass to it. If you want to +benefit from Schemacop-applied castings, you need to access the cloned, modified +data returned by `validate` or `validate!`. + +### Specifying type castings + +Type castings can be specified using two forms: Either as a hash or as an array. +While using an array only allows you to specify the supported source types to be +casted, using a hash allows you to specify custom casting logic as blocks. + +For hashes, the key must be a class and the value must be either `:default` for +using a built-in caster or a callable object (proc or lambda) that receives the +value and is supposed to cast it. If the value can't be casted, the proc must +fail with an exception. The exception message will then be contained in the +collected validation errors. + +Example: + +```ruby +Schema.new do + # Pass array to `cast`. This enables casting from String or Float to Integer + # using the built-in casters. + req: id_1, :integer, cast: [String, Float] + + # Pass hash to `cast`. This enables casting from Float to Integer using the + # built-in caster and from String to Integer using a custom callback. + req :id_2, :integer, cast: { Float => :default, String => proc { |s| Integer(s) } +end +``` + +### Built-in casters + +Schemacop comes with the following casters: + +- `String` to `Integer` and `Float` +- `Float` to `Integer` +- `Integer` to `Float` + +Note that all built-in casters are precise, so the string `foo` will fail with +an error if casted to an Integer. When casting float values and strings +containing float values to integers, the decimal places will be discarded +however. + +### Execution order + +The casting is done *before* the options `if` and `check` are evaluated. +Example: + +```ruby +s = Schema.new do + type :integer, if: proc { |i| i == 42 } # 1 + type :integer, check: proc { |i| i < 3 } # 2 + type :string +end + +s.validate!('42') # 1 will match +s.validate!('2') # 2 will match +s.validate!('234') # 3 will match +s.validate!(5) # Will fail, as nothing matches +``` + +### Caveats + +Casting only works with type definitions that only include one type. For +instance, the `Numeric` validator includes both `Integer` and `Float`, which +would made it unclear what to cast a string into: + +```ruby +# This does not work, as it is unclear whether to cast the String into an +# Integer or a Float. +type :number, cast: [String] +``` + +The same also applies to booleans, as they compound both `TrueClass` and +`FalseClass`. This may be tackled in future releases. + ## Exceptions Schemacop will throw one of the following checked exceptions: * {Schemacop::Exceptions::InvalidSchemaError} @@ -569,9 +744,22 @@ * Schemacop is not made for validating complex causalities (i.e. field `a` needs to be given only if field `b` is present). * Schemacop does not yet support string regex matching. + +## Development + +To run tests: + +* Check out the source + +* Run `bundle install` + +* Run `bundle exec rake test` to run all tests + +* Run `bundle exec rake test TEST=test/unit/some/file.rb` to run a single test + file ## Contributors Thanks to [Rubocop](https://github.com/bbatsov/rubocop) for great inspiration concerning their name and the structure of their README file. And special thanks