Skip to content

A way to ignore unknown attributes #41

Description

@deathiop

Hi there,

Cannot label it myself but this is a suggestion.

Building a custom tool based on MJML, I need to include to metadata inside this document.
My way of doing this for now is to include custom attributes in MJML nodes, based on this idea of a soft validation: https://documentation.mjml.io/#validating-mjml
As far as I understand this module, it's currently hardwired to be a strict validator, and rejects any custom attribute:

package main

import (
        "fmt"

        "github.com/preslavrachev/gomjml/mjml"
)

const m = `
<mjml>
 <mj-body>
   <mj-section>
     <mj-column>
       <mj-text custom-attr="some-value" font-family="Helvetica" color="#F45E43">
         <h1>Title</h1>
         <p>Paragraph</p>
         <p style="font-family:Comic Sans Ms">Another paragraph</p>
       </mj-text>
     </mj-column>
   </mj-section>
 </mj-body>
</mjml>
`

func main() {
        r, err := mjml.Render(m)
        if err != nil {
                panic(err)
        }
        fmt.Printf("Rendered:\n%.100s\n", r)
}

results in

panic: MJML compilation error:
        - Line 5 of (mj-text) - Invalid attribute 'custom-attr' for tag <mj-text>
…

I saw that one can add a hook on unknown attribute using

r, err := mjml.Render(m, func(ro *mjml.RenderOpts) {
    ro.InvalidAttributeReporter = func(tagName, attrName string, line int) {
        // do something
    }
})

but this cannot signal an acceptance.
I would have loved to put a return strings.HasPrefix(attrName, "custom-") in there to make it valid.

Is this a kind of feature you would accept?
If so, in what form? This is quite open as it can be this signature change just wrote (returning a bool), accept a whitelist somehow, implement global validation levels as shown in the first link, …

Activity

  1. danielloader commented on Sep 24, 2026

    @danielloader

    I've opened #43 (draft while I finish a round of review fixes) with a possible approach. It takes two independent commits: an opt-in attribute predicate, WithAllowedAttributes, for #41, and a registry passed per render, WithComponents, for #32, whose custom tags expand into built-in MJML. It's additive, and default behaviour is unchanged. Either commit can be taken alone, and it needs your review on the API shape.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions