Skip to content

implementation with myzod #25

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

Closed
Parables opened this issue Mar 31, 2022 · 12 comments
Closed

implementation with myzod #25

Parables opened this issue Mar 31, 2022 · 12 comments
Labels
good first issue Good for newcomers

Comments

@Parables
Copy link
Contributor

I really love the plugin so much.....

Please can we have an implementation with myzod.

@github-actions github-actions bot added the good first issue Good for newcomers label Mar 31, 2022
@Code-Hex
Copy link
Owner

Code-Hex commented Apr 2, 2022

Thanks your feedback.
myzod does not seem to provide the extend API.

davidmdm/myzod#51

yup and zod provide it. So we can modify schema if generated a little bit different. (I announced tips here. https://github.com/Code-Hex/graphql-codegen-typescript-validation-schema/tree/main/example/zod)

Isn't this inconvenient?

@Parables
Copy link
Contributor Author

Parables commented Apr 2, 2022

I haven't seen where you use .extend feature for zod and besides, the generated output is there to be modified accordingly.

I don't see why this should be an obstacle for supporting myzod.

Moreover, you can use .and or Type.map to transform/modify the schema

const AttributeInputSchemaWithCUID = AttributeInputSchema().extend({
  key: z.string().cuid(),
})

Can be written as:

const withCUID = myzod.object({   key: z.string()  .withPredicate(CUID.isValid, 'must be a valid CUID')
  .map(value => new CUID(value)),
 });
 
const AttributeInputSchemaWithCUID = AttributeInputSchema().and(withCUID)

@Parables
Copy link
Contributor Author

Parables commented Apr 2, 2022

So what do you say, does my PR gets merged or what? 93/93 test passed

@Code-Hex
Copy link
Owner

Code-Hex commented Apr 2, 2022

@Parables Amazing your work! Thanks a lot.
And I appreciate your answer about how to extend in myzod also.

I need a little bit fix documents for myzod. but I can handle for it. Great job!

One more!

Could you fix to be valid syntax of your suggested sample code in your comment? 🙏

#25 (comment)

modified

I just published https://github.com/Code-Hex/graphql-codegen-typescript-validation-schema/releases/tag/v0.5.0

@Code-Hex
Copy link
Owner

Code-Hex commented Apr 2, 2022

Your code is broken on a few points. I need to fix them,

@Parables
Copy link
Contributor Author

Parables commented Apr 2, 2022

I am really grateful for accepting my PR.

Your code is broken on a few points. I need to fix them,

Please let me know what I have to do and I will do it ASAP

@Code-Hex
Copy link
Owner

Code-Hex commented Apr 2, 2022

@Parables I fixed the problem in here #28

So no problem for now (in v0.5.0)

@Code-Hex
Copy link
Owner

Code-Hex commented Apr 3, 2022

@Parables Sorry again. One more!

Could you fix to be valid syntax of your suggested sample code in your comment? 🙏

#25 (comment)

Around const withCUID = myzod.object

@Parables
Copy link
Contributor Author

Parables commented Apr 5, 2022

import cuid from "cuid";
import myzod, { Infer } from "myzod";
import isEmail from "validator/es/lib/isEmail";
import isStrongPassword from "validator/es/lib/isStrongPassword";

const ProfileInputSchema = myzod
  .object({
    username: myzod.string().min(3),
    email: myzod
      .string()
      .withPredicate((value) => isEmail(value), "Email must be a valid email"),
    password: myzod
      .string()
      .min(8)
      .withPredicate(isStrongPassword, "Password is too weak"),
    confirmedPassword: myzod.string()
  })
  .withPredicate(
    (value) => value.password === value.confirmedPassword,
    "password and confirmed do not match"
  );

const ProfileSchema = myzod.pick(ProfileInputSchema, ["username", "email"]);

const WithCUID = myzod.object({
  id: myzod
    .string()
    .optional()
    .map((value) => value ?? cuid())
});

const ProfileSchemaWithCuid = ProfileSchema.and(WithCUID);

type Profile = Infer<typeof ProfileSchemaWithCuid>;

const result = ProfileSchemaWithCuid.try({
  username: "Parables",
  email: "[email protected]"
});

if (result instanceof myzod.ValidationError) {
  console.log("Invalid format");
} else {
  console.log("Result: ", result);
}

// console.log("Shape of ProfileSchemaWithCuid: ", ProfileSchemaWithCuid.shape());

@Parables
Copy link
Contributor Author

Parables commented Apr 5, 2022

checkout a live demo https://codesandbox.io/s/extend-myzod-schema-giwqf7

@Parables
Copy link
Contributor Author

Parables commented Apr 5, 2022

Hope it helps... let me know if there is anything else....

Thanks for accepting my PR once again... mean a lot to me

@Parables
Copy link
Contributor Author

Parables commented Apr 6, 2022 via email

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

No branches or pull requests

2 participants