Skip to content

Instantly share code, notes, and snippets.

@solnic
Last active September 21, 2018 09:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save solnic/cd4a6e9d65b668f172f6bcb16cf9edcb to your computer and use it in GitHub Desktop.
Save solnic/cd4a6e9d65b668f172f6bcb16cf9edcb to your computer and use it in GitHub Desktop.

what other choices than required are there? is there a full list somewhere?

There's only required and optional. required means a key must be present in the input, and optional which means that a key is not required. More here https://dry-rb.org/gems/dry-validation/optional-keys-and-values/

what does filled do? what are the alternatives? what happens if I don't call it?

It means "a non-empty value is expected". If you don't call it, no rules will be created for a value under given key (which actually is a known "gotcha" that I want to address). You can use other predicates depending on the value type. ie required(:tags).value(:array?, min_size?: 1)

what is the difference between required without filled and optional?

Currently, as I mentioned, required without anything else will be a no/op.

what are the values yielded in custom rules? what's true??

rule DSL is still an abstract DSL, the blocks aren't really what's executed at run-time. This turned out to be a very bad idea, so it's going away. Values that are yielded are rule builder objects, calling predicate methods (ending with '?') on them results in rule definitions that you can compose using logic operators, like foo.true? | bar.false? means foo must be true or bar must be false.

what's the difference between rules and validation blocks? when should I choose one over the other?

validation blocks are more obvious, as in, it's plain ruby inside, whereas rule blocks are abstract DSLs, which, despite some advantages like concise syntax in some cases, are just too confusing for people. In dry-v 1.0.0 we will only have rule blocks that use plain Ruby, they will operate on pattern-matched values that have been already checked by an associated schema.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment