-
-
Notifications
You must be signed in to change notification settings - Fork 41
feat: Add {:and, subtypes} option type.
#246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| # SPDX-FileCopyrightText: 2022 spark contributors <https://github.com/ash-project/spark/graphs/contributors> | ||
| # | ||
| # SPDX-License-Identifier: MIT | ||
|
|
||
| defmodule Spark.Options.AndTypeTest do | ||
| @moduledoc false | ||
|
|
||
| use ExUnit.Case | ||
|
|
||
| describe "{:and, subtypes}" do | ||
| test "validates when value matches all subtypes" do | ||
| schema = [value: [type: {:and, [:integer, :pos_integer]}]] | ||
|
|
||
| assert {:ok, [value: 5]} = Spark.Options.validate([value: 5], schema) | ||
| end | ||
|
|
||
| test "fails when value doesn't match any subtype" do | ||
| schema = [value: [type: {:and, [:integer, :pos_integer]}]] | ||
|
|
||
| assert {:error, %Spark.Options.ValidationError{}} = | ||
| Spark.Options.validate([value: "not an integer"], schema) | ||
| end | ||
|
|
||
| test "fails when value matches first subtype but not second" do | ||
| schema = [value: [type: {:and, [:integer, :pos_integer]}]] | ||
|
|
||
| assert {:error, %Spark.Options.ValidationError{}} = | ||
| Spark.Options.validate([value: -5], schema) | ||
| end | ||
|
|
||
| test "fails when value matches second subtype but not first" do | ||
| schema = [value: [type: {:and, [:pos_integer, {:in, [1, 2, 3]}]}]] | ||
|
|
||
| assert {:error, %Spark.Options.ValidationError{}} = | ||
| Spark.Options.validate([value: 5], schema) | ||
| end | ||
|
|
||
| test "threads transformed values through subsequent subtypes" do | ||
| # First subtype transforms the value, second validates the transformed value | ||
| schema = [ | ||
| value: [ | ||
| type: | ||
| {:and, | ||
| [ | ||
| {:custom, __MODULE__, :double_value, []}, | ||
| {:in, [10, 20, 30]} | ||
| ]} | ||
| ] | ||
| ] | ||
|
|
||
| # 5 gets doubled to 10, which is in [10, 20, 30] | ||
| assert {:ok, [value: 10]} = Spark.Options.validate([value: 5], schema) | ||
|
|
||
| # 7 gets doubled to 14, which is NOT in [10, 20, 30] | ||
| assert {:error, %Spark.Options.ValidationError{}} = | ||
| Spark.Options.validate([value: 7], schema) | ||
| end | ||
|
|
||
| test "works with three or more subtypes" do | ||
| schema = [ | ||
| value: [type: {:and, [:integer, :pos_integer, {:in, 1..10}]}] | ||
| ] | ||
|
|
||
| assert {:ok, [value: 5]} = Spark.Options.validate([value: 5], schema) | ||
|
|
||
| assert {:error, %Spark.Options.ValidationError{}} = | ||
| Spark.Options.validate([value: 15], schema) | ||
| end | ||
|
|
||
| test "works with keyword_list subtype" do | ||
| schema = [ | ||
| config: [ | ||
| type: | ||
| {:and, | ||
| [ | ||
| :keyword_list, | ||
| {:keyword_list, [name: [type: :string, required: true]]} | ||
| ]} | ||
| ] | ||
| ] | ||
|
|
||
| assert {:ok, [config: [name: "test"]]} = | ||
| Spark.Options.validate([config: [name: "test"]], schema) | ||
|
|
||
| assert {:error, %Spark.Options.ValidationError{}} = | ||
| Spark.Options.validate([config: []], schema) | ||
| end | ||
|
|
||
| test "works with non_empty_keyword_list subtype" do | ||
| schema = [ | ||
| config: [ | ||
| type: | ||
| {:and, | ||
| [ | ||
| :non_empty_keyword_list, | ||
| {:keyword_list, [name: [type: :string]]} | ||
| ]} | ||
| ] | ||
| ] | ||
|
|
||
| assert {:ok, [config: [name: "test"]]} = | ||
| Spark.Options.validate([config: [name: "test"]], schema) | ||
|
|
||
| assert {:error, %Spark.Options.ValidationError{}} = | ||
| Spark.Options.validate([config: []], schema) | ||
| end | ||
|
|
||
| test "works with map subtype" do | ||
| schema = [ | ||
| config: [ | ||
| type: | ||
| {:and, | ||
| [ | ||
| :map, | ||
| {:map, [name: [type: :string, required: true]]} | ||
| ]} | ||
| ] | ||
| ] | ||
|
|
||
| assert {:ok, [config: %{name: "test"}]} = | ||
| Spark.Options.validate([config: %{name: "test"}], schema) | ||
|
|
||
| assert {:error, %Spark.Options.ValidationError{}} = | ||
| Spark.Options.validate([config: %{}], schema) | ||
| end | ||
|
|
||
| test "error message mentions all failing subtypes" do | ||
| schema = [value: [type: {:and, [:integer, :string]}]] | ||
|
|
||
| assert {:error, %Spark.Options.ValidationError{message: message}} = | ||
| Spark.Options.validate([value: :atom], schema) | ||
|
|
||
| assert message =~ "integer" | ||
| assert message =~ "string" | ||
| end | ||
| end | ||
|
|
||
| # Helper function for custom type that doubles the value | ||
| def double_value(value) when is_integer(value), do: {:ok, value * 2} | ||
| def double_value(value), do: {:error, "expected integer, got: #{inspect(value)}"} | ||
| end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lol
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be nice to not have to do this. We may want to do something to make it look nice in docs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually yeah I don't see a handler for the docs bit here.