ash
ash copied to clipboard
Calculations with `allow_nil?: false` should raise an error when loading `nil` value
Describe the bug
Calculation with allow_nil?: false has no effect on record that loads using PostgreSQL query that resolves to NULL (e.g., (NULL :: UUID = ... :: UUID) :: BOOLEAN).
Expected behavior An error when loading such record.
Runtime
"ash": {:git, "https://github.com/ash-project/ash.git", "5315d3d6c4e3b7de8223b0098e562b610fa0e5a0", []},
"ash_authentication": {:git, "https://github.com/team-alembic/ash_authentication.git", "a0dd7796e1f17b4cb9cd33d0ba31b63065fe4c56", []},
"ash_authentication_phoenix": {:git, "https://github.com/team-alembic/ash_authentication_phoenix.git", "e3e2f811e798c094d067dc4e9c2b86045bc44220", []},
"ash_graphql": {:git, "https://github.com/ash-project/ash_graphql.git", "e95c95282acd7a60cf0cb6a370888e7478c2e15d", []},
"ash_json_api": {:hex, :ash_json_api, "0.34.0", "f11b21c322cead92d0a886c2f9640a35c5866e5024c4744ad1869996aeb3b123", [:mix], [{:ash, "~> 2.3 and >= 2.9.24", [hex: :ash, repo: "hexpm", optional: false]}, {:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: false]}, {:json_xema, "~> 0.4.0", [hex: :json_xema, repo: "hexpm", optional: false]}, {:open_api_spex, "~> 3.16", [hex: :open_api_spex, repo: "hexpm", optional: true]}, {:plug, "~> 1.11", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "192d805447e2ed506751a2ae6f58f564741f68a9e8cba1a71a2f6f3928e182f1"},
"ash_oban": {:git, "https://github.com/ash-project/ash_oban.git", "a65dd5f413b0b9f45cc6adb3ac70eae03caa35a7", []},
"ash_phoenix": {:hex, :ash_phoenix, "1.2.23", "9d98bb2c1f9762e27411a5a021b43d5fb5ab716f195346d2b5edc422d789ed23", [:mix], [{:ash, ">= 2.14.1 and < 3.0.0-0", [hex: :ash, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.5.6 or ~> 1.6", [hex: :phoenix, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.14 or ~> 3.0", [hex: :phoenix_html, repo: "hexpm", optional: false]}, {:phoenix_live_view, "~> 0.15", [hex: :phoenix_live_view, repo: "hexpm", optional: false]}], "hexpm", "64a09ad8969e83a36da0975f66e0d3acfb66754e0d091da7bfe1c94b70a5b6ff"},
"ash_postgres": {:git, "https://github.com/ash-project/ash_postgres.git", "bc4b69a5681cda56b469abaf9891499dff949c58", []},
"ash_state_machine": {:git, "https://github.com/ash-project/ash_state_machine.git", "fa109180e4c15ddfdc42e625b7a08417c376c54c", []},
elixir 1.15.6-otp-26 /Users/smefju/repos/[...]/.tool-versions
erlang 26.0.2 /Users/smefju/repos/[...]/.tool-versions
Yeah, this is definitely an interesting issue. We don't actually currently validate the types and or values returned from calculations, primarily because it would add a non-trivial cost to do so, especially ones calculated in the data layer, because we'd basically have to traverse the entire result set and validate. We could potentially add this as a dev-only validation? We've had internal discussions about ways to let dev/staging be more strict at the expense of some marginal slow downs. Anyone have thoughts on that idea?
Maybe you should just remove allow_nil? configuration option?
its important to communicate generated type information, like in ash_graphql, so we need to have the option.
I know but you cannot guarantee return type so generating "always set" schema might be inconsistent with actual data 🤔
I don't think it's really an option to remove, because non-nullability drastically simplifies client interfaces, it just has to be on the user to ensure that their calculation honors the rules they set forth. I think the best short term thing we can do is document and then medium term we could show a warning only in dev mode or something like that.