zf

zenflows testing
git clone https://s.sonu.ch/~srfsh/zf.git
Log | Files | Refs | Submodules | README | LICENSE

commit 20c387ab762bd95c391bf1206bfe7ef8b2d8032b
parent dae518d958bba4756d77a0c6cd82d53ce6a0e008
Author: srfsh <dev@srf.sh>
Date:   Tue, 16 Aug 2022 17:46:26 +0300

Zenflows{Test,}.VF.Validate: loosen length requirements

It's a bit much to require at least 2-3 bytes.
Requiring at least a byte is enough.

Diffstat:
Msrc/zenflows/vf/validate.ex | 24++++++++++++------------
Mtest/vf/validate.test.exs | 8++++----
2 files changed, 16 insertions(+), 16 deletions(-)

diff --git a/src/zenflows/vf/validate.ex b/src/zenflows/vf/validate.ex @@ -38,12 +38,12 @@ def key(cset, field) do end) end -@doc "Checks if the given string field is [2, 256] bytes long." +@doc "Checks if the given string field is [1, 256] bytes long." @spec name(Chset.t(), atom()) :: Chset.t() def name(cset, field) do Chset.validate_change(cset, field, :valflow, fn - _, str when byte_size(str) < 2 -> - [{field, "should be at least 2 bytes long"}] + _, str when byte_size(str) < 1 -> + [{field, "should be at least 1 byte long"}] _, str when byte_size(str) > 256 -> [{field, "should be at most 256 bytes long"}] _, _ -> @@ -51,12 +51,12 @@ def name(cset, field) do end) end -@doc "Checks if the given string field is [2, 2048] bytes long." +@doc "Checks if the given string field is [1, 2048] bytes long." @spec note(Chset.t(), atom()) :: Chset.t() def note(cset, field) do Chset.validate_change(cset, field, :valflow, fn - _, str when byte_size(str) < 2 -> - [{field, "should be at least 2 bytes long"}] + _, str when byte_size(str) < 1 -> + [{field, "should be at least 1 bytes long"}] _, str when byte_size(str) > 2048 -> [{field, "should be at most 2048 bytes long"}] _, _ -> @@ -64,12 +64,12 @@ def note(cset, field) do end) end -@doc "Checks if the given string is [3, 512] bytes long." +@doc "Checks if the given string is [1, 512] bytes long." @spec uri(Chset.t(), atom()) :: Chset.t() def uri(cset, field) do Chset.validate_change(cset, field, :valflow, fn - _, str when byte_size(str) < 3 -> - [{field, "should be at least 3 bytes long"}] + _, str when byte_size(str) < 1 -> + [{field, "should be at least 1 bytes long"}] _, str when byte_size(str) > 512 -> [{field, "should be at most 512 bytes long"}] _, _ -> @@ -101,7 +101,7 @@ end @doc """ Checks if the given classifications (list of strings) for: - - Each item in the list is [3, 512] bytes long; + - Each item in the list is [1, 512] bytes long; - The list can contain only [1, 128] items. """ @spec class(Chset.t(), atom()) :: Chset.t() @@ -112,7 +112,7 @@ def class(cset, field) do _, list -> case do_class(list) do {:exceeds, _ind} -> [{field, "must contain at most 128 items"}] - {:short, ind} -> [{field, "the item at #{ind + 1} cannot be shorter than 3 bytes"}] + {:short, ind} -> [{field, "the item at #{ind + 1} cannot be shorter than 1 bytes"}] {:long, ind} -> [{field, "the item at #{ind + 1} cannot be longer than 512 bytes"}] {:valid, _ind} -> [] end @@ -134,7 +134,7 @@ end defp do_class([head | tail], index, remaining) do cond do remaining == 0 -> {:exceeds, index} - byte_size(head) < 3 -> {:short, index} + byte_size(head) < 1 -> {:short, index} byte_size(head) > 512 -> {:long, index} true -> do_class(tail, index + 1, remaining - 1) end diff --git a/test/vf/validate.test.exs b/test/vf/validate.test.exs @@ -83,7 +83,7 @@ end describe "name/2" do test "with too short param" do assert %Changeset{errors: errs} = - %{name: "a"} + %{name: ""} |> name_chgset() |> Validate.name(:name) @@ -112,7 +112,7 @@ end describe "note/2" do test "with too short param" do assert %Changeset{errors: errs} = - %{note: "a"} + %{note: ""} |> note_chgset() |> Validate.note(:note) @@ -170,7 +170,7 @@ end describe "uri/2" do test "with too short param" do assert %Changeset{errors: errs} = - %{uri: "aa"} + %{uri: ""} |> uri_chgset() |> Validate.uri(:uri) @@ -217,7 +217,7 @@ describe "class/2" do test "with one of the items too short" do assert %Changeset{errors: errs} = - %{list: Enum.map(0..64, &("uri #{&1}")) ++ ["aa"]} + %{list: Enum.map(0..64, &("uri #{&1}")) ++ [""]} |> class_chgset() |> Validate.class(:list)