-
-
Notifications
You must be signed in to change notification settings - Fork 10
Description
Code of Conduct
- I agree to follow this project's Code of Conduct
AI Policy
- I agree to follow this project's AI Policy, or I agree that AI was not used while creating this issue.
Versions
Elixir: 1.18.1-otp-27
Erlang/OTP: 27
AshTypescript: 0.8.0
Ash: 3.5+ (specify your exact version)
Mix: 1.18.1
Operating system
macOS
Current Behavior
When running mix ash_typescript.codegen without explicitly setting generate_zod_schemas and generate_validation_functions in the configuration, the code generation fails with a BadBooleanError.Error:
** (BadBooleanError) expected a boolean on left-side of "and", got: nil (ash_typescript 0.8.0) lib/ash_typescript/rpc/zod_schema_generator.ex:346: AshTypescript.Rpc.ZodSchemaGenerator.generate_zod_schemas_for_embedded_resources/1 (ash_typescript 0.8.0) lib/ash_typescript/rpc/codegen.ex:188: AshTypescript.Rpc.Codegen.generate_full_typescript/5 (ash_typescript 0.8.0) lib/ash_typescript/rpc/codegen.ex:116: AshTypescript.Rpc.Codegen.generate_typescript_types/2 (ash_typescript 0.8.0) lib/mix/tasks/ash_typescript.codegen.ex:67: Mix.Tasks.AshTypescript.Codegen.run/1 (mix 1.18.1) lib/mix/task.ex:495: anonymous fn/3 in Mix.Task.run_task/5 (mix 1.18.1) lib/mix/cli.ex:107: Mix.CLI.run_task/2
Root Cause:
In lib/ash_typescript/rpc/zod_schema_generator.ex, the following functions return nil when their respective configuration options are not explicitly set:
`def generate_zod_schemas? do
Application.get_env(:ash_typescript, :generate_zod_schemas)
end
def generate_validation_functions? do
Application.get_env(:ash_typescript, :generate_validation_functions)
end`
When these functions are used in boolean expressions (e.g., if generate_zod_schemas? and ...), the nil value causes Elixir to raise a BadBooleanError since it expects a strict boolean value on the left side of the and operator.
Configuration used:
config :ash_typescript,
ash_domains: [MyProject.Accounts],
output_file: "native/src/api/ash_rpc.ts",
run_endpoint: "/api/rpc/run",
validate_endpoint: "/api/rpc/validate",
input_field_formatter: :camel_case,
output_field_formatter: :camel_case
Reproduction
Setup:
- Create a new Phoenix project with Ash and AshTypescript
- Install dependencies:
- Configure AshTypescript without explicitly setting the boolean config options:
config :ash_typescript,
output_file: "assets/js/ash_rpc.ts",
run_endpoint: "/rpc/run",
validate_endpoint: "/rpc/validate"
- Create a basic resource with the AshTypescript extension:
defmodule MyApp.Todo do
use Ash.Resource,
domain: MyApp.Domain,
extensions: [AshTypescript.Resource]
typescript do
type_name "Todo"
end
attributes do
uuid_primary_key :id
attribute :title, :string, public?: true
end
actions do
defaults [:read, :create]
end
end
- Configure domain with RPC:
defmodule MyApp.Domain do
use Ash.Domain, extensions: [AshTypescript.Rpc]
typescript_rpc do
resource MyApp.Todo do
rpc_action :list_todos, :read
end
end
end
Reproduce the error:
mix deps.get
mix ash_typescript.codegen
**Expected to fail with:**
** (BadBooleanError) expected a boolean on left-side of "and", got: nil
Workaround:
Explicitly set the boolean values in config:
config :ash_typescript,
generate_zod_schemas: true,
generate_validation_functions: true,
output_file: "assets/js/ash_rpc.ts"
Expected Behavior
The code generation should succeed even when generate_zod_schemas and generate_validation_functions are not explicitly configured, using the documented default values (true for both according to the configuration documentation).
Expected function behavior:
def generate_zod_schemas? do
Application.get_env(:ash_typescript, :generate_zod_schemas, true)
end
def generate_validation_functions? do
Application.get_env(:ash_typescript, :generate_validation_functions, true)
end
This would ensure that:
When not configured, both options default to true (as documented)
No BadBooleanError is raised
Users can run mix ash_typescript.codegen with minimal configuration
The behavior matches the documentation which states both options default to true
Suggested Fix:
Add the default value parameter to Application.get_env/3 calls in both functions to return true when the configuration is not explicitly set, aligning with the documented default behavior.