You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# You can not use it, but it is mentioned here for test clarity
@@ -270,37 +271,57 @@ defmodule NormalValidatorStruct do
270
271
end
271
272
```
272
273
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.
275
285
276
286
## Define the struct by calling the main_validator for full access on the output
277
287
278
288
##### Options
279
289
280
-
*`main_validator` - if set as tuple like this {ModuleName, :function_name},
290
+
*`main_validator` - if set as tuple like this `{ModuleName, :function_name}`,
281
291
for guardedstruct, in fact you have a global validation.
282
292
283
293
```elixir
284
294
# First, it looks at whether a main_validator has been set for each field,
285
295
# otherwise it looks inside the module.
286
296
defmoduleNormalMainValidatorStructdo
287
-
aliasMyModule.AnotherModule
288
297
useGuardedStruct
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
291
301
field(:name, String.t())
292
302
field(:title, String.t())
293
303
end
294
304
295
305
# if `guardedstruct` has no `main_validator` which is configed
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.
0 commit comments