Skip to content

Commit

Permalink
Document :dry_validation plugin [WIP]
Browse files Browse the repository at this point in the history
  • Loading branch information
pabloh committed Oct 27, 2017
1 parent 8ed1f7f commit ea24fba
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,45 @@ When activated it will enrich your operations with new instance and class method
Mind you, if you wish to activate a plugin for a number of operations you can activate it for all of them on the `Pathway::Operation` class, or you can create your own base operation and all its descendants will inherit the base class' plugins.

#### `DryValidation` plugin

This plugin provides integration with the [dry-validation](http://dry-rb.org/gems/dry-validation/) gem. I won't explain in detail how to use this library since is already extensively documented on its official website, but instead I'll assume certain knowledge of it, nonetheless, as you'll see in a moment, its API pretty self-explanatory.

`dry-validation` provides a very simple way to define form (or schema) objects to process and validate our input. The provided custom `:validate` step allows you to run your input though a form to check your data is valid before continuing, it's often the first step the operation runs.

When using this plugin we'll have to provide an already defined form to the step to use or we can also define one inline.
Let's see a few examples:

```ruby
```

As it can be seen at the code above, the form is first defined elsewhere, and the operation can be set up to use it by calling `form NuggetForm`, and use it within the process block with `step :validate`.

```ruby
```

On this other example we call the same `form` method but we pass a block instead, this block will be use as definition body for a form that will be stored internally. Then the validation step is defined exactly the same was as the other example.

One interesting nuance to keep in mind when using the inline block form is that, when using inheritance, if the parent operation already defines a form, the child operation will define a new form extending the one at the parent. This is very useful to share functionality among related operations on the same class hierarchy.

##### Form options

If you are familiar with `dry-validation` you probably know it provides a way to [inject options](http://dry-rb.org/gems/dry-validation/basics/working-with-schemas/#injecting-external-dependencies) before calling the form instance.

For those scenarios you can either use the `auto_wire_options: true` plugin argument, or specify how to map options from the execution state when calling `step`.
Lets see and example for each case:

```ruby
```

Here we see that the form needs a `:user` option so we tell the operation to grab the attribute with the same name from the execution state, so when the validation runs it already has the user available.

Mind you, this option is `false` by default, so be sure to activate it at `Pathway::Operation` if you'd rather use on all your operations.

```ruby
```

On the other hand, if for some reason the name of the form's option and attribute at the state don't match, we just pass `with: {...}`, indicating how to wire the values, when performing the validation at `process`. The `with:` parameter can always be specified and it allows you to override the default behavior even with auto-wiring activated.

#### `SimpleAuth` plugin

This very simple plugin adds a custom step called `:authorize`, that can be used to check for permissions and halt the operation with a `:forbidden` error when they aren't fulfilled.
Expand Down

0 comments on commit ea24fba

Please sign in to comment.