Skip to content

Commit f377033

Browse files
committed
Add some docs for external derives in livebook
1 parent a62b00d commit f377033

File tree

1 file changed

+97
-10
lines changed

1 file changed

+97
-10
lines changed

guidance/guarded-struct.livemd

Lines changed: 97 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,14 @@ defmodule NormalDeriveStruct do
414414
end
415415
```
416416

417+
```elixir
418+
NormalDeriveStruct.builder(%{title: "mishka"})
419+
```
420+
421+
```elixir
422+
NormalDeriveStruct.builder(%{title: :mishka})
423+
```
424+
417425
## Extending derive section
418426

419427
##### Options
@@ -438,42 +446,53 @@ defmodule TestValidateModule2 do
438446
end
439447
end
440448

441-
defmodule TestSanitizeModule do
449+
defmodule TestSanitizeModuleOne do
442450
def sanitize(:capitalize_v1, input) do
443-
if is_binary(input), do: String.capitalize(input), else: input
451+
if is_binary(input), do: String.capitalize(input) <> "::_v1", else: input
444452
end
445453
end
446454

447-
defmodule TestSanitizeModule2 do
455+
defmodule TestSanitizeModuleTwo do
448456
def sanitize(:capitalize_v2, input) do
449-
if is_binary(input), do: String.capitalize(input), else: input
457+
if is_binary(input), do: String.capitalize(input) <> "::_v2", else: input
450458
end
451459
end
460+
```
452461

462+
```elixir
453463
defmodule NormalExtendedModuleDeriveStruct do
454464
use GuardedStruct
455465

456-
guardedstruct validate_derive: TestValidateModule, sanitize_derive: TestSanitizeModule do
466+
guardedstruct validate_derive: TestValidateModule, sanitize_derive: TestSanitizeModuleOne do
457467
field(:id, integer(), derive: "sanitize(trim) validate(not_exist)")
458468
field(:title, String.t(), derive: "sanitize(trim) validate(string)")
459-
field(:name, String.t(), derive: "sanitize(capitalize_v2) validate(string)")
469+
field(:name, String.t(), derive: "sanitize(capitalize_v1) validate(string)")
460470
end
461471
end
472+
```
462473

463-
# OR you can extend with list of modules
474+
```elixir
475+
NormalExtendedModuleDeriveStruct.builder(%{name: "Mishka"})
476+
```
464477

478+
```elixir
479+
# OR you can extend with list of modules
465480
defmodule NormalExtendedListModuleDeriveStruct do
466481
use GuardedStruct
467482

468-
guardedstruct validate_derive: [TestValidateModule, TestValidate2Module],
469-
sanitize_derive: [TestSanitizeModule, TestSanitizeModule2] do
483+
guardedstruct validate_derive: [TestValidateModule, TestValidateModule2],
484+
sanitize_derive: [TestSanitizeModuleTwo, TestSanitizeModuleOne] do
470485
field(:id, integer(), derive: "validate(ineteger)")
471-
field(:title, String.t(), derive: "sanitize(trim) validate(string)")
472486
field(:name, String.t(), derive: "sanitize(capitalize_v2) validate(string)")
487+
field(:title, String.t(), derive: "sanitize(trim) validate(string)")
473488
end
474489
end
475490
```
476491

492+
```elixir
493+
NormalExtendedListModuleDeriveStruct.builder(%{name: "Mishka"})
494+
```
495+
477496
## Struct definition with validator and derive simultaneously
478497

479498
```elixir
@@ -501,6 +520,10 @@ defmodule NormalSimultaneouslyDeriveAndValidatorStruct do
501520
end
502521
```
503522

523+
```elixir
524+
NormalSimultaneouslyDeriveAndValidatorStruct.builder(%{name: "Mishka", title: "mishka"})
525+
```
526+
504527
```elixir
505528
# OR with custom validator
506529

@@ -531,6 +554,14 @@ defmodule NormalSimultaneouslyDeriveWithCustomValidatorStruct do
531554
end
532555
```
533556

557+
```elixir
558+
NormalSimultaneouslyDeriveWithCustomValidatorStruct.builder(%{name: "mishka"})
559+
```
560+
561+
```elixir
562+
NormalSimultaneouslyDeriveWithCustomValidatorStruct.builder(%{name: :mishka})
563+
```
564+
534565
## Define a nested and complex struct
535566

536567
```elixir
@@ -596,6 +627,60 @@ defmodule NestedStruct do
596627
end
597628
```
598629

630+
```elixir
631+
NestedStruct.builder(%{
632+
username: " <p>Mishka </p>",
633+
auth: %{
634+
server: "[email protected]",
635+
identity_provider: "google",
636+
role: %{
637+
name: :user,
638+
action: "true",
639+
status: %{status: 2}
640+
},
641+
last_activity: "2023-08-20 16:54:07.841434Z"
642+
},
643+
age: 18,
644+
family: "group",
645+
name: "mishka",
646+
profile: %{
647+
site: "https://elixir-lang.org",
648+
nickname: "mishka"
649+
}
650+
})
651+
```
652+
653+
```elixir
654+
NestedStruct.builder(%{
655+
username: "mishka",
656+
auth: %{
657+
server: "[email protected]",
658+
identity_provider: "google",
659+
role: %{
660+
name: :admin,
661+
action: "test",
662+
status: %{status: 2}
663+
},
664+
last_activity: "20213-08-20 16:54:07.841434Z"
665+
},
666+
age: 18,
667+
family: "group",
668+
name: "mishka",
669+
profile: %{
670+
site: "https://elixir-lang.org",
671+
nickname: :test
672+
}
673+
})
674+
```
675+
676+
```elixir
677+
NestedStruct.keys() |> IO.inspect()
678+
NestedStruct.keys(:profile) |> IO.inspect()
679+
NestedStruct.__information__() |> IO.inspect()
680+
NestedStruct.enforce_keys() |> IO.inspect()
681+
NestedStruct.enforce_keys(:profile) |> IO.inspect()
682+
```
683+
599684
## Data and Error output sample
600685

601686
<!-- livebook:{"force_markdown":true} -->
@@ -705,6 +790,8 @@ If this option is not used, the program will automatically drop fields that are
705790

706791
**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.
707792

793+
**Security note**: The assumption of the correctness of this function is that you must enter the name of each field correctly (as a map key), for example, Phoenix should consider a layer of care for the names of the fields, otherwise, a security problem may arise.
794+
708795
```elixir
709796
defmodule TestAuthorizeKeys do
710797
use GuardedStruct

0 commit comments

Comments
 (0)