timdeschryver / zod-fixture

Creating fixtures based on zod schemas
https://zod-fixture.timdeschryver.dev/
MIT License
122 stars 10 forks source link

String-specific validations #2

Closed timdeschryver closed 1 year ago

timdeschryver commented 2 years ago

Implement string-specific validations

z.string().email();
z.string().url();
z.string().uuid();
z.string().cuid();
z.string().regex(regex);
z.string().startsWith(string);
z.string().endsWith(string);
franok commented 1 year ago

Hi @timdeschryver I just came across your library (via your blog post) and found it quite useful.

For my project, I had to create a uuidCustomization which looks like this:

import { v4 as uuidv4 } from 'uuid';
import {
  createFixture,
  type Customization
} from 'zod-fixture';

const uuidCustomization: Customization = {
  condition: ({ type, propertName }) =>
    type === 'string' && propertName === 'id',
  generator: () => {
    return uuidv4();
  },
};

I wanted to know about the project's general state: Do you plan to continue development of this library? If yes, I thought of contributing UUIDs a default customization to zod-fixture.

Just let me know, if you would appreciate a contribution. However, I am quite new to typescript, so please bear with me, when I give it a try 😅.


Side note:

Although I know zod-fixture is purposed for use in test code, I wanted suggest to use the uuid package, rather than using Math.random for generating uuids. While the chance of collisions during a test is rather low, I just thought it would be a "cleaner" way then generating uuids "by hand".

timdeschryver commented 1 year ago

👋 Hey @franok we would love a contribution. While the snippet works for your case, I'm think we should make the solution more versatile. We can make use of the shape to detect if a uuid is requested.

image

I didn't include a package to generate the uuid's too keep things as small as possible, but I agree that it would be better to make this more robust and use the suggested package.

timdeschryver commented 1 year ago

@franok v1.2 just got released and it creates a uuid when using z.string().uuid().

franok commented 1 year ago

cool, thx 👍

timdeschryver commented 1 year ago

@franok feel free to create a PR to replace the guid implementation if you want.