You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was playing around with the parser.ts and came up with something like this in the parseSchema function:
letsinglePatternProperty=falseif(schema.patternProperties){// partially support patternProperties. in the case that// additionalProperties is not set, and there is only a single// value definition, we can validate against that.singlePatternProperty=!schema.additionalProperties&&Object.keys(schema.patternProperties).length===1asts=asts.concat(map(schema.patternProperties,(value,key: string)=>{constast=parse(value,options,key,processed,usedNames)constcomment=`This interface was referenced by \`${parentSchemaName}\`'s JSON-Schema definitionvia the \`patternProperty\` "${key}".`ast.comment=ast.comment ? `${ast.comment}\n\n${comment}` : commentletkeyName=singlePatternProperty ? '[k: string]' : key// find the literals that can be used to make up the "or"constliteralsMatch=key.match(/(?<keyPart>.*)\((?<literals>(\w+\|?)*)\)/)if(literalsMatch&&literalsMatch.groups?.literals&&singlePatternProperty){constliterals=literalsMatch.groups.literals.split('|')keyName=`[k in \`${literalsMatch.groups.keyPart}\$\{${literals.map(k=>`"${k}"`).join('|')}\}\`]`}return{
ast,isPatternProperty: !singlePatternProperty,isRequired: singlePatternProperty||includes(schema.required||[],key),isUnreachableDefinition: false,keyName: keyName}}))}
This is working (after changing the generator's escapeKeyName function a bit), but I'm sure I'm missing some obvious use cases where this approach can be improved. While this would work for my specific case I would like to get it working for some other applicable cases and prevent it from breaking current cases.
Any feedback/suggestions would be appreciated :)
The text was updated successfully, but these errors were encountered:
UPDATE: A major key that I'm missing is how the library determines if the type should be generated as a type or interface. The implementation that I have works for nested properties, but not standalone properties because they need to be exported as type. @bcherny could you give me an idea of how I could tell the generator to make these properties their own types and join them by unions?
I have an schema like:
This can be transferred into a template literal structure in typescript like:
I was playing around with the
parser.ts
and came up with something like this in the parseSchema function:This is working (after changing the generator's
escapeKeyName
function a bit), but I'm sure I'm missing some obvious use cases where this approach can be improved. While this would work for my specific case I would like to get it working for some other applicable cases and prevent it from breaking current cases.Any feedback/suggestions would be appreciated :)
The text was updated successfully, but these errors were encountered: