Skip to content

Commit

Permalink
Rename the value() helper to any()
Browse files Browse the repository at this point in the history
  • Loading branch information
colinsmetz committed Oct 23, 2021
1 parent b7ed302 commit 3702b2f
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 44 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ All helpers support a few common options:
- `cast_from` - See [Casting](#casting)
- `on_error` - See [Overriding error messages](#overriding-error-messages)

The `value()` helper is the only helper without a specific type. It can be used
The `any()` helper is the only helper without a specific type. It can be used
to match literally anything.

### Checks
Expand All @@ -106,7 +106,7 @@ is useful if the rule should only be evaluated on well-formed data.
integer(checks: [between(0, 10)])

# Accepts only "yes", true, or 1
value(check: one_of(["yes", true, 1]))
any(check: one_of(["yes", true, 1]))

# A number that is different from 0
number(check: rule(&(&1 != 0), "The number should be different from zero."))
Expand Down
4 changes: 2 additions & 2 deletions guides/schema_definition.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ All types of elements support a few common options:

Several helpers are provided to define basic types:

* `value()`: matches anything
* `any()`
* `integer()`
* `float()`
* `number()`
Expand Down Expand Up @@ -365,7 +365,7 @@ Then you can call `validate(tree, tree_schema())` as usual.

Sometimes, a value can take multiple possible forms. In this case, it is useful
to specify a union of possible types. This possible to some extent using the
catch-all helper `value()` or using `cast_from`, but those are not exactly
catch-all helper `any()` or using `cast_from`, but those are not exactly
unions.

Flawless offers two better ways to do unions of specific schemas.
Expand Down
2 changes: 1 addition & 1 deletion lib/flawless.ex
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ defmodule Flawless do
%Spec{for: %Spec.List{}} -> validate_list(value, schema, context)
%Union{} -> validate_union(value, schema, context)
[item_type] -> validate_list(value, Helpers.list(item_type), context)
[] -> validate_list(value, Helpers.list(Helpers.value()), context)
[] -> validate_list(value, Helpers.list(Helpers.any()), context)
%Spec{for: %Spec.Tuple{}} -> validate_tuple(value, schema, context)
tuple when is_tuple(tuple) -> validate_tuple(value, Helpers.tuple(tuple), context)
%Spec{for: %Spec.Literal{}} -> validate_literal(value, schema, context)
Expand Down
24 changes: 14 additions & 10 deletions lib/flawless/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ defmodule Flawless.Helpers do
}
end

defp value(opts) do
type = opts |> Keyword.get(:type, :any)

%Spec.Value{schema: opts |> Keyword.get(:schema, nil)}
|> build_spec(type, opts)
end

@doc """
Represents any value.
Expand All @@ -62,22 +69,19 @@ defmodule Flawless.Helpers do
## Examples
iex> Flawless.validate(:something, value())
iex> Flawless.validate(:something, any())
[]
iex> Flawless.validate([1, 2, 3], value())
iex> Flawless.validate([1, 2, 3], any())
[]
iex> Flawless.validate(2, value(in: [1, "1", :one]))
iex> Flawless.validate(2, any(in: [1, "1", :one]))
[%Flawless.Error{context: [], message: "Invalid value: 2. Valid options: [1, \\"1\\", :one]"}]
"""
@spec value(keyword) :: Flawless.Spec.t()
def value(opts \\ []) do
type = opts |> Keyword.get(:type, :any)

%Spec.Value{schema: opts |> Keyword.get(:schema, nil)}
|> build_spec(type, opts)
@spec any(keyword) :: Flawless.Spec.t()
def any(opts \\ []) do
value(opts)
end

@doc """
Expand Down Expand Up @@ -105,7 +109,7 @@ defmodule Flawless.Helpers do
## Shortcuts
If you do not need any of the options, you can use the `[item_type]` shortcut instead of this
function. You can also use `[]` for matching any list (equivalent to `list(value())`). For
function. You can also use `[]` for matching any list (equivalent to `list(any())`). For
matching the empty list, consider using `literal([])` instead.
"""
Expand Down
4 changes: 2 additions & 2 deletions lib/flawless/schema_validator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ defmodule Flawless.SchemaValidator do

defp literal_spec_schema() do
structure(%Flawless.Spec.Literal{
value: value()
value: any()
})
end

Expand Down Expand Up @@ -117,7 +117,7 @@ defmodule Flawless.SchemaValidator do
%_{} ->
structure(%Flawless.Rule{
predicate: predicate_schema(),
message: value()
message: any()
})

_else ->
Expand Down
2 changes: 1 addition & 1 deletion test/flawless/inspect_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ defmodule Flawless.InspectTest do
schema = %{
"a" => number(),
"b" => string(check: min_length(2), cast_from: [:integer, :atom]),
"c" => value(),
"c" => any(),
"d" => literal(19),
"e" => list(string(), non_empty: true, no_duplicate: true),
"f" => tuple({number(), atom()}, nil: true),
Expand Down
4 changes: 2 additions & 2 deletions test/flawless/schema_validator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ defmodule Flawless.SchemaValidatorTest do
"polling" =>
map(%{
"slice_size" =>
value(
any(
checks: [
rule(&(String.length(&1) > 100), "Slice size must be longer than 100")
]
Expand All @@ -39,7 +39,7 @@ defmodule Flawless.SchemaValidatorTest do
"is_required" => boolean(),
"meta" =>
map(%{
"id" => value()
"id" => any()
})
},
checks: []
Expand Down
48 changes: 24 additions & 24 deletions test/flawless_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ defmodule FlawlessTest do
describe "basic types" do
import Flawless.Helpers

test "value/1 expects any value" do
assert validate(123, value()) == []
assert validate(true, value()) == []
assert validate("hey", value()) == []
test "any/1 expects any value" do
assert validate(123, any()) == []
assert validate(true, any()) == []
assert validate("hey", any()) == []
end

test "boolean/1 expects a boolean value" do
Expand Down Expand Up @@ -195,7 +195,7 @@ defmodule FlawlessTest do
checks = [one_of([0, 1])]
expected_errors = [Error.new("Invalid value: 123. Valid options: [0, 1]", [])]

assert validate(123, value(checks: checks)) == expected_errors
assert validate(123, any(checks: checks)) == expected_errors
assert validate(123, number(checks: checks)) == expected_errors
assert validate(123, integer(checks: checks)) == expected_errors
assert validate(0, integer(checks: checks)) == []
Expand Down Expand Up @@ -476,7 +476,7 @@ defmodule FlawlessTest do
Error.new("Expected type: number, got: \"hey\".", ["d"])
]

assert validate(%{}, %{any_key() => value()}) == []
assert validate(%{}, %{any_key() => any()}) == []
end
end

Expand Down Expand Up @@ -621,7 +621,7 @@ defmodule FlawlessTest do
tuple(
{
atom(checks: [one_of([:ok, :error])]),
value()
any()
},
checks: tuple_checks
)
Expand Down Expand Up @@ -775,7 +775,7 @@ defmodule FlawlessTest do
assert validate(nil, string(nil: false)) == [Error.new("Value cannot be nil.", [])]
assert validate(nil, atom(nil: false)) == [Error.new("Value cannot be nil.", [])]

assert validate(nil, value(in: [nil], nil: false)) == [
assert validate(nil, any(in: [nil], nil: false)) == [
Error.new("Value cannot be nil.", [])
]

Expand Down Expand Up @@ -804,7 +804,7 @@ defmodule FlawlessTest do
assert validate(nil, nil) == []
assert validate(nil, literal(nil)) == []
assert validate(nil, atom()) == []
assert validate(nil, value()) == []
assert validate(nil, any()) == []
end

test "a nil value is not accepted for required fields in a map, when nothing was specified" do
Expand Down Expand Up @@ -920,17 +920,17 @@ defmodule FlawlessTest do
test "it validates very complex schema" do
schema = %{
"format" => string(checks: [one_of(["csv", "xml"])]),
"regex" => string(),
"string" => string(),
maybe("bim") => %{
"truc" => string(),
"struct" => structure(%M{x: number()})
},
maybe("polling") =>
maybe("settings") =>
map(%{
maybe("slice_size") =>
value(
maybe("stuff") =>
any(
checks: [
rule(&(String.length(&1) > 100), "Slice size must be longer than 100")
rule(&(String.length(&1) > 100), "Stuff must be longer than 100")
]
)
}),
Expand All @@ -944,7 +944,7 @@ defmodule FlawlessTest do
maybe("is_required") => boolean(),
maybe("meta") =>
map(%{
"id" => value()
"id" => any()
})
},
checks: [required_if_is_key()]
Expand All @@ -962,27 +962,27 @@ defmodule FlawlessTest do

value = %{
"format" => "yml",
"regex" => "/the/regex",
"string" => "/the/string",
"options" => %{"a" => "b"},
"bim" => %{},
"fields" => [
%{"name" => "a", "type" => "INT64", "is_key" => true, "is_required" => false},
%{"name" => "b", "type" => "STRING", "tru" => "blop", "meta" => %{}}
],
"polling" => %{
"slice_size" => "50MB",
"interval_seconds" => "12",
"timeout_ms" => "34567"
"settings" => %{
"stuff" => "abcd",
"other_stuff" => "12",
"something" => "34567"
},
"file_max_age_days" => "67",
"unexpected_number" => "67",
"brands" => ["hey", 28],
"status" => :ok,
"tuple_of_things" => {["x", "y", 9, "z"], %{}},
"test" => nil
}

assert Flawless.validate(value, schema) == [
Error.new("Unexpected fields: [\"file_max_age_days\", \"options\"].", []),
Error.new("Unexpected fields: [\"options\", \"unexpected_number\"].", []),
Error.new("Missing required fields: \"struct\" (struct), \"truc\" (string).", [
"bim"
]),
Expand All @@ -991,8 +991,8 @@ defmodule FlawlessTest do
Error.new("Unexpected fields: [\"tru\"].", ["fields", 1]),
Error.new("Missing required fields: \"id\" (any).", ["fields", 1, "meta"]),
Error.new("Invalid value: \"yml\". Valid options: [\"csv\", \"xml\"]", ["format"]),
Error.new("Unexpected fields: [\"interval_seconds\", \"timeout_ms\"].", ["polling"]),
Error.new("Slice size must be longer than 100", ["polling", "slice_size"]),
Error.new("Unexpected fields: [\"other_stuff\", \"something\"].", ["settings"]),
Error.new("Stuff must be longer than 100", ["settings", "stuff"]),
Error.new("Value cannot be nil.", ["test"]),
Error.new("Expected type: string, got: 9.", ["tuple_of_things", 0, 2])
]
Expand Down

0 comments on commit 3702b2f

Please sign in to comment.