File tree Expand file tree Collapse file tree 2 files changed +68
-1
lines changed Expand file tree Collapse file tree 2 files changed +68
-1
lines changed Original file line number Diff line number Diff line change @@ -81,6 +81,13 @@ def with(default_properties)
81
81
)
82
82
end
83
83
84
+ def validation_schema ( validation_schema )
85
+ self . class . new (
86
+ @service_name ,
87
+ **@args . merge ( validation_schema : validation_schema )
88
+ )
89
+ end
90
+
84
91
def context_provider ( &blk )
85
92
new_context_providers = Array ( @args [ :context_providers ] )
86
93
new_context_providers << blk
Original file line number Diff line number Diff line change 679
679
end
680
680
end
681
681
682
+ describe '#validation_schema' do
683
+ it 'allows for reconfiguring the validation_schema on new logger instances' do
684
+ validation_schema = <<-JSON
685
+ {
686
+ "type": "object",
687
+ "required": ["pet"],
688
+ "properties": {
689
+ "pet": {
690
+ "type": "object",
691
+ "required": ["name", "best_boy_or_girl?"],
692
+ "properties": {
693
+ "name": {
694
+ "type": "string",
695
+ "minLength": 1
696
+ },
697
+ "best_boy_or_girl?": {
698
+ "type": "boolean"
699
+ }
700
+ }
701
+ }
702
+ }
703
+ }
704
+ JSON
705
+
706
+ logger = Twiglet ::Logger . new (
707
+ 'petshop' ,
708
+ now : @now ,
709
+ output : @buffer
710
+ )
711
+
712
+ message = { message : 'hi' }
713
+
714
+ pet_message = {
715
+ pet : { name : 'Davis' , best_boy_or_girl? : true , species : 'dog' }
716
+ }
717
+
718
+ logger . info ( message )
719
+ log = read_json ( @buffer )
720
+ assert_equal 'hi' , log [ :message ]
721
+
722
+ error = assert_raises JSON ::Schema ::ValidationError do
723
+ logger . info ( pet_message )
724
+ end
725
+ assert_equal "The property '#/' did not contain a required property of 'message'" , error . message
726
+
727
+ logger = logger . validation_schema ( validation_schema )
728
+
729
+ error = assert_raises JSON ::Schema ::ValidationError do
730
+ logger . info ( message )
731
+ end
732
+ assert_equal "The property '#/' did not contain a required property of 'pet'" , error . message
733
+
734
+ logger . info ( pet_message )
735
+ log = read_json ( @buffer )
736
+ assert_equal 'Davis' , log [ :pet ] [ :name ]
737
+ end
738
+ end
739
+
682
740
private
683
741
684
742
def read_json ( buffer )
685
743
buffer . rewind
686
- JSON . parse ( buffer . read , symbolize_names : true )
744
+ string = buffer . read
745
+ buffer . rewind
746
+ JSON . parse ( string , symbolize_names : true )
687
747
end
688
748
end
689
749
# rubocop:enable Metrics/BlockLength
You can’t perform that action at this time.
0 commit comments