Skip to content

Commit

Permalink
Add custom validator error field name, fix warnings, fix 1 failing test
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristophLeitner-AKM committed Apr 6, 2023
1 parent a101983 commit 804cd01
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion config/dev.exs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
use Mix.Config
use Config
2 changes: 1 addition & 1 deletion config/test.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use Mix.Config
import Config

defmodule CustomFormatValidator do
def validate(_format, _data), do: true
Expand Down
1 change: 1 addition & 0 deletions lib/ex_json_schema/validator/format.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion test/ex_json_schema/schema_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
23 changes: 23 additions & 0 deletions test/ex_json_schema/validator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 804cd01

Please sign in to comment.