Skip to content

Commit a62b00d

Browse files
committed
Add some call builders for validator in livebook
1 parent 3a1ff89 commit a62b00d

File tree

1 file changed

+35
-14
lines changed

1 file changed

+35
-14
lines changed

guidance/guarded-struct.livemd

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -251,16 +251,17 @@ NormalSubModuleAsGuardedEstructMacroStruct.Struct.builder(%{field: "name"})
251251
# First, it looks at whether a validator has been set for each field,
252252
# otherwise it looks inside the module.
253253
defmodule NormalValidatorStruct do
254-
alias MyModule.AnotherModule
255254
use GuardedStruct
256-
255+
# You can call `validator` from another module
256+
# You can use field(:name, String.t(), validator: {__MODULE__, :validator})
257+
# Even you can not call validator, because you put the reserved name inside module
257258
guardedstruct do
258-
field(:name, String.t(), validator: {AnotherModule, :validator})
259+
field(:name, String.t(), validator: {NormalValidatorStruct, :validator})
259260
field(:title, String.t())
260261
end
261262

262-
def validator(:title, value) do
263-
{:ok, :title, value}
263+
def validator(:name, value) do
264+
if is_binary(value), do: {:ok, :name, value}, else: {:error, :name, "Not right!"}
264265
end
265266

266267
# You can not use it, but it is mentioned here for test clarity
@@ -270,37 +271,57 @@ defmodule NormalValidatorStruct do
270271
end
271272
```
272273

273-
* Output without error: `{:ok, :field_name, value}`
274-
* Output with error: `{:error, :field_name, ERROR MESSAGE}`
274+
```elixir
275+
NormalValidatorStruct.builder(%{name: "Mishka"})
276+
```
277+
278+
```elixir
279+
NormalValidatorStruct.builder(%{name: :mishka})
280+
```
281+
282+
> **Note**: There are other ways to call the `validator` function. Using a Tuple is the first method. It is shown in the code excerpt above. The module address appears in the first entry, and the function name in Atomic form appears in the second.
283+
> Note that you can use `__MODULE__` if you define it in this module.
284+
> But there's a simpler method. The module itself has a reserved word for `validator`. It only has to be valued as a field; the macro will verify that the input is accurate.
275285
276286
## Define the struct by calling the main_validator for full access on the output
277287

278288
##### Options
279289

280-
* `main_validator` - if set as tuple like this {ModuleName, :function_name},
290+
* `main_validator` - if set as tuple like this `{ModuleName, :function_name}`,
281291
for guardedstruct, in fact you have a global validation.
282292

283293
```elixir
284294
# First, it looks at whether a main_validator has been set for each field,
285295
# otherwise it looks inside the module.
286296
defmodule NormalMainValidatorStruct do
287-
alias MyModule.AnotherModule
288297
use GuardedStruct
289-
290-
guardedstruct main_validator: {AnotherModule, :main_validator} do
298+
# You can call `main_validator` from another module
299+
# Even you can not call validator, because you put the reserved name inside module
300+
guardedstruct main_validator: {__MODULE__, :main_validator} do
291301
field(:name, String.t())
292302
field(:title, String.t())
293303
end
294304

295305
# if `guardedstruct` has no `main_validator` which is configed
296306
def main_validator(value) do
297-
{:ok, value}
307+
if Map.get(value, :title) == "mishka" do
308+
{:ok, value}
309+
else
310+
{:error, [%{message: "Not right!", field: :title, action: :validator}]}
311+
end
298312
end
299313
end
300314
```
301315

302-
* Output without error: `{:ok, value}`
303-
* Output with error: `{:error, :general_reason, errors_list}`
316+
```elixir
317+
NormalMainValidatorStruct.builder(%{title: "mishka"})
318+
```
319+
320+
```elixir
321+
NormalMainValidatorStruct.builder(%{title: :mishka})
322+
```
323+
324+
As the code sample above illustrates. `main_validator` is a bit rudimentary in terms of error and data presentation, and it can only be used once. since it gives you the ability to generate a unique code. Similar to `validator`, this option can be called by a macro within the same module or from another module.
304325

305326
## Define struct with derive
306327

0 commit comments

Comments
 (0)