77 "strconv"
88 "strings"
99
10- validate "github.com/go-playground/validator/v10"
1110 "github.com/projectdiscovery/nuclei/v3/pkg/model"
1211 "github.com/projectdiscovery/nuclei/v3/pkg/protocols"
1312 "github.com/projectdiscovery/nuclei/v3/pkg/protocols/code"
@@ -310,10 +309,8 @@ func (template *Template) validateAllRequestIDs() {
310309// MarshalYAML forces recursive struct validation during marshal operation
311310func (template * Template ) MarshalYAML () ([]byte , error ) {
312311 out , marshalErr := yaml .Marshal (template )
313- // Review: we are adding requestIDs for templateContext
314- // if we are using this method then we might need to purge manually added IDS that start with `templatetype_`
315- // this is only applicable if there are more than 1 request fields in protocol
316- errValidate := validate .New ().Struct (template )
312+ // Use shared validator to avoid rebuilding struct cache for every template marshal
313+ errValidate := tplValidator .Struct (template )
317314 return out , multierr .Append (marshalErr , errValidate )
318315}
319316
@@ -354,7 +351,7 @@ func (template *Template) UnmarshalYAML(unmarshal func(interface{}) error) error
354351 if len (alias .RequestsWithTCP ) > 0 {
355352 template .RequestsNetwork = alias .RequestsWithTCP
356353 }
357- err = validate . New () .Struct (template )
354+ err = tplValidator .Struct (template )
358355 if err != nil {
359356 return err
360357 }
@@ -525,7 +522,7 @@ func (template *Template) hasMultipleRequests() bool {
525522func (template * Template ) MarshalJSON () ([]byte , error ) {
526523 type TemplateAlias Template //avoid recursion
527524 out , marshalErr := json .Marshal ((* TemplateAlias )(template ))
528- errValidate := validate . New () .Struct (template )
525+ errValidate := tplValidator .Struct (template )
529526 return out , multierr .Append (marshalErr , errValidate )
530527}
531528
@@ -538,7 +535,7 @@ func (template *Template) UnmarshalJSON(data []byte) error {
538535 return err
539536 }
540537 * template = Template (* alias )
541- err = validate . New () .Struct (template )
538+ err = tplValidator .Struct (template )
542539 if err != nil {
543540 return err
544541 }
0 commit comments