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

Complete zod plugin with proprietary brands #1730

Merged
merged 19 commits into from
May 9, 2024
Merged

Conversation

RobinTail
Copy link
Owner

@RobinTail RobinTail commented May 5, 2024

Due to #1721, colinhacks/zod#2860 (comment), and #1719 I'm exploring the possibility to alter the behaviour of .brand() for storing the brand property as a way to distinguish the proprietary schemas in runtime.

This can replace the proprietary() function with a call of .brand().

However, so far it turned out to be breaking because ZodBranded does not expose the methods of the wrapped schema, such as .extend(), which is used in accept-raw.ts endpoint for possible route params.

——

After a series of serious considerations I realized that exposing brand to consumers of express-zod-api could be a beneficial feature.
Runtime brand can be accessed via ._def[Symbol.for("express-zod-api")].brand

@RobinTail RobinTail added enhancement New feature or request refactoring The better way to achieve the same result breaking Backward incompatible changes labels May 5, 2024
Copy link

coveralls-official bot commented May 5, 2024

Coverage Status

coverage: 100.0%. remained the same
when pulling 4a6d91b on exp-proprietary-brands
into 234fec6 on make-v19.

src/io-schema.ts Outdated Show resolved Hide resolved
@RobinTail
Copy link
Owner Author

RobinTail commented May 5, 2024

At the moment, despite the beauty of it, I'm thinking that it's not needed to be a part of consumer's API, so it can be kept as an internal method for a while.

At least until I come up with something better for ez.raw(). Maybe a transformation schema.

@RobinTail RobinTail closed this May 5, 2024
@RobinTail
Copy link
Owner Author

UPDATE on raw / extending shape issue:
#1730 (comment)

@RobinTail RobinTail reopened this May 9, 2024
@RobinTail RobinTail added this to the v19 milestone May 9, 2024
@RobinTail RobinTail changed the base branch from master to make-v19 May 9, 2024 09:10
@RobinTail RobinTail mentioned this pull request May 9, 2024
@RobinTail
Copy link
Owner Author

I realized that this can actually be a feature for those who may need it as much as I am.

src/metadata.ts Outdated Show resolved Hide resolved
@RobinTail RobinTail added the miracle Mysterious events are happening here label May 9, 2024
@RobinTail RobinTail changed the title Experiment: proprietary brands Experiment: complete zod plugin with proprietary brands May 9, 2024
@RobinTail RobinTail marked this pull request as ready for review May 9, 2024 12:50
@RobinTail RobinTail changed the title Experiment: complete zod plugin with proprietary brands Complete zod plugin with proprietary brands May 9, 2024
Copy link
Owner Author

@RobinTail RobinTail left a comment

Choose a reason for hiding this comment

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

ok, I'm kinda proud of it

@RobinTail RobinTail merged commit 4ef6ba6 into make-v19 May 9, 2024
11 checks passed
@RobinTail RobinTail deleted the exp-proprietary-brands branch May 9, 2024 14:13
RobinTail added a commit that referenced this pull request May 13, 2024
![image](https://github.com/RobinTail/express-zod-api/assets/13189514/f090c11f-4d73-409a-8546-5c586adb0afc)

Pebbles LaDime (Dime) Doe was a black transgender woman.


https://edition.cnn.com/2024/02/25/us/dime-doe-trial-south-carolina-federal-hate-crime/index.html

https://www.nbcnews.com/feature/nbc-out/south-carolina-death-marks-14th-black-transgender-woman-killed-u-n1040971

https://www.justice.gov/opa/pr/south-carolina-man-found-guilty-hate-crime-killing-transgender-woman-because-her-gender

Transgender women suffer too frequently from transphobic violence and
cruelty, being the less protected social group. I'd like to raise an
awareness of this problem. Humans should be creators — not killers. But
most importantly, I want every transgender girl to have an opportunity
to create applications quickly and, in general, learn to write code
easily in order to receive job offers and leave dangerously transphobic
territories for more favorable and civilized ones, and live happily
there. Protect transgender women.

------------------------------

This version is focused on making `express-zod-api` a complete `zod`
plugin. In this regard `withMeta` is removed in favor of the recommended
approach on extending `zod` functionality, which opens up opportunities
for new features and simplifies the consumer experience. Another
improvement has been made for parsers: they are now applied selectively
depending on the type of expected request.

- #1693 
- #1705 
- #1707 
- Theoretically #1631 
  - In advance #1708 
- #1726 
- #1736 
- #1741 
  - #1745 
  - #1762 
  - #1756 
  - #1766 
- #1730 
  - #1747 
  - #1748 
- #1755 
- #1760 

- Not included, but comes later: #1750 

⚠️ don't squash it, to avoid conflicts with #1750
RobinTail added a commit that referenced this pull request May 14, 2024
…#1750)

Based on #1470  and thanks to #1730 

This will be a feature in v19.
I'd like to keep changes of 19.0.0 smaller, making it easier to migrate.
Thus, I'm going to separate breaking changes from features and release
this as 19.1.0.

This will be the interface:

```ts
import { z } from "zod";
import { Documentation, Integration } from "express-zod-api";

const myBrand = Symbol("MamaToldMeImSpecial"); // I highly recommend using symbols for this purpose
const myBrandedSchema = z.string().brand(myBrand);

new Documentation({
  /* config, routing, title, version */
  brandHandling: {
    [myBrand]: (
      schema: typeof myBrandedSchema, // you should assign type yourself
      { next, path, method, isResponse }, // handle a nested schema using next()
    ) => {
      const defaultResult = next(schema.unwrap()); // { type: string }
      return { summary: "Special type of data" };
    },
  },
});

import ts from "typescript";
const { factory: f } = ts;

new Integration({
  /* routing */
  brandHandling: {
    [myBrand]: (
      schema: typeof myBrandedSchema, // you should assign type yourself
      { next, isResponse, serializer }, // handle a nested schema using next()
    ) => f.createKeywordTypeNode(ts.SyntaxKind.BooleanKeyword),
  },
});
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking Backward incompatible changes enhancement New feature or request miracle Mysterious events are happening here refactoring The better way to achieve the same result
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant