Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 13, 05:30 UTC
cmd: Strict unmarshal for validate (#5383)
Francis Lavoie committed
Feb 22, 2023 at 13:39 UTC
79de6df93d0404790c3bfecfefa9e1458ffcff75
4 files changed
+6
-6
caddy.go
+1
-1
@@ -314,7 +314,7 @@ func unsyncedDecodeAndRun(cfgJSON []byte, allowPersist bool) error {
314
strippedCfgJSON := RemoveMetaFields(cfgJSON)
315
316
var newCfg *Config
317
- err := strictUnmarshalJSON(strippedCfgJSON, &newCfg)
317
+ err := StrictUnmarshalJSON(strippedCfgJSON, &newCfg)
318
if err != nil {
319
return err
320
}
cmd/commandfuncs.go
+2
-2
@@ -490,7 +490,7 @@ func cmdAdaptConfig(fl Flags) (int, error) {
490
// validate output if requested
491
if adaptCmdValidateFlag {
492
var cfg *caddy.Config
493
- err = json.Unmarshal(adaptedConfig, &cfg)
493
+ err = caddy.StrictUnmarshalJSON(adaptedConfig, &cfg)
494
if err != nil {
495
return caddy.ExitCodeFailedStartup, fmt.Errorf("decoding config: %v", err)
496
}
@@ -523,7 +523,7 @@ func cmdValidateConfig(fl Flags) (int, error) {
523
input = caddy.RemoveMetaFields(input)
524
525
var cfg *caddy.Config
526
- err = json.Unmarshal(input, &cfg)
526
+ err = caddy.StrictUnmarshalJSON(input, &cfg)
527
if err != nil {
528
return caddy.ExitCodeFailedStartup, fmt.Errorf("decoding config: %v", err)
529
}
context.go
+1
-1
@@ -326,7 +326,7 @@ func (ctx Context) LoadModuleByID(id string, rawMsg json.RawMessage) (any, error
326
327
// fill in its config only if there is a config to fill in
328
if len(rawMsg) > 0 {
329
- err := strictUnmarshalJSON(rawMsg, &val)
329
+ err := StrictUnmarshalJSON(rawMsg, &val)
330
if err != nil {
331
return nil, fmt.Errorf("decoding module config: %s: %v", modInfo, err)
332
}
modules.go
+2
-2
@@ -333,11 +333,11 @@ func ParseStructTag(tag string) (map[string]string, error) {
333
return results, nil
334
}
335
336
-// strictUnmarshalJSON is like json.Unmarshal but returns an error
336
+// StrictUnmarshalJSON is like json.Unmarshal but returns an error
337
// if any of the fields are unrecognized. Useful when decoding
338
// module configurations, where you want to be more sure they're
339
// correct.
340
-func strictUnmarshalJSON(data []byte, v any) error {
340
+func StrictUnmarshalJSON(data []byte, v any) error {
341
dec := json.NewDecoder(bytes.NewReader(data))
342
dec.DisallowUnknownFields()
343
return dec.Decode(v)