sinclairzx81 / typebox

Json Schema Type Builder with Static Type Resolution for TypeScript
Other
4.77k stars 152 forks source link

Incorrect symbol escapement in record keys #794

Closed Danksa closed 5 months ago

Danksa commented 5 months ago

Using one of the examples in the "Template Literal Types" section and adding a "$" or "." (or any other character with special meaning in Regex) will cause the properties to have escaped names in the resulting schema:

const K = Type.TemplateLiteral('$prop${A|B|C}'); // Notice the leading $
const R = Type.Record(K, Type.String());

console.log(R)

This outputs the following:

{
   "type":"object",
   "properties":{
      "\\$propA":{
         "type":"string"
      },
      "\\$propB":{
         "type":"string"
      },
      "\\$propC":{
         "type":"string"
      }
   },
   "required":[
      "\\$propA",
      "\\$propB",
      "\\$propC"
   ]
}

The expected output should be the following instead:

{
   "type":"object",
   "properties":{
      "$propA":{
         "type":"string"
      },
      "$propB":{
         "type":"string"
      },
      "$propC":{
         "type":"string"
      }
   },
   "required":[
      "$propA",
      "$propB",
      "$propC"
   ]
}
sinclairzx81 commented 5 months ago

@Danksa Hi, thanks for reporting.

Have pushed a fix for this on 0.32.16. Just note, some of the parsing and control character transformations between strings/regex and TS template literal types is a bit complex. The fix should resolve the issue, but feel free to ping on this thread if you run into any issues.

Cheers! S

Danksa commented 5 months ago

@sinclairzx81 That was quick, thanks a lot!