Conversation
cnavas88
commented
Jul 18, 2019
- Adding "included" in String validator.
- Validate that a string is included in a list.
- Testing Included function.
- Documentation included function.
dmlittle
reviewed
Jul 23, 2019
| * `:trim` - Removes additional whitespace at the front and end of a string. | ||
| Allowed values are `true` and `false`. The default is `false`. | ||
|
|
||
| * `:included` - Specifies a option list. |
There was a problem hiding this comment.
I like this feature but I think I'd prefer having the name option be a list called :valid since it specifies the list of valid strings. This would make the using this feature like this:
schema = %{
"casing" => %Litmus.Type.String{
valid: ["lowercase", "uppercase"]
}
}
Contributor
There was a problem hiding this comment.
@dmlittle i think you tpyo'd included instead of valid 😆
| if Map.get(params, field) in included do | ||
| {:ok, params} | ||
| else | ||
| {:error, "#{field} isn't into the list."} |
There was a problem hiding this comment.
I think it might be helpful to return the valid values in the error to provide a more helpful message. Taking inspiration from Joi we could do something like this:
defp included(%__MODULE__{valid: valid}, field, params) do
if Map.get(params, field) in valid do
{:ok, params}
else
{:error, "#{field} must be one of #{valid}"}
end
end
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.