]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
resources.functions: improve validation
authorAndreas Deininger <adeininger@urbanonline.de>
Thu, 16 Mar 2023 15:56:05 +0000 (16:56 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 20 Mar 2023 13:53:57 +0000 (14:53 +0100)
resources/resource_transformers/integrity/integrity.go
tpl/resources/resources.go

index e1575468581e5d90df370f07ae3936080b5f3ffa..3a7db4c80782ab2c54041f5bdadb4e1e2f2d721b 100644 (file)
@@ -91,7 +91,7 @@ func newHash(algo string) (hash.Hash, error) {
        case "sha512":
                return sha512.New(), nil
        default:
-               return nil, fmt.Errorf("unsupported crypto algo: %q, use either md5, sha256, sha384 or sha512", algo)
+               return nil, fmt.Errorf("unsupported hash algorithm: %q, use either md5, sha256, sha384 or sha512", algo)
        }
 }
 
index cc5310277f7ed19982dd345cce9df9439aa7206a..d18797ebcb0fd22715f394192c13812f1e9492a0 100644 (file)
@@ -145,7 +145,7 @@ func (ns *Namespace) Get(filename any) resource.Resource {
 //
 // A second argument may be provided with an option map.
 //
-// Note: This method does not return any error as a second argument,
+// Note: This method does not return any error as a second return value,
 // for any error situations the error can be checked in .Err.
 func (ns *Namespace) GetRemote(args ...any) resource.Resource {
        get := func(args ...any) (resource.Resource, error) {
@@ -153,6 +153,10 @@ func (ns *Namespace) GetRemote(args ...any) resource.Resource {
                        return nil, errors.New("must provide an URL")
                }
 
+               if len(args) > 2 {
+                       return nil, errors.New("must not provide more arguments than URL and options")
+               }
+
                urlstr, err := cast.ToStringE(args[0])
                if err != nil {
                        return nil, err
@@ -302,8 +306,12 @@ func (ns *Namespace) ExecuteAsTemplate(ctx context.Context, args ...any) (resour
 // Fingerprint transforms the given Resource with a MD5 hash of the content in
 // the RelPermalink and Permalink.
 func (ns *Namespace) Fingerprint(args ...any) (resource.Resource, error) {
-       if len(args) < 1 || len(args) > 2 {
-               return nil, errors.New("must provide a Resource and (optional) crypto algo")
+       if len(args) < 1 {
+               return nil, errors.New("must provide a Resource object")
+       }
+
+       if len(args) > 2 {
+               return nil, errors.New("must not provide more arguments than Resource and hash algorithm")
        }
 
        var algo string
@@ -332,9 +340,15 @@ func (ns *Namespace) Minify(r resources.ResourceTransformer) (resource.Resource,
        return ns.minifyClient.Minify(r)
 }
 
-// ToCSS converts the given Resource to CSS. You can optional provide an Options
-// object or a target path (string) as first argument.
+// ToCSS converts the given Resource to CSS. You can optional provide an Options object
+// as second argument. As an option, you can e.g. specify e.g. the target path (string)
+// for the converted CSS resource.
 func (ns *Namespace) ToCSS(args ...any) (resource.Resource, error) {
+
+       if len(args) > 2 {
+               return nil, errors.New("must not provide more arguments than resource object and options")
+       }
+
        const (
                // Transpiler implementation can be controlled from the client by
                // setting the 'transpiler' option.
@@ -404,6 +418,11 @@ func (ns *Namespace) ToCSS(args ...any) (resource.Resource, error) {
 
 // PostCSS processes the given Resource with PostCSS
 func (ns *Namespace) PostCSS(args ...any) (resource.Resource, error) {
+
+       if len(args) > 2 {
+               return nil, errors.New("must not provide more arguments than resource object and options")
+       }
+
        r, m, err := resourcehelpers.ResolveArgs(args)
        if err != nil {
                return nil, err
@@ -419,6 +438,11 @@ func (ns *Namespace) PostProcess(r resource.Resource) (postpub.PostPublishedReso
 
 // Babel processes the given Resource with Babel.
 func (ns *Namespace) Babel(args ...any) (resource.Resource, error) {
+
+       if len(args) > 2 {
+               return nil, errors.New("must not provide more arguments than resource object and options")
+       }
+
        r, m, err := resourcehelpers.ResolveArgs(args)
        if err != nil {
                return nil, err