lemminx icon indicating copy to clipboard operation
lemminx copied to clipboard

How to accept syntax of Mustache templates

Open weilbith opened this issue 3 years ago • 8 comments

Hey 👋🏾

Unfortunately I can't open a discussion, so I use an issue to ask for help. I hope this is okay. 🙈

I have an XML file as a Mustache template. The problem is that the template syntax is causing diagnostic errors for the pattern validation of certain attributes. So for example I have something like <element attribute="{{value}}" /> and it complains that {{value}} is incorrect for this attribute. Is there an option to make this work?

Thank you very much! 🙏🏾

weilbith avatar Dec 29 '22 10:12 weilbith

Is your xml file is associated with a dtd, xsd?

I fear that it is not possible to ignore your error. An idea that I had is to ignore validation error for some given error code declared in settings. I think it could resolve your issue.

angelozerr avatar Dec 29 '22 15:12 angelozerr

Yes, it is associated with an xsd.

How would I do that? 🙈

weilbith avatar Dec 29 '22 16:12 weilbith

Could you share your xml and xsd please.

angelozerr avatar Dec 29 '22 17:12 angelozerr

I'm afraid I can't 🙈 Only working professionally on XML and that is private. 😓 I'm very thankful for your help. I'll try to ignore certain errors for this project. Thank you! 🙏🏾

weilbith avatar Jan 03 '23 14:01 weilbith

I reopen the issue because I think we can provide the capability to ignore error code while validation.

Is not possible to provide a simple XSD which defines the rules wich doesn't allow mustache syntax? Or perhaps you are trying to use mustache syntax in XSD schema?

angelozerr avatar Jan 03 '23 14:01 angelozerr

Well, here is a simple type definition:

    <xs:complexType name="foo" mixed="true">
       <!-- ... -->
      <xs:attribute name="eId" use="required" type="eIdLiterals"/>
   </xs:complexType>

   <!-- ... -->
   <xs:simpleType name="eIdLiterals">
      <xs:annotation>
         <xs:documentation>...</xs:documentation>
      </xs:annotation>
      <xs:restriction base="xs:string">
         <xs:pattern value="(([a-z0-9]+-[a-z0-9]+(\.[a-z0-9]+)*)_)*(para)-[a-z0-9]+(\.[a-z0-9]+)*"/>
      </xs:restriction>
   </xs:simpleType>

Having now as template:

<foo eId="{{identifier}}" />

It tells me that {{identifier}} is not valid for the pattern (([a-z0-9]+-[a-z0-9]+(\.[a-z0-9]+)*)_)*(para)-[a-z0-9]+(\.[a-z0-9]+)*.

weilbith avatar Jan 03 '23 15:01 weilbith

Ok thanks for your sample.

Here the full sample:

  • mustache.xml :
<?xml-model href="mustache.xsd"?>
<foo eId="{{identifier}}" ></foo>
  • mustache.xsd :
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:element name="foo" type="foo" >
	</xs:element>

	<xs:complexType name="foo" mixed="true">
       <!-- ... -->
      <xs:attribute name="eId" use="required" type="eIdLiterals"/>
   </xs:complexType>

   <!-- ... -->
   <xs:simpleType name="eIdLiterals">
      <xs:annotation>
         <xs:documentation>...</xs:documentation>
      </xs:annotation>
      <xs:restriction base="xs:string">
         <xs:pattern value="(([a-z0-9]+-[a-z0-9]+(\.[a-z0-9]+)*)_)*(para)-[a-z0-9]+(\.[a-z0-9]+)*"/>
      </xs:restriction>
   </xs:simpleType>

</xs:schema>

There are 2 errors:

image

I think we should improve https://github.com/redhat-developer/vscode-xml/blob/main/docs/Validation.md#xmlvalidationfilters to support this kind of usecase to ignore some error code.

A basic idea is to provide an array of error code to manage severity. In your case you could configure cvc-pattern-valid and cvc-attribute.3 as ignore severity.

An another idea is to provide the capability to ignore any error if the attribute value matches a regexp. In your case, you could configure this settings by writting a regular expression of mustache syntax.

I don't know when we could support that, but I wanted just to share my ideas.

angelozerr avatar Jan 03 '23 15:01 angelozerr

Thanks for constructing a full proper example out of my input. :+1:

Reads good! 🙂

weilbith avatar Jan 03 '23 16:01 weilbith