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
Copy file name to clipboardExpand all lines: guidance/guarded-struct.md
+286-1Lines changed: 286 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,7 +22,7 @@ Before explaining the copyright, I must point out that the primary library, whic
22
22
23
23
## Copyright
24
24
25
-
The code in this module is based on the 'typed_struct' library (https://github.com/ejpcmac/typed_struct),
25
+
The code in this module is based on the `typed_struct` library (https://github.com/ejpcmac/typed_struct),
26
26
which is licensed under the MIT License.
27
27
28
28
Modifications and additions have been made to enhance its capabilities as part of the current project.
@@ -306,6 +306,7 @@ end
306
306
|`"validate(enum=Map[%{status: 1}::%{status: 2}::%{status: 3}])"`| NO | Validate if the data is one of the enum value, which is Map|
307
307
|`"validate(enum=Tuple[{:admin, 1}::{:user, 2}::{:banned, 3}])"`| NO | Validate if the data is one of the enum value, which is Tuple|
308
308
|`"validate(equal=some_thing)"`| NO | Validate if the data is equal with validation value, which is any type|
309
+
|`"validate(either=[string, enum=Integer[1::2::3]])"`| NO | Validate if the data is valid with each derive validation|
309
310
310
311
```elixir
311
312
defmoduleMyModuledo
@@ -543,3 +544,287 @@ end
543
544
name:"Mishka"
544
545
}}
545
546
```
547
+
548
+
10.#### Set config to show error inside `defexception`
549
+
550
+
You may want to display the received errors in Elixir's `defexception`. you just need to enable the `error: true` for `guardedstruct` macro or `sub_field`.
551
+
552
+
**Note**: When you enable the `error` option. This macro will generate for you a module that is part of the parent module subset, and within that module, it will generate a `defexception` struct.
11.#### `authorized_fields` option to limit user input
586
+
587
+
If this option is not used, the program will automatically drop fields that are not defined; however, if this option is set, it will return an error to the user if they transmit a field that is not in the list of specified fields. If this option is not used, the program will automatically drop fields that are not defined.
588
+
589
+
**Please take note** that the `required_fields` and this section are not the same thing, and that the validation of the mandatory fields will take place after this section.
12.#### `authorized_fields` option to limit user input
616
+
617
+
This option can be helpful for you if you wish to construct your own modules in various files and then make those modules reusable in the future. Simply implement the macro in another module, and then call that module from the `field` macro. The `struct` and `structs` options are the ones in which the module can be placed. The first one will provide you with an indication that you will be given a map, and the second one will provide you with a list of maps.
As was discussed in the earlier available choices. In the `field` macro that is used to call **another module**, as well as in the `sub_field` macro, you have the ability to retrieve a list of structs rather than a single struct.
You will need to include a function known as `__information__()` in each and every module that you develop for your very own `structs`. This function will store a variety of information, such as keys, callers, and so on.
681
+
682
+
**Note:** There is a possibility that further information will be added to this function; please check its output after each update.
683
+
684
+
**Note:** If you call another Struct module within the `field` macro, you should not use the `caller` key within this function. This is due to the fact that the constructor information is only available during **compile** time, and not run time.
685
+
686
+
```elixir
687
+
TestStruct.__information__()
688
+
```
689
+
690
+
15.#### Transmitting whole output of builder function to its children
691
+
692
+
Because new keys have been added, such as `auto`, `on`, and `from` which will be explained in more detail below. The `builder` function is available in the following two different styles.
693
+
694
+
> If you don't provide the `:root` key, you can just specify the child key, but if you do, you have to send the entire map as an `attar`. This is something to keep in mind.
695
+
696
+
697
+
```elixir
698
+
defbuilder(attrs, error)
699
+
700
+
defbuilder({key, attrs} = input, error)
701
+
whenis_tuple(input) andis_map(attrs) andis_list(key) do
702
+
...
703
+
end
704
+
```
705
+
706
+
16. #### Auto core key
707
+
708
+
Evenif the user transmits the information and it is already in the input, such as with the ID field, the sequence of fields still has to be formed automatically. You can accomplish what you want to with the help of the `auto` option.
709
+
710
+
>As you can see in the code below, we have several types of `auto` option calls
711
+
712
+
---
713
+
714
+
**TODO**: In the next version, it will not be made automatically if there is data for the key that is desired, but it will be feasible to do so if the data exists.
715
+
716
+
>When the core keys are called, the entire primary map is sent to each child.
With the aid of this option, you can make the presence of a field dependent on the presence of another field and, if there is no error, produce an error message.
753
+
754
+
If you pay attention to the routing method, the routing will start from the sent map itself if `:root` is specified, but if it is not used, the routing will start from the received map in the child if it is not used.
755
+
756
+
> When the core keys are called, the entire primary map is sent to each child.
You can select this alternative if you require any data that was delivered in another key to be incorporated into the key that you are looking for. If the key is present, the data associated with it will be copied; however, if the key is not there, the data in and of itself will be retained.
791
+
792
+
If you pay attention to the routing method, the routing will start from the sent map itself if `:root` is specified, but if it is not used, the routing will start from the received map in the child if it is not used.
793
+
794
+
---
795
+
796
+
> When the core keys are called, the entire primary map is sent to each child.
797
+
798
+
> Note: It is possible that you will need to check that the field you wish to duplicate exists, and in order to do so, you can use either the `on` key or the `enforce` option.
0 commit comments