Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Convert object patternProperties string regex to Record<string, type> #341

Open
smiley-uriux opened this issue Nov 30, 2022 · 3 comments

Comments

@smiley-uriux
Copy link
Contributor

I've often validated types like Record<string, SomeInterface> using json schema's "patternProperties" option with the regular expression ^.*$. Currently oazapfts converts a spec like:

{
  "type": "object",
  "patternProperties": {
    "^.*$": {
      "type": "object",
      "properties": {
        "name": {
          "type": "string"
        }
      }
    },
    "required": [
      "name"
    ]
  },
  "additionalProperties": false
}

to object when ideally it could produce Record<string, { name: string }>

Checking for a specific regular expression string like ^.*$ feels a little wonky, but then again, this approach is fairly ubiquitous in the wild. Libraries like typebox convert Record<string, SomeInterface> to exactly this and I think support could be included with very little code and no breaking changes. If the type included any other regular expression for patternProperties or the top level object had any explicitly defined properties, the existing behavior would be preserved.

Would a PR supporting this feature get merged?

@Xiphe
Copy link
Collaborator

Xiphe commented Nov 30, 2022

That sounds interesting 🤔

I will need to read up on patternproperties to fully wrap my head around this.

To further understand your proposal, are you using ^.*$ as an example here or will the code only work when using literally "^.*$" as the patternProperty regex?

@smiley-uriux
Copy link
Contributor Author

smiley-uriux commented Nov 30, 2022

Yeah so patternProperties just provides a way for you to enforce object property value types based on dynamic property key names that aren't known in advance. The JSON spec only provides support for regular expressions, so the convention is to use a regex like ^.*$ to basically mean any string key which provides a way to map to a typescript type of Record<string, SomeType>. That said, a regular expression like .* would work equally well so perhaps we could support a few popular variations.

Again this transformation would only be applied in this specific case - when patternProperties is set to exactly one known regular expression that means "any string" and no other object properties are explicitly specified.

Despite types of the form Record<string, SomeType> being incredibly common in typescript, I realize this probably feels like a pretty specialized case and may be beyond the scope of what this library is trying to achieve, however it appears it could be supported with minimal changes to the current code base. Long term I'd love to see this library provide a richer extension system so that people could implement these types of specialized cases without relying on the core.

@Xiphe
Copy link
Collaborator

Xiphe commented Dec 1, 2022

Thank you for elaborating. As of now I'd say: Not this year since I'll be minimizing OSS work during December.

In general I agree with your points. I would also like to see a plugin system (ref #293)

If it only requires minimal changes to the codebase it would help me access the impact of the change if you could provide a PR. I can't promise I'll be able to merge it soon though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants