Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## 0.1.13 - 2026-07-20

### Fixed

- `BeamWeaver.Schema` now resolves primitive field types, nested schema modules,
and compound literal types such as `{:array, MySchema}` into their complete
JSON Schema definitions on current Elixir releases.

## 0.1.12 - 2026-07-18

### Added
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ Add BeamWeaver to your application:
```elixir
def deps do
[
{:beam_weaver, "~> 0.1.12"}
{:beam_weaver, "~> 0.1.13"}
]
end
```
Expand Down
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Add BeamWeaver to your Mix project:
```elixir
def deps do
[
{:beam_weaver, "~> 0.1.12"}
{:beam_weaver, "~> 0.1.13"}
]
end
```
Expand Down
2 changes: 1 addition & 1 deletion docs/deep_agents_quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Add BeamWeaver to your Mix project:
```elixir
def deps do
[
{:beam_weaver, "~> 0.1.12"},
{:beam_weaver, "~> 0.1.13"},
{:req, "~> 0.5"}
]
end
Expand Down
2 changes: 1 addition & 1 deletion docs/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Add BeamWeaver to your Mix project:
```elixir
def deps do
[
{:beam_weaver, "~> 0.1.12"}
{:beam_weaver, "~> 0.1.13"}
]
end
```
Expand Down
2 changes: 1 addition & 1 deletion docs/subgraphs.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ BeamWeaver is installed as an Elixir dependency, not with `pip` or `uv`:
```elixir
def deps do
[
{:beam_weaver, "~> 0.1.12"}
{:beam_weaver, "~> 0.1.13"}
]
end
```
Expand Down
47 changes: 45 additions & 2 deletions lib/beam_weaver/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,28 @@ defmodule BeamWeaver.Schema do
legacy `__beam_weaver_schema__/0` callback.
"""

@primitive_types [
:string,
:integer,
:number,
:float,
:decimal,
:boolean,
:binary,
:id,
:binary_id,
:object,
:map,
:array,
:list,
:null,
:any,
:date,
:time,
:utc_datetime,
:naive_datetime
]

defmacro __using__(_opts) do
quote do
import BeamWeaver.Schema,
Expand All @@ -39,10 +61,11 @@ defmodule BeamWeaver.Schema do
defmacro description(value), do: quote(do: @beam_weaver_schema_description(unquote(value)))
defmacro strict(value), do: quote(do: @beam_weaver_schema_strict(unquote(value)))

defmacro field(name_ast, type, opts \\ []) do
defmacro field(name_ast, type_ast, opts \\ []) do
name = literal_field_name!(__CALLER__, name_ast)
type = literal_field_type!(__CALLER__, type_ast)

quote bind_quoted: [name: name, type: Macro.escape(type), opts: opts] do
quote bind_quoted: [name: name, type: type, opts: opts] do
@beam_weaver_schema_fields {name, type, opts}
end
end
Expand All @@ -64,6 +87,12 @@ defmodule BeamWeaver.Schema do
@spec to_json_schema(term()) :: map()
def to_json_schema(schema) when is_map(schema), do: schema

def to_json_schema(type) when type in @primitive_types do
type
|> BeamWeaver.Tool.Schema.type_schema()
|> stringify_schema()
end

def to_json_schema(module) when is_atom(module) do
ensure_schema_module_compiled(module)

Expand Down Expand Up @@ -180,6 +209,20 @@ defmodule BeamWeaver.Schema do
end
end

defp literal_field_type!(caller, type_ast) do
expanded = Macro.expand_literals(type_ast, caller)

if Macro.quoted_literal?(expanded) do
{type, []} = Code.eval_quoted(expanded, [], caller)
type
else
raise CompileError,
file: caller.file,
line: caller.line,
description: "schema field types must be literal schema terms"
end
end

defp ensure_schema_module_compiled(module) do
case Code.ensure_compiled(module) do
{:module, _module} ->
Expand Down
5 changes: 5 additions & 0 deletions test/beam_weaver/agent/semantic_dsl_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ defmodule BeamWeaver.Agent.SemanticDSLTest do
assert schema.name == "facts_output"
assert schema.json_schema["additionalProperties"] == false
assert schema.json_schema["required"] == ["facts"]

assert schema.json_schema["properties"]["facts"] == %{
"type" => "array",
"items" => %{"type" => "string"}
}
end

test "response_schema strategy accepts atoms only" do
Expand Down
35 changes: 35 additions & 0 deletions test/beam_weaver/schema_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,41 @@ defmodule BeamWeaver.SchemaTest do

alias BeamWeaver.Schema

defmodule PrimitiveFields do
use BeamWeaver.Schema

field(:name, :string)
field(:enabled, :boolean)
end

defmodule NestedFields do
use BeamWeaver.Schema

field(:value, :string)
end

defmodule CompoundFields do
use BeamWeaver.Schema

field(:nested, NestedFields)
field(:entries, {:array, NestedFields})
end

test "module DSL resolves primitive atoms as JSON Schema types" do
schema = PrimitiveFields.json_schema()

assert schema["properties"]["name"] == %{"type" => "string"}
assert schema["properties"]["enabled"] == %{"type" => "boolean"}
end

test "module DSL resolves nested schema modules and arrays" do
nested_schema = NestedFields.json_schema()
schema = CompoundFields.json_schema()

assert schema["properties"]["nested"] == nested_schema
assert schema["properties"]["entries"] == %{"type" => "array", "items" => nested_schema}
end

describe "to_json_schema/1 field defaults" do
test "emits an explicit empty-string default" do
schema = Schema.to_json_schema({:object, [{:name, :string, [default: ""]}]})
Expand Down