mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-16 21:56:40 -05:00
cmd: Strict unmarshal for validate (#5383)
This commit is contained in:
parent
8bc05e598d
commit
79de6df93d
4 changed files with 6 additions and 6 deletions
2
caddy.go
2
caddy.go
|
@ -314,7 +314,7 @@ func unsyncedDecodeAndRun(cfgJSON []byte, allowPersist bool) error {
|
||||||
strippedCfgJSON := RemoveMetaFields(cfgJSON)
|
strippedCfgJSON := RemoveMetaFields(cfgJSON)
|
||||||
|
|
||||||
var newCfg *Config
|
var newCfg *Config
|
||||||
err := strictUnmarshalJSON(strippedCfgJSON, &newCfg)
|
err := StrictUnmarshalJSON(strippedCfgJSON, &newCfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -490,7 +490,7 @@ func cmdAdaptConfig(fl Flags) (int, error) {
|
||||||
// validate output if requested
|
// validate output if requested
|
||||||
if adaptCmdValidateFlag {
|
if adaptCmdValidateFlag {
|
||||||
var cfg *caddy.Config
|
var cfg *caddy.Config
|
||||||
err = json.Unmarshal(adaptedConfig, &cfg)
|
err = caddy.StrictUnmarshalJSON(adaptedConfig, &cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return caddy.ExitCodeFailedStartup, fmt.Errorf("decoding config: %v", err)
|
return caddy.ExitCodeFailedStartup, fmt.Errorf("decoding config: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -523,7 +523,7 @@ func cmdValidateConfig(fl Flags) (int, error) {
|
||||||
input = caddy.RemoveMetaFields(input)
|
input = caddy.RemoveMetaFields(input)
|
||||||
|
|
||||||
var cfg *caddy.Config
|
var cfg *caddy.Config
|
||||||
err = json.Unmarshal(input, &cfg)
|
err = caddy.StrictUnmarshalJSON(input, &cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return caddy.ExitCodeFailedStartup, fmt.Errorf("decoding config: %v", err)
|
return caddy.ExitCodeFailedStartup, fmt.Errorf("decoding config: %v", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -326,7 +326,7 @@ func (ctx Context) LoadModuleByID(id string, rawMsg json.RawMessage) (any, error
|
||||||
|
|
||||||
// fill in its config only if there is a config to fill in
|
// fill in its config only if there is a config to fill in
|
||||||
if len(rawMsg) > 0 {
|
if len(rawMsg) > 0 {
|
||||||
err := strictUnmarshalJSON(rawMsg, &val)
|
err := StrictUnmarshalJSON(rawMsg, &val)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("decoding module config: %s: %v", modInfo, err)
|
return nil, fmt.Errorf("decoding module config: %s: %v", modInfo, err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -333,11 +333,11 @@ func ParseStructTag(tag string) (map[string]string, error) {
|
||||||
return results, nil
|
return results, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// strictUnmarshalJSON is like json.Unmarshal but returns an error
|
// StrictUnmarshalJSON is like json.Unmarshal but returns an error
|
||||||
// if any of the fields are unrecognized. Useful when decoding
|
// if any of the fields are unrecognized. Useful when decoding
|
||||||
// module configurations, where you want to be more sure they're
|
// module configurations, where you want to be more sure they're
|
||||||
// correct.
|
// correct.
|
||||||
func strictUnmarshalJSON(data []byte, v any) error {
|
func StrictUnmarshalJSON(data []byte, v any) error {
|
||||||
dec := json.NewDecoder(bytes.NewReader(data))
|
dec := json.NewDecoder(bytes.NewReader(data))
|
||||||
dec.DisallowUnknownFields()
|
dec.DisallowUnknownFields()
|
||||||
return dec.Decode(v)
|
return dec.Decode(v)
|
||||||
|
|
Loading…
Reference in a new issue