@@ -6,6 +6,8 @@ use jsonschema::{BasicOutput, Validator};
6
6
use serde_json:: { json, Value } ;
7
7
use tracing:: error;
8
8
9
+ use crate :: utils:: schema:: { extract_json_schema_for, SCHEMA_VERSION } ;
10
+
9
11
/// Configuration key
10
12
#[ derive( Debug , Clone , PartialEq ) ]
11
13
pub ( crate ) enum ConfigKey {
@@ -24,9 +26,9 @@ impl Display for ConfigKey {
24
26
}
25
27
}
26
28
27
- /// Frontend schema.
28
- static FRONTEND_SCHEMA : LazyLock < Value > =
29
- LazyLock :: new ( || load_json_lazy ( include_str ! ( "jsonschema/frontend.json" ) ) ) ;
29
+ /// Frontend schema from API specification .
30
+ pub ( crate ) static FRONTEND_SCHEMA : LazyLock < Value > =
31
+ LazyLock :: new ( || extract_json_schema_for ( "FrontendConfig" ) ) ;
30
32
31
33
/// Frontend schema validator.
32
34
static FRONTEND_SCHEMA_VALIDATOR : LazyLock < Validator > =
@@ -43,28 +45,35 @@ static FRONTEND_IP_DEFAULT: LazyLock<Value> =
43
45
/// Helper function to create a JSON validator from a JSON schema.
44
46
/// If the schema is invalid, a default JSON validator is created.
45
47
fn schema_validator ( schema : & Value ) -> Validator {
46
- jsonschema:: validator_for ( schema) . unwrap_or_else ( |err| {
47
- error ! (
48
- id = "schema_validator" ,
49
- error=?err,
50
- "Error creating JSON validator"
51
- ) ;
52
-
53
- // Create a default JSON validator as a fallback
54
- // This should not fail since it is hard coded
55
- #[ allow( clippy:: expect_used) ]
56
- Validator :: new ( & json ! ( {
57
- "$schema" : "http://json-schema.org/draft-07/schema#" ,
58
- "type" : "object"
59
- } ) )
60
- . expect ( "Failed to create default JSON validator" )
61
- } )
48
+ Validator :: options ( )
49
+ . with_draft ( jsonschema:: Draft :: Draft202012 )
50
+ . build ( schema)
51
+ . unwrap_or_else ( |err| {
52
+ error ! (
53
+ id="schema_validator" ,
54
+ error=?err,
55
+ "Error creating JSON validator"
56
+ ) ;
57
+
58
+ default_validator ( )
59
+ } )
60
+ }
61
+
62
+ /// Create a default JSON validator as a fallback
63
+ /// This should not fail since it is hard coded
64
+ fn default_validator ( ) -> Validator {
65
+ #[ allow( clippy:: expect_used) ]
66
+ Validator :: new ( & json ! ( {
67
+ "$schema" : SCHEMA_VERSION ,
68
+ "type" : "object"
69
+ } ) )
70
+ . expect ( "Failed to create default JSON validator" )
62
71
}
63
72
64
73
/// Helper function to convert a JSON string to a JSON value.
65
74
fn load_json_lazy ( data : & str ) -> Value {
66
75
serde_json:: from_str ( data) . unwrap_or_else ( |err| {
67
- error ! ( id = "load_json_lazy" , error=?err, "Error parsing JSON" ) ;
76
+ error ! ( id= "load_json_lazy" , error=?err, "Error parsing JSON" ) ;
68
77
json ! ( { } )
69
78
} )
70
79
}
@@ -115,15 +124,43 @@ mod tests {
115
124
use super :: * ;
116
125
117
126
#[ test]
118
- fn test_valid_validate ( ) {
127
+ fn test_schema_for_schema ( ) {
128
+ // Invalid schema
129
+ let invalid_schema = json ! ( {
130
+ "title" : "Invalid Example Schema" ,
131
+ "type" : "object" ,
132
+
133
+ "properties" : {
134
+ "invalidProperty" : {
135
+ "type" : "unknownType"
136
+ }
137
+ } ,
138
+
139
+ } ) ;
140
+ // This should not fail
141
+ schema_validator ( & invalid_schema) ;
142
+ }
143
+
144
+ #[ test]
145
+ fn test_valid_validate_1 ( ) {
119
146
let value = json ! ( {
120
- "test" : "test"
147
+ "sentry" : {
148
+ "dsn" : "https://test.com"
149
+ }
121
150
} ) ;
122
151
let result = ConfigKey :: Frontend . validate ( & value) ;
123
152
assert ! ( result. is_valid( ) ) ;
124
153
println ! ( "{:?}" , serde_json:: to_value( result) . unwrap( ) ) ;
125
154
}
126
155
156
+ #[ test]
157
+ fn test_valid_validate_2 ( ) {
158
+ let value = json ! ( { } ) ;
159
+ let result = ConfigKey :: Frontend . validate ( & value) ;
160
+ assert ! ( result. is_valid( ) ) ;
161
+ println ! ( "{:?}" , serde_json:: to_value( result) . unwrap( ) ) ;
162
+ }
163
+
127
164
#[ test]
128
165
fn test_invalid_validate ( ) {
129
166
let value = json ! ( [ ] ) ;
@@ -137,4 +174,13 @@ mod tests {
137
174
let result = ConfigKey :: Frontend . default ( ) ;
138
175
assert ! ( result. is_object( ) ) ;
139
176
}
177
+
178
+ #[ test]
179
+ fn test_default_validator ( ) {
180
+ let result = std:: panic:: catch_unwind ( || {
181
+ default_validator ( ) ;
182
+ } ) ;
183
+ // Assert that no panic occurred
184
+ assert ! ( result. is_ok( ) , "default_validator panicked" ) ;
185
+ }
140
186
}
0 commit comments