diff --git a/config/config.exs b/config/config.exs index 08f7580..475234a 100644 --- a/config/config.exs +++ b/config/config.exs @@ -1,6 +1,6 @@ # This file is responsible for configuring your application # and its dependencies with the aid of the Mix.Config module. -use Mix.Config +import Config # This configuration is loaded before any dependency and is restricted # to this project. If another project depends on this project, this diff --git a/config/dev.exs b/config/dev.exs index d2d855e..b30c7d0 100644 --- a/config/dev.exs +++ b/config/dev.exs @@ -1 +1 @@ -use Mix.Config +use Config diff --git a/config/test.exs b/config/test.exs index 678a891..1265799 100644 --- a/config/test.exs +++ b/config/test.exs @@ -1,4 +1,4 @@ -use Mix.Config +import Config defmodule CustomFormatValidator do def validate(_format, _data), do: true diff --git a/lib/ex_json_schema/validator/format.ex b/lib/ex_json_schema/validator/format.ex index 92bd24d..730a3cf 100644 --- a/lib/ex_json_schema/validator/format.ex +++ b/lib/ex_json_schema/validator/format.ex @@ -111,6 +111,7 @@ defmodule ExJsonSchema.Validator.Format do case apply(mod, fun, [format, data]) do true -> [] false -> [%Error{error: %Error.Format{expected: format}}] + {false, custom_validation_error_format_name} -> [%Error{error: %Error.Format{expected: custom_validation_error_format_name}}] end end end diff --git a/test/ex_json_schema/schema_test.exs b/test/ex_json_schema/schema_test.exs index d28569e..526c262 100644 --- a/test/ex_json_schema/schema_test.exs +++ b/test/ex_json_schema/schema_test.exs @@ -35,7 +35,7 @@ defmodule ExJsonSchema.SchemaTest do schema = %{"properties" => "foo"} assert_raise ExJsonSchema.Schema.InvalidSchemaError, - ~s(schema did not pass validation against its meta-schema: [%ExJsonSchema.Validator.Error{error: %ExJsonSchema.Validator.Error.Type{actual: "string", expected: ["object"]}, path: "#/properties"}]), + ~s(schema did not pass validation against its meta-schema: [%ExJsonSchema.Validator.Error{error: %ExJsonSchema.Validator.Error.Type{expected: ["object"], actual: "string"}, path: "#/properties"}]), fn -> resolve(schema) end end diff --git a/test/ex_json_schema/validator_test.exs b/test/ex_json_schema/validator_test.exs index 7b4d71c..1f7948a 100644 --- a/test/ex_json_schema/validator_test.exs +++ b/test/ex_json_schema/validator_test.exs @@ -723,6 +723,10 @@ defmodule ExJsonSchema.ValidatorTest do false end + def validate("custom_validation_error_format_name", _data) do + {false, "custom_format_fieldname"} + end + def validate("zipcode", data) do Regex.match?(~r/^\d+$/, data) end @@ -757,6 +761,25 @@ defmodule ExJsonSchema.ValidatorTest do ) end + test "configuring a custom format validator with custom error message" do + schema = + Schema.resolve( + %{ + "properties" => %{ + "error" => %{"format" => "custom_validation_error_format_name"} + } + }, + custom_format_validator: {MyFormatValidator, :validate} + ) + + assert_validation_errors( + schema, + %{"error" => ""}, + [{"Expected to be a valid custom_format_fieldname.", "#/error"}], + [%Error{error: %Error.Format{expected: "custom_format_fieldname"}, path: "#/error"}] + ) + end + test "passing the formatter as an option" do assert :ok = validate(%{"type" => "string"}, "foo", error_formatter: Error.StringFormatter)