@@ -414,6 +414,14 @@ defmodule NormalDeriveStruct do
414
414
end
415
415
```
416
416
417
+ ``` elixir
418
+ NormalDeriveStruct .builder (%{title: " mishka" })
419
+ ```
420
+
421
+ ``` elixir
422
+ NormalDeriveStruct .builder (%{title: :mishka })
423
+ ```
424
+
417
425
## Extending derive section
418
426
419
427
##### Options
@@ -438,42 +446,53 @@ defmodule TestValidateModule2 do
438
446
end
439
447
end
440
448
441
- defmodule TestSanitizeModule do
449
+ defmodule TestSanitizeModuleOne do
442
450
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
444
452
end
445
453
end
446
454
447
- defmodule TestSanitizeModule2 do
455
+ defmodule TestSanitizeModuleTwo do
448
456
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
450
458
end
451
459
end
460
+ ```
452
461
462
+ ``` elixir
453
463
defmodule NormalExtendedModuleDeriveStruct do
454
464
use GuardedStruct
455
465
456
- guardedstruct validate_derive: TestValidateModule , sanitize_derive: TestSanitizeModule do
466
+ guardedstruct validate_derive: TestValidateModule , sanitize_derive: TestSanitizeModuleOne do
457
467
field (:id , integer (), derive: " sanitize(trim) validate(not_exist)" )
458
468
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)" )
460
470
end
461
471
end
472
+ ```
462
473
463
- # OR you can extend with list of modules
474
+ ``` elixir
475
+ NormalExtendedModuleDeriveStruct .builder (%{name: " Mishka" })
476
+ ```
464
477
478
+ ``` elixir
479
+ # OR you can extend with list of modules
465
480
defmodule NormalExtendedListModuleDeriveStruct do
466
481
use GuardedStruct
467
482
468
- guardedstruct validate_derive: [TestValidateModule , TestValidate2Module ],
469
- sanitize_derive: [TestSanitizeModule , TestSanitizeModule2 ] do
483
+ guardedstruct validate_derive: [TestValidateModule , TestValidateModule2 ],
484
+ sanitize_derive: [TestSanitizeModuleTwo , TestSanitizeModuleOne ] do
470
485
field (:id , integer (), derive: " validate(ineteger)" )
471
- field (:title , String .t (), derive: " sanitize(trim) validate(string)" )
472
486
field (:name , String .t (), derive: " sanitize(capitalize_v2) validate(string)" )
487
+ field (:title , String .t (), derive: " sanitize(trim) validate(string)" )
473
488
end
474
489
end
475
490
```
476
491
492
+ ``` elixir
493
+ NormalExtendedListModuleDeriveStruct .builder (%{name: " Mishka" })
494
+ ```
495
+
477
496
## Struct definition with validator and derive simultaneously
478
497
479
498
``` elixir
@@ -501,6 +520,10 @@ defmodule NormalSimultaneouslyDeriveAndValidatorStruct do
501
520
end
502
521
```
503
522
523
+ ``` elixir
524
+ NormalSimultaneouslyDeriveAndValidatorStruct .builder (%{name: " Mishka" , title: " mishka" })
525
+ ```
526
+
504
527
``` elixir
505
528
# OR with custom validator
506
529
@@ -531,6 +554,14 @@ defmodule NormalSimultaneouslyDeriveWithCustomValidatorStruct do
531
554
end
532
555
```
533
556
557
+ ``` elixir
558
+ NormalSimultaneouslyDeriveWithCustomValidatorStruct .builder (%{name: " mishka" })
559
+ ```
560
+
561
+ ``` elixir
562
+ NormalSimultaneouslyDeriveWithCustomValidatorStruct .builder (%{name: :mishka })
563
+ ```
564
+
534
565
## Define a nested and complex struct
535
566
536
567
``` elixir
@@ -596,6 +627,60 @@ defmodule NestedStruct do
596
627
end
597
628
```
598
629
630
+ ``` elixir
631
+ NestedStruct .builder (%{
632
+ username: " <p>Mishka </p>" ,
633
+ auth: %{
634
+
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
+
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
+
599
684
## Data and Error output sample
600
685
601
686
<!-- livebook:{"force_markdown":true} -->
@@ -705,6 +790,8 @@ If this option is not used, the program will automatically drop fields that are
705
790
706
791
** 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.
707
792
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
+
708
795
``` elixir
709
796
defmodule TestAuthorizeKeys do
710
797
use GuardedStruct
0 commit comments