Skip to content

Strongly typed string formats #61

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

apoco
Copy link

@apoco apoco commented Jul 15, 2023

  • List specific formats supported by ajv
  • Fix security vulnerabilities
  • Fix random typos

apoco added 2 commits July 15, 2023 13:03
- List specific formats supported by ajv
- Fix security vulnerabilities
- Fix random typos
@ostrowr
Copy link
Owner

ostrowr commented Jul 17, 2023

Awesome, thanks!

If you're using this in production, one thing to note is that I'm not actively maintaining this library (though I'm very happy to review PRs) and there may be better alternatives using more recent Typescript features. The problem with using this library in production is that it creates insanely complicated types that substantially slow down compilation.

That said, thanks for the fix, and I'll try to find some time to make some more improvements here soon!

Comment on lines +34 to +36
"@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/eslint-plugin-tslint": "^6.0.0",
"@typescript-eslint/parser": "^6.0.0",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the updates

@@ -8,7 +8,7 @@

// PARTIALLY ENFORCED (🔓) indicates that the field is partially enforced by the type system, but it may be possible
// to assign a type to T that fails validation against s.
// For example, arrays with the additionalItems parameter are PARTIALLY ENFORCED becuase (currently) every element in
// For example, arrays with the additionalItems parameter are PARTIALLY ENFORCED because (currently) every element in
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙏

@@ -235,7 +235,25 @@ export interface Schema<
const?: Const; // 💪
enum?: Enum; // 💪
type?: Type; // 💪
format?: Type extends "string" ? string : never; // ⚠️ only makes sense for string types
format?: Type extends "string"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment referencing where these come from? Assuming this is in the spec, lgtm

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can add later on in my alt account, good call. It's actually not from the spec but from ajv; they don't have a type exposing their default supported formats, unfortunately.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fine; since we're using ajv under the hood let's just point to wherever it's being implemented.

@jpage-godaddy
Copy link

Awesome, thanks!

If you're using this in production, one thing to note is that I'm not actively maintaining this library (though I'm very happy to review PRs) and there may be better alternatives using more recent Typescript features. The problem with using this library in production is that it creates insanely complicated types that substantially slow down compilation.

That said, thanks for the fix, and I'll try to find some time to make some more improvements here soon!

Well it's been quite useful so far. I have a similar never-released code base that does something quite similar, and I know how complicated it can get (and mine was before conditional types were released!). I never figured out how to do references and abandoned the effort.

Know of any alternatives that fill this niche that you'd recommend (short of code generation)? I might use zod since in my case I don't need JSON Schema specifically, but I have other projects where I've considered using your library because I hate having to write two schemas or having a code generation script.

@ostrowr
Copy link
Owner

ostrowr commented Jul 17, 2023

now of any alternatives that fill this niche that you'd recommend (short of code generation)?

Code generation is often a pretty good bet. It can get complicated with json schema but I've had a lot of success with json type definition (https://jsontypedef.com/)

If code generation is out of the question, I have to imagine Zod would work! This project would also definitely work for a small codebase, I just caution you that as it grows you may start running into compiler OOMs.

@ostrowr
Copy link
Owner

ostrowr commented Jul 26, 2023

@apoco mind if I make the requested change and pull this in/cut a release?

@apoco
Copy link
Author

apoco commented Jul 26, 2023 via email

@ostrowr
Copy link
Owner

ostrowr commented Jul 26, 2023

For my own reference for this weekend, this is actually part of the spec (at least the latest version!)

https://json-schema.org/draft/2020-12/json-schema-validation.html#name-defined-formats

I have to read the spec more carefully to determine whether limiting this is actually correct; seems like custom implementations can define their own formats

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

Successfully merging this pull request may close these issues.

3 participants