Skip to content

Commit 792174f

Browse files
committed
Update Ash.Type.NewType validation, set description to be either :string
or nil for both Ash.TypedStruct & Ash.Type.Struct.
1 parent b3da249 commit 792174f

File tree

4 files changed

+11
-10
lines changed

4 files changed

+11
-10
lines changed

lib/ash/type/new_type.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ defmodule Ash.Type.NewType do
459459
{key, field}, :ok ->
460460
field_keys = field |> List.wrap() |> Keyword.keys()
461461

462-
case field_keys -- [:type, :constraints, :allow_nil?] do
462+
case field_keys -- [:type, :constraints, :allow_nil?, :description] do
463463
[] ->
464464
{:cont, validate_constraints(field[:type], field[:constraints])}
465465

lib/ash/type/struct.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ defmodule Ash.Type.Struct do
2525
default: true
2626
],
2727
description: [
28-
type: :string
28+
type: {:or, [:string, nil]}
2929
],
3030
constraints: [
3131
type: :keyword_list,
@@ -83,7 +83,7 @@ defmodule Ash.Type.Struct do
8383
Example:
8484
8585
defmodule MyStruct do
86-
use Ash.TypedStruct
86+
use Ash.TypedStruct
8787
typed_struct do
8888
field :name, :string, allow_nil?: false
8989
field :age, :integer, constraints: [min: 0]

lib/ash/typed_struct.ex

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ defmodule Ash.TypedStruct do
1010
## Example
1111
1212
defmodule MyApp.UserProfile do
13-
use Ash.TypedStruct
13+
use Ash.TypedStruct
1414
1515
typed_struct do
1616
field :username, :string, allow_nil?: false
@@ -23,14 +23,14 @@ defmodule Ash.TypedStruct do
2323
2424
# Creating instances
2525
{:ok, profile} = MyApp.UserProfile.new(username: "john", email: "john@example.com")
26-
26+
2727
# Using new! for raising on errors
2828
profile = MyApp.UserProfile.new!(username: "jane", email: "jane@example.com", age: 25)
2929
3030
# Can be used as an Ash type
3131
defmodule MyApp.User do
3232
use Ash.Resource
33-
33+
3434
attributes do
3535
attribute :profile, MyApp.UserProfile
3636
end
@@ -98,7 +98,8 @@ defmodule Ash.TypedStruct do
9898
doc: "the default value for the field"
9999
],
100100
description: [
101-
type: :any,
101+
type: {:or, [:string, nil]},
102+
default: nil,
102103
doc: "a description for the field"
103104
],
104105
constraints: [
@@ -112,7 +113,7 @@ defmodule Ash.TypedStruct do
112113
type: :boolean,
113114
default: true,
114115
doc: """
115-
Whether or not the field can be set to nil.
116+
Whether or not the field can be set to nil.
116117
"""
117118
]
118119
]

test/typed_struct_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ defmodule Ash.TypedStructTest do
1313
use Ash.TypedStruct
1414

1515
typed_struct do
16-
field(:id, Ash.Type.UUID, allow_nil?: false)
17-
field(:name, :string, allow_nil?: false)
16+
field(:id, Ash.Type.UUID, allow_nil?: false, description: "The unique identifier for the user")
17+
field(:name, :string, allow_nil?: false, description: "The name of the user")
1818
field(:email, :string, constraints: [match: ~r/@/])
1919
field(:age, :integer, constraints: [min: 0, max: 150])
2020
field(:active, :boolean, default: true)

0 commit comments

Comments
 (0)