diff options
author | Daniil Baturin <daniil@baturin.org> | 2015-04-26 15:01:13 +0600 |
---|---|---|
committer | Daniil Baturin <daniil@baturin.org> | 2015-04-26 15:01:13 +0600 |
commit | 35bbd44f54b2738027869d67c99f55c742945a5e (patch) | |
tree | 97639859ac8b7004927ad33c4d9090845f0029ad | |
parent | 124b2b2e1081e45bfd31bb71992f9b60550e6668 (diff) | |
download | vyconf-35bbd44f54b2738027869d67c99f55c742945a5e.tar.gz vyconf-35bbd44f54b2738027869d67c99f55c742945a5e.zip |
Make Value_checker.validate_any return true if constraint list is empty
(i.e. no constraints means anything goes).
-rw-r--r-- | src/value_checker.ml | 16 | ||||
-rw-r--r-- | test/value_checker_test.ml | 5 |
2 files changed, 17 insertions, 4 deletions
diff --git a/src/value_checker.ml b/src/value_checker.ml index 245b769..1c19c3d 100644 --- a/src/value_checker.ml +++ b/src/value_checker.ml @@ -17,8 +17,16 @@ let validate_value validators value_constraint value = | _ -> false with Not_found -> raise (Bad_validator t) -let rec validate_any validators constraints value = +(* If no constraints given, consider it valid. + Otherwise consider it valid if it satisfies at least + one constraint *) +let validate_any validators constraints value = + let rec aux validators constraints value = + match constraints with + | [] -> false + | c :: cs -> if validate_value validators c value then true + else aux validators cs value + in match constraints with - | [] -> false - | c :: cs -> if validate_value validators c value then true - else validate_any validators cs value + | [] -> true + | _ -> aux validators constraints value diff --git a/test/value_checker_test.ml b/test/value_checker_test.ml index a975d93..169ed86 100644 --- a/test/value_checker_test.ml +++ b/test/value_checker_test.ml @@ -38,6 +38,10 @@ let test_validate_any_invalid test_ctxt = let cs = [Regex "\\d+"; Regex "[a-z]+"] in assert_equal (validate_any validators cs "AAAA") false +let test_validate_any_no_constraints test_ctxt = + let cs = [] in + assert_equal (validate_any validators cs "foo") true + let suite = "VyConf value checker tests" >::: [ "test_check_regex_valid" >:: test_check_regex_valid; @@ -47,6 +51,7 @@ let suite = "test_check_external_bad_validator" >:: test_check_external_bad_validator; "test_validate_any_valid" >:: test_validate_any_valid; "test_validate_any_invalid" >:: test_validate_any_invalid; + "test_validate_any_no_constraints" >:: test_validate_any_no_constraints; ] let () = |